Skip to content

Ensure that url suffix resolvers return strings to match function declaration #30910

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private function getCategoryUrlSuffix(int $storeId): string
self::$xml_path_category_url_suffix,
ScopeInterface::SCOPE_STORE,
$storeId
);
) ?? '';
}
return $this->categoryUrlSuffix[$storeId];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private function getProductUrlSuffix(int $storeId): string
self::$xml_path_product_url_suffix,
ScopeInterface::SCOPE_STORE,
$storeId
);
) ?? '';
}
return $this->productUrlSuffix[$storeId];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\CatalogUrlRewriteGraphQl\Test\Unit\Model\Resolver;

use Magento\CatalogUrlRewriteGraphQl\Model\Resolver\CategoryUrlSuffix;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\GraphQl\Model\Query\ContextExtensionInterface;
use Magento\GraphQl\Model\Query\ContextInterface;
use Magento\Store\Api\Data\StoreInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Magento\Framework\App\Config\ScopeConfigInterface;

/**
* Test for \Magento\CatalogUrlRewriteGraphQl\Model\Resolver\CategoryUrlSuffix.
*/
class CategoryUrlSuffixTest extends TestCase
{
/**
* @var ScopeConfigInterface|MockObject
*/
private $scopeConfigMock;

/**
* @var ContextInterface|MockObject
*/
private $contextMock;

/**
* @var ContextExtensionInterface|MockObject
*/
private $contextExtensionMock;

/**
* @var StoreInterface|MockObject
*/
private $storeMock;

/**
* @var Field|MockObject
*/
private $fieldMock;

/**
* @var ResolveInfo|MockObject
*/
private $resolveInfoMock;

/**
* @var CategoryUrlSuffix
*/
private $resolver;

/**
* @inheritDoc
*/
protected function setUp(): void
{
$this->contextMock = $this->getMockBuilder(ContextInterface::class)
->disableOriginalConstructor()
->setMethods(
[
'getExtensionAttributes'
]
)
->getMockForAbstractClass();

$this->contextExtensionMock = $this->getMockBuilder(ContextExtensionInterface::class)
->setMethods(
[
'getStore'
]
)
->getMockForAbstractClass();

$this->storeMock = $this->getMockBuilder(StoreInterface::class)
->setMethods(
[
'getId'
]
)
->getMockForAbstractClass();

$this->fieldMock = $this->getMockBuilder(Field::class)
->disableOriginalConstructor()
->getMock();

$this->resolveInfoMock = $this->getMockBuilder(ResolveInfo::class)
->disableOriginalConstructor()
->getMock();

$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
->getMockForAbstractClass();

$this->resolver = new CategoryUrlSuffix(
$this->scopeConfigMock
);
}

/**
* Verify that empty string is returned when config value is null
*/
public function testNullValue()
{
$this->scopeConfigMock->expects($this->once())
->method('getValue')
->willReturn(null);

$this->contextMock
->expects($this->once())
->method('getExtensionAttributes')
->willReturn($this->contextExtensionMock);

$this->contextExtensionMock
->expects($this->once())
->method('getStore')
->willReturn($this->storeMock);

$this->storeMock
->expects($this->once())
->method('getId')
->willReturn(1);

$this->assertEquals(
'',
$this->resolver->resolve(
$this->fieldMock,
$this->contextMock,
$this->resolveInfoMock
)
);
}

/**
* Verify that the configured value is returned
*/
public function testNonNullValue()
{
$value = 'html';
$this->scopeConfigMock->expects($this->once())
->method('getValue')
->willReturn($value);

$this->contextMock
->expects($this->once())
->method('getExtensionAttributes')
->willReturn($this->contextExtensionMock);

$this->contextExtensionMock
->expects($this->once())
->method('getStore')
->willReturn($this->storeMock);

$this->storeMock
->expects($this->once())
->method('getId')
->willReturn(1);

$this->assertEquals(
$value,
$this->resolver->resolve(
$this->fieldMock,
$this->contextMock,
$this->resolveInfoMock
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\CatalogUrlRewriteGraphQl\Test\Unit\Model\Resolver;

use Magento\CatalogUrlRewriteGraphQl\Model\Resolver\ProductUrlSuffix;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\GraphQl\Model\Query\ContextExtensionInterface;
use Magento\GraphQl\Model\Query\ContextInterface;
use Magento\Store\Api\Data\StoreInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Magento\Framework\App\Config\ScopeConfigInterface;

/**
* Test for \Magento\CatalogUrlRewriteGraphQl\Model\Resolver\ProductUrlSuffix.
*/
class ProductUrlSuffixTest extends TestCase
{
/**
* @var ScopeConfigInterface|MockObject
*/
private $scopeConfigMock;

/**
* @var ContextInterface|MockObject
*/
private $contextMock;

/**
* @var ContextExtensionInterface|MockObject
*/
private $contextExtensionMock;

/**
* @var StoreInterface|MockObject
*/
private $storeMock;

/**
* @var Field|MockObject
*/
private $fieldMock;

/**
* @var ResolveInfo|MockObject
*/
private $resolveInfoMock;

/**
* @var ProductUrlSuffix
*/
private $resolver;

/**
* @inheritDoc
*/
protected function setUp(): void
{
$this->contextMock = $this->getMockBuilder(ContextInterface::class)
->disableOriginalConstructor()
->setMethods(
[
'getExtensionAttributes'
]
)
->getMockForAbstractClass();

$this->contextExtensionMock = $this->getMockBuilder(ContextExtensionInterface::class)
->setMethods(
[
'getStore'
]
)
->getMockForAbstractClass();

$this->storeMock = $this->getMockBuilder(StoreInterface::class)
->setMethods(
[
'getId'
]
)
->getMockForAbstractClass();

$this->fieldMock = $this->getMockBuilder(Field::class)
->disableOriginalConstructor()
->getMock();

$this->resolveInfoMock = $this->getMockBuilder(ResolveInfo::class)
->disableOriginalConstructor()
->getMock();

$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
->getMockForAbstractClass();

$this->resolver = new ProductUrlSuffix(
$this->scopeConfigMock
);
}

/**
* Verify that empty string is returned when config value is null
*/
public function testNullValue()
{
$this->scopeConfigMock->expects($this->once())
->method('getValue')
->willReturn(null);

$this->contextMock
->expects($this->once())
->method('getExtensionAttributes')
->willReturn($this->contextExtensionMock);

$this->contextExtensionMock
->expects($this->once())
->method('getStore')
->willReturn($this->storeMock);

$this->storeMock
->expects($this->once())
->method('getId')
->willReturn(1);

$this->assertEquals(
'',
$this->resolver->resolve(
$this->fieldMock,
$this->contextMock,
$this->resolveInfoMock
)
);
}

/**
* Verify that the configured value is returned
*/
public function testNonNullValue()
{
$value = 'html';
$this->scopeConfigMock->expects($this->once())
->method('getValue')
->willReturn($value);

$this->contextMock
->expects($this->once())
->method('getExtensionAttributes')
->willReturn($this->contextExtensionMock);

$this->contextExtensionMock
->expects($this->once())
->method('getStore')
->willReturn($this->storeMock);

$this->storeMock
->expects($this->once())
->method('getId')
->willReturn(1);

$this->assertEquals(
$value,
$this->resolver->resolve(
$this->fieldMock,
$this->contextMock,
$this->resolveInfoMock
)
);
}
}