This repository was archived by the owner on Dec 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 153
Return full canonical product URL within product object #7
Merged
magento-engcom-team
merged 3 commits into
magento:2.3-develop
from
austris-argalis:issue-2
May 22, 2018
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
app/code/Magento/CatalogGraphQl/Model/Resolver/Product/CanonicalUrl.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CatalogGraphQl\Model\Resolver\Product; | ||
|
||
use Magento\Catalog\Model\Product; | ||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Query\Resolver\Value; | ||
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
|
||
/** | ||
* Resolve data for product canonical URL | ||
*/ | ||
class CanonicalUrl implements ResolverInterface | ||
{ | ||
/** | ||
* @var ValueFactory | ||
*/ | ||
private $valueFactory; | ||
|
||
/** | ||
* @param ValueFactory $valueFactory | ||
*/ | ||
public function __construct( | ||
ValueFactory $valueFactory | ||
) { | ||
$this->valueFactory = $valueFactory; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function resolve( | ||
Field $field, | ||
$context, | ||
ResolveInfo $info, | ||
array $value = null, | ||
array $args = null | ||
): Value { | ||
if (!isset($value['model'])) { | ||
$result = function () { | ||
return null; | ||
}; | ||
return $this->valueFactory->create($result); | ||
} | ||
|
||
/* @var $product Product */ | ||
$product = $value['model']; | ||
$url = $product->getUrlModel()->getUrl($product, ['_ignore_category' => true]); | ||
$result = function () use ($url) { | ||
return $url; | ||
}; | ||
|
||
return $this->valueFactory->create($result); | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
app/code/Magento/CatalogGraphQl/Test/Unit/Model/Resolver/Product/CanonicalUrlTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CatalogGraphQl\Test\Unit\Model\Resolver\Product; | ||
|
||
use Magento\CatalogGraphQl\Model\Resolver\Product\CanonicalUrl; | ||
use Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator; | ||
use Magento\Framework\GraphQl\Query\Resolver\Value; | ||
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory; | ||
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; | ||
use Magento\Store\Model\StoreManagerInterface; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class CanonicalUrlTest extends TestCase | ||
{ | ||
/** | ||
* @var ObjectManager | ||
*/ | ||
private $objectManager; | ||
|
||
/** | ||
* @var CanonicalUrl | ||
*/ | ||
private $subject; | ||
|
||
/** | ||
* @var \PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $mockValueFactory; | ||
|
||
/** | ||
* @var \PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $mockStoreManager; | ||
|
||
public function testReturnsNullWhenNoProductAvailable() | ||
{ | ||
$mockField = $this->getMockBuilder(\Magento\Framework\GraphQl\Config\Element\Field::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$mockInfo = $this->getMockBuilder(\Magento\Framework\GraphQl\Schema\Type\ResolveInfo::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$this->mockValueFactory->method('create')->with( | ||
$this->callback( | ||
function ($param) { | ||
return $param() === null; | ||
} | ||
) | ||
); | ||
|
||
$this->subject->resolve($mockField, '', $mockInfo, [], []); | ||
} | ||
|
||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
$this->objectManager = new ObjectManager($this); | ||
$this->mockStoreManager = $this->getMockBuilder(StoreManagerInterface::class)->getMock(); | ||
$this->mockValueFactory = $this->getMockBuilder(ValueFactory::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$this->mockValueFactory->method('create')->willReturn( | ||
$this->objectManager->getObject( | ||
Value::class, | ||
['callback' => function () { | ||
return ''; | ||
}] | ||
) | ||
); | ||
|
||
$mockProductUrlPathGenerator = $this->getMockBuilder(ProductUrlPathGenerator::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$mockProductUrlPathGenerator->method('getUrlPathWithSuffix')->willReturn('product_url.html'); | ||
|
||
$this->subject = $this->objectManager->getObject( | ||
CanonicalUrl::class, | ||
[ | ||
'valueFactory' => $this->mockValueFactory, | ||
'storeManager' => $this->mockStoreManager, | ||
'productUrlPathGenerator' => $mockProductUrlPathGenerator | ||
] | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI, unit tests are optional and should be added only when it makes sense (e.g. when you can test method API without mocks). Since the test is already completed, we can keep it.