ltsecurityinc安防产品在线商城代码
This commit is contained in:
+98
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Framework\Api;
|
||||
|
||||
use Magento\TestModuleExtensionAttributes\Model\Data\FakeRegionFactory;
|
||||
use Magento\TestModuleExtensionAttributes\Api\Data\FakeRegionExtension;
|
||||
|
||||
/**
|
||||
* Test for \Magento\Framework\Api\AbstractExtensibleObject
|
||||
*/
|
||||
class AbstractExtensibleObjectTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/** @var \Magento\Framework\ObjectManagerInterface */
|
||||
private $_objectManager;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->_objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$this->_objectManager->configure(
|
||||
[
|
||||
'preferences' => [
|
||||
\Magento\TestModuleExtensionAttributes\Api\Data\FakeAddressInterface::class =>
|
||||
\Magento\TestModuleExtensionAttributes\Model\FakeAddress::class,
|
||||
\Magento\TestModuleExtensionAttributes\Api\Data\FakeRegionInterface::class =>
|
||||
\Magento\TestModuleExtensionAttributes\Model\FakeRegion::class,
|
||||
],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test setExtensionAttributes and getExtensionAttributes for \Magento\Framework\Api\AbstractExtensibleObject
|
||||
*
|
||||
* @param array $expectedDataBefore
|
||||
* @param array $expectedDataAfter
|
||||
* @dataProvider extensionAttributesDataProvider
|
||||
*/
|
||||
public function testExtensionAttributes($expectedDataBefore, $expectedDataAfter)
|
||||
{
|
||||
/** @var \Magento\Framework\Api\ExtensionAttributesFactory $regionExtensionFactory */
|
||||
$regionExtensionFactory = $this->_objectManager->get(\Magento\Framework\Api\ExtensionAttributesFactory::class);
|
||||
/** @var FakeRegionFactory $regionFactory */
|
||||
$regionFactory = $this->_objectManager->get(FakeRegionFactory::class);
|
||||
|
||||
/** @var \Magento\TestModuleExtensionAttributes\Model\Data\FakeRegion $region */
|
||||
$region = $regionFactory->create();
|
||||
|
||||
$regionCode = 'test_code';
|
||||
/** @var \Magento\TestModuleExtensionAttributes\Model\Data\FakeRegionExtensionInterface $regionExtension */
|
||||
$regionExtension = $regionExtensionFactory->create(
|
||||
\Magento\TestModuleExtensionAttributes\Model\Data\FakeRegion::class,
|
||||
['data' => $expectedDataBefore]
|
||||
);
|
||||
$region->setRegionCode($regionCode)->setExtensionAttributes($regionExtension);
|
||||
$this->assertInstanceOf(\Magento\TestModuleExtensionAttributes\Model\Data\FakeRegion::class, $region);
|
||||
|
||||
$extensionAttributes = $region->getExtensionAttributes();
|
||||
$this->assertInstanceOf(FakeRegionExtension::class, $extensionAttributes);
|
||||
$this->assertEquals($expectedDataBefore, $extensionAttributes->__toArray());
|
||||
$this->assertEquals($regionCode, $region->getRegionCode());
|
||||
|
||||
$regionCode = 'changed_test_code';
|
||||
$region->setExtensionAttributes(
|
||||
$regionExtensionFactory->create(
|
||||
\Magento\TestModuleExtensionAttributes\Model\Data\FakeRegion::class,
|
||||
['data' => $expectedDataAfter]
|
||||
)
|
||||
)->setRegionCode($regionCode); // change $regionCode to test AbstractExtensibleObject::setData
|
||||
$extensionAttributes = $region->getExtensionAttributes();
|
||||
$this->assertEquals($expectedDataAfter, $extensionAttributes->__toArray());
|
||||
$this->assertEquals($regionCode, $region->getRegionCode());
|
||||
}
|
||||
|
||||
public function extensionAttributesDataProvider()
|
||||
{
|
||||
return [
|
||||
'boolean' => [
|
||||
[true],
|
||||
[false]
|
||||
],
|
||||
'integer' => [
|
||||
[1],
|
||||
[2]
|
||||
],
|
||||
'string' => [
|
||||
['test'],
|
||||
['test test']
|
||||
],
|
||||
'array' => [
|
||||
[[1]],
|
||||
[[1, 2]]
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Framework\Api\ExtensionAttribute\Config;
|
||||
|
||||
/**
|
||||
* Tests for \Magento\Framework\Api\ExtensionAttribute\Config\Reader
|
||||
*/
|
||||
class ReaderTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Framework\Api\ExtensionAttribute\Config\Reader
|
||||
*/
|
||||
protected $_model;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $_fileList;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\App\Arguments\FileResolver\Primary
|
||||
*/
|
||||
protected $_fileResolverMock;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\App\Arguments\ValidationState
|
||||
*/
|
||||
protected $_validationState;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Api\ExtensionAttribute\Config\SchemaLocator
|
||||
*/
|
||||
protected $_schemaLocator;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Api\ExtensionAttribute\Config\Converter
|
||||
*/
|
||||
protected $_converter;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$fixturePath = realpath(__DIR__ . '/_files') . '/';
|
||||
$this->_fileList = [
|
||||
file_get_contents($fixturePath . 'config_one.xml'),
|
||||
file_get_contents($fixturePath . 'config_two.xml'),
|
||||
];
|
||||
|
||||
$this->_fileResolverMock = $this->getMockBuilder(\Magento\Framework\App\Arguments\FileResolver\Primary::class)
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(['get'])
|
||||
->getMock();
|
||||
$this->_fileResolverMock->expects($this->once())
|
||||
->method('get')
|
||||
->willReturn($this->_fileList);
|
||||
|
||||
$this->_converter = new \Magento\Framework\Api\ExtensionAttribute\Config\Converter();
|
||||
|
||||
$this->_validationState = new \Magento\Framework\App\Arguments\ValidationState(
|
||||
\Magento\Framework\App\State::MODE_DEFAULT
|
||||
);
|
||||
$this->_schemaLocator = new \Magento\Framework\Api\ExtensionAttribute\Config\SchemaLocator(
|
||||
new \Magento\Framework\Config\Dom\UrnResolver()
|
||||
);
|
||||
}
|
||||
|
||||
public function testMerge()
|
||||
{
|
||||
$model = new \Magento\Framework\Api\ExtensionAttribute\Config\Reader(
|
||||
$this->_fileResolverMock,
|
||||
$this->_converter,
|
||||
$this->_schemaLocator,
|
||||
$this->_validationState
|
||||
);
|
||||
|
||||
$expectedArray = [
|
||||
\Magento\Tax\Api\Data\TaxRateInterface::class => [],
|
||||
\Magento\Catalog\Api\Data\Product::class => [
|
||||
'stock_item' => [
|
||||
"type" => "Magento\CatalogInventory\Api\Data\StockItem",
|
||||
"resourceRefs" => [],
|
||||
"join" => null,
|
||||
],
|
||||
],
|
||||
\Magento\Customer\Api\Data\CustomerInterface::class => [
|
||||
'custom_1' => [
|
||||
"type" => "Magento\Customer\Api\Data\CustomerCustom",
|
||||
"resourceRefs" => [],
|
||||
"join" => null,
|
||||
],
|
||||
'custom_2' => [
|
||||
"type" => "Magento\CustomerExtra\Api\Data\CustomerCustom22",
|
||||
"resourceRefs" => [],
|
||||
"join" => null,
|
||||
],
|
||||
'custom_3' => [
|
||||
"type" => "Magento\Customer\Api\Data\CustomerCustom3",
|
||||
"resourceRefs" => [],
|
||||
"join" => null,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$this->assertEquals($expectedArray, $model->read('global'));
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0"?>
|
||||
<!--
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
-->
|
||||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
|
||||
<extension_attributes for="Magento\Tax\Api\Data\TaxRateInterface">
|
||||
</extension_attributes>
|
||||
<extension_attributes for="Magento\Catalog\Api\Data\Product">
|
||||
<attribute code="stock_item" type="Magento\CatalogInventory\Api\Data\StockItem" />
|
||||
</extension_attributes>
|
||||
<extension_attributes for="Magento\Customer\Api\Data\CustomerInterface">
|
||||
<attribute code="custom_1" type="Magento\Customer\Api\Data\CustomerCustom" />
|
||||
<attribute code="custom_2" type="Magento\CustomerExtra\Service\V1\Data\CustomerCustom21" />
|
||||
</extension_attributes>
|
||||
</config>
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0"?>
|
||||
<!--
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
-->
|
||||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
|
||||
<extension_attributes for="Magento\Customer\Api\Data\CustomerInterface">
|
||||
<attribute code="custom_2" type="Magento\CustomerExtra\Api\Data\CustomerCustom22" />
|
||||
<attribute code="custom_3" type="Magento\Customer\Api\Data\CustomerCustom3" />
|
||||
</extension_attributes>
|
||||
</config>
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Framework\Api\ExtensionAttribute;
|
||||
|
||||
use Magento\Catalog\Api\Data\ProductInterface;
|
||||
use Magento\Catalog\Api\Data\ProductExtensionInterface;
|
||||
use Magento\Customer\Api\Data\CustomerInterface;
|
||||
use Magento\Customer\Api\Data\CustomerExtensionInterface;
|
||||
|
||||
/**
|
||||
* Class to test the automatic generation of extension attributes object.
|
||||
*/
|
||||
class ExtensionAttributesGenerationTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* Test extension attributes generation for extensible models.
|
||||
*
|
||||
* Make sure that extension attributes object is not empty after instantiation
|
||||
* of objects inherited from @see \Magento\Framework\Model\AbstractExtensibleModel.
|
||||
*
|
||||
* In addition, verify that empty objects are not generated for complex extension attributes.
|
||||
*/
|
||||
public function testAttributeObjectGenerationForExtensibleModel()
|
||||
{
|
||||
/** @var \Magento\Framework\ObjectManagerInterface */
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
/** @var ProductInterface $product */
|
||||
$product = $objectManager->get(ProductInterface::class);
|
||||
|
||||
$extensionAttributes = $product->getExtensionAttributes();
|
||||
$this->assertInstanceOf(ProductExtensionInterface::class, $extensionAttributes);
|
||||
|
||||
$stockItemExtensionAttribute = $extensionAttributes->getStockItem();
|
||||
$this->assertNull($stockItemExtensionAttribute);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test extension attributes generation for extensible objects.
|
||||
*
|
||||
* Make sure that extension attributes object is not empty after instantiation
|
||||
* of objects inherited from @see \Magento\Framework\Api\AbstractExtensibleObject
|
||||
*/
|
||||
public function testAttributeObjectGenerationForExtensibleObject()
|
||||
{
|
||||
/** @var \Magento\Framework\ObjectManagerInterface */
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
/** @var CustomerInterface $customer */
|
||||
$customer = $objectManager->get(CustomerInterface::class);
|
||||
|
||||
$extensionAttributes = $customer->getExtensionAttributes();
|
||||
$this->assertInstanceOf(CustomerExtensionInterface::class, $extensionAttributes);
|
||||
}
|
||||
}
|
||||
+444
@@ -0,0 +1,444 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Framework\Api\ExtensionAttribute;
|
||||
|
||||
use Magento\Framework\Api\ExtensionAttribute\Config\Converter;
|
||||
use Magento\Framework\Api\ExtensionAttribute\Config\Reader;
|
||||
use Magento\Framework\Api\ExtensionAttribute\JoinData;
|
||||
use Magento\Framework\Api\ExtensionAttribute\JoinDataInterfaceFactory;
|
||||
use Magento\Framework\Reflection\TypeProcessor;
|
||||
use Magento\Framework\App\ResourceConnection as AppResource;
|
||||
use Magento\Framework\Api\ExtensionAttributesFactory;
|
||||
|
||||
/**
|
||||
* Class to test the JoinProcessor functionality
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
||||
*/
|
||||
class JoinProcessorTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Framework\Api\ExtensionAttribute\JoinProcessor
|
||||
*/
|
||||
private $joinProcessor;
|
||||
|
||||
/**
|
||||
* @var Reader|\PHPUnit\Framework\MockObject\MockObject
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* @var JoinDataInterfaceFactory|\PHPUnit\Framework\MockObject\MockObject
|
||||
*/
|
||||
private $extensionAttributeJoinDataFactory;
|
||||
|
||||
/**
|
||||
* @var TypeProcessor|\PHPUnit\Framework\MockObject\MockObject
|
||||
*/
|
||||
private $typeProcessor;
|
||||
|
||||
/**
|
||||
* @var AppResource|\PHPUnit\Framework\MockObject\MockObject
|
||||
*/
|
||||
private $appResource;
|
||||
|
||||
/**
|
||||
* @var ExtensionAttributesFactory|\PHPUnit\Framework\MockObject\MockObject
|
||||
*/
|
||||
private $extensionAttributesFactory;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Api\ExtensionAttribute\JoinProcessorHelper
|
||||
*/
|
||||
private $joinProcessorHelper;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->config = $this->getMockBuilder(\Magento\Framework\Api\ExtensionAttribute\Config::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$this->extensionAttributeJoinDataFactory = $this
|
||||
->getMockBuilder(\Magento\Framework\Api\ExtensionAttribute\JoinDataInterfaceFactory::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$this->typeProcessor = $this->getMockBuilder(\Magento\Framework\Reflection\TypeProcessor::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$this->extensionAttributesFactory = $this->getMockBuilder(
|
||||
\Magento\Framework\Api\ExtensionAttributesFactory::class
|
||||
)->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
/** @var \Magento\Framework\ObjectManagerInterface */
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
|
||||
$this->appResource = $objectManager->get(\Magento\Framework\App\ResourceConnection::class);
|
||||
|
||||
$this->joinProcessorHelper = $objectManager->create(
|
||||
\Magento\Framework\Api\ExtensionAttribute\JoinProcessorHelper::class,
|
||||
[
|
||||
'config' => $this->config,
|
||||
'joinDataInterfaceFactory' => $this->extensionAttributeJoinDataFactory
|
||||
]
|
||||
);
|
||||
|
||||
$this->joinProcessor = $objectManager->create(
|
||||
\Magento\Framework\Api\ExtensionAttribute\JoinProcessor::class,
|
||||
[
|
||||
'objectManager' => $objectManager,
|
||||
'typeProcessor' => $this->typeProcessor,
|
||||
'joinProcessorHelper' => $this->joinProcessorHelper
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the processing of the join config for a particular type
|
||||
*/
|
||||
public function testProcess()
|
||||
{
|
||||
$this->config->expects($this->once())
|
||||
->method('get')
|
||||
->willReturn($this->getConfig());
|
||||
|
||||
$collection = $this->getMockBuilder(\Magento\Framework\Data\Collection\AbstractDb::class)
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(['joinExtensionAttribute'])
|
||||
->getMockForAbstractClass();
|
||||
|
||||
$extensionAttributeJoinData = new JoinData();
|
||||
$this->extensionAttributeJoinDataFactory
|
||||
->expects($this->once())
|
||||
->method('create')
|
||||
->willReturn($extensionAttributeJoinData);
|
||||
|
||||
$collection->expects($this->once())->method('joinExtensionAttribute')->with($extensionAttributeJoinData);
|
||||
|
||||
$this->joinProcessor->process($collection, \Magento\Catalog\Api\Data\ProductInterface::class);
|
||||
$expectedTableName = 'reviews';
|
||||
$this->assertEquals($expectedTableName, $extensionAttributeJoinData->getReferenceTable());
|
||||
$this->assertEquals('extension_attribute_review_id', $extensionAttributeJoinData->getReferenceTableAlias());
|
||||
$this->assertEquals('product_id', $extensionAttributeJoinData->getReferenceField());
|
||||
$this->assertEquals('id', $extensionAttributeJoinData->getJoinField());
|
||||
$this->assertEquals(
|
||||
[
|
||||
[
|
||||
'external_alias' => 'review_id',
|
||||
'internal_alias' => 'extension_attribute_review_id_db_review_id',
|
||||
'with_db_prefix' => 'extension_attribute_review_id.db_review_id',
|
||||
'setter' => 'setReviewId',
|
||||
]
|
||||
],
|
||||
$extensionAttributeJoinData->getSelectFields()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Will return the data that is expected from the config object
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getConfig()
|
||||
{
|
||||
return [\Magento\Catalog\Api\Data\ProductInterface::class => [
|
||||
'review_id' => [
|
||||
Converter::DATA_TYPE => 'string',
|
||||
Converter::RESOURCE_PERMISSIONS => [],
|
||||
Converter::JOIN_DIRECTIVE => [
|
||||
Converter::JOIN_REFERENCE_TABLE => "reviews",
|
||||
Converter::JOIN_REFERENCE_FIELD => "product_id",
|
||||
Converter::JOIN_FIELDS => [
|
||||
[
|
||||
Converter::JOIN_FIELD => "review_id",
|
||||
Converter::JOIN_FIELD_COLUMN => "db_review_id",
|
||||
],
|
||||
],
|
||||
Converter::JOIN_ON_FIELD => "id",
|
||||
],
|
||||
],
|
||||
], \Magento\Customer\Api\Data\CustomerInterface::class => [
|
||||
'library_card_id' => [
|
||||
Converter::DATA_TYPE => 'string',
|
||||
Converter::RESOURCE_PERMISSIONS => [],
|
||||
Converter::JOIN_DIRECTIVE => [
|
||||
Converter::JOIN_REFERENCE_TABLE => "library_account",
|
||||
Converter::JOIN_FIELDS => [
|
||||
[
|
||||
Converter::JOIN_FIELD => "library_card_id",
|
||||
Converter::JOIN_FIELD_COLUMN => "",
|
||||
],
|
||||
],
|
||||
Converter::JOIN_ON_FIELD => "customer_id",
|
||||
],
|
||||
],
|
||||
'reviews' => [
|
||||
Converter::DATA_TYPE => 'Magento\Reviews\Api\Data\Reviews[]',
|
||||
Converter::RESOURCE_PERMISSIONS => [],
|
||||
Converter::JOIN_DIRECTIVE => [
|
||||
Converter::JOIN_REFERENCE_TABLE => "reviews",
|
||||
Converter::JOIN_FIELDS => [
|
||||
[
|
||||
Converter::JOIN_FIELD => "comment",
|
||||
Converter::JOIN_FIELD_COLUMN => "",
|
||||
],
|
||||
[
|
||||
Converter::JOIN_FIELD => "rating",
|
||||
Converter::JOIN_FIELD_COLUMN => "",
|
||||
],
|
||||
],
|
||||
Converter::JOIN_ON_FIELD => "customer_id",
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function testProcessSqlSelectVerification()
|
||||
{
|
||||
/** @var \Magento\Framework\ObjectManagerInterface */
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
/** @var \Magento\Framework\Api\ExtensionAttribute\Config $config */
|
||||
$config = $objectManager->get(\Magento\Framework\Api\ExtensionAttribute\Config::class);
|
||||
$config->reset();
|
||||
|
||||
$extensionConfigFileResolverMock = $this->getMockBuilder(\Magento\Framework\Config\FileResolverInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$extensionConfigFilePath = __DIR__ . '/../_files/extension_attributes.xml';
|
||||
$extensionConfigFileContent = file_get_contents($extensionConfigFilePath);
|
||||
$extensionConfigFileResolverMock->expects($this->any())
|
||||
->method('get')
|
||||
->willReturn([$extensionConfigFilePath => $extensionConfigFileContent]);
|
||||
$configReader = $objectManager->create(
|
||||
\Magento\Framework\Api\ExtensionAttribute\Config\Reader::class,
|
||||
['fileResolver' => $extensionConfigFileResolverMock]
|
||||
);
|
||||
/** @var \Magento\Framework\Api\ExtensionAttribute\Config $config */
|
||||
$config = $objectManager->create(
|
||||
\Magento\Framework\Api\ExtensionAttribute\Config::class,
|
||||
['reader' => $configReader]
|
||||
);
|
||||
|
||||
/** @var \Magento\Framework\Api\ExtensionAttribute\JoinProcessorHelper $extensionAttributesProcessorHelper */
|
||||
$extensionAttributesProcessorHelper = $objectManager->create(
|
||||
\Magento\Framework\Api\ExtensionAttribute\JoinProcessorHelper::class,
|
||||
['config' => $config]
|
||||
);
|
||||
|
||||
/** @var \Magento\Framework\Api\ExtensionAttribute\JoinProcessor $extensionAttributesProcessor */
|
||||
$extensionAttributesProcessor = $objectManager->create(
|
||||
\Magento\Framework\Api\ExtensionAttribute\JoinProcessor::class,
|
||||
['joinProcessorHelper' => $extensionAttributesProcessorHelper]
|
||||
);
|
||||
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
|
||||
$collection = $objectManager->create(\Magento\Catalog\Model\ResourceModel\Product\Collection::class);
|
||||
$extensionAttributesProcessor->process($collection);
|
||||
$config->reset();
|
||||
|
||||
$catalogProductEntity = $this->appResource->getTableName('catalog_product_entity');
|
||||
$catalogInventoryStockItem = $this->appResource->getTableName('cataloginventory_stock_item');
|
||||
$reviews = $this->appResource->getTableName('reviews');
|
||||
$expectedSql = <<<EXPECTED_SQL
|
||||
SELECT `e`.*,
|
||||
`extension_attribute_stock_item`.`qty` AS `extension_attribute_stock_item_qty`,
|
||||
`extension_attribute_reviews`.`comment` AS `extension_attribute_reviews_comment`,
|
||||
`extension_attribute_reviews`.`rating` AS `extension_attribute_reviews_rating`,
|
||||
`extension_attribute_reviews`.`date` AS `extension_attribute_reviews_date` FROM `$catalogProductEntity` AS `e`
|
||||
LEFT JOIN `$catalogInventoryStockItem` AS `extension_attribute_stock_item` ON e.id = extension_attribute_stock_item.id
|
||||
LEFT JOIN `$reviews` AS `extension_attribute_reviews` ON e.id = extension_attribute_reviews.product_id
|
||||
EXPECTED_SQL;
|
||||
$resultSql = $collection->getSelectSql(true);
|
||||
$formattedResultSql = str_replace(',', ",\n ", $resultSql);
|
||||
$this->assertStringContainsString($expectedSql, $formattedResultSql);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Catalog/_files/products.php
|
||||
*/
|
||||
public function testGetListWithExtensionAttributesAbstractModel()
|
||||
{
|
||||
/** @var \Magento\Framework\ObjectManagerInterface */
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
|
||||
$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
|
||||
|
||||
$firstProductId = (int)$productRepository->get('simple')->getId();
|
||||
$firstProductQty = 11;
|
||||
$secondProductId = (int)$productRepository->get('custom-design-simple-product')->getId();
|
||||
$secondProductQty = 22;
|
||||
/** @var \Magento\CatalogInventory\Api\StockItemRepositoryInterface $stockItemRepository */
|
||||
$stockItemRepository = $objectManager->get(\Magento\CatalogInventory\Api\StockItemRepositoryInterface::class);
|
||||
|
||||
/** Prepare stock items */
|
||||
$firstStockItem = $productRepository->getById($firstProductId)->getExtensionAttributes()->getStockItem();
|
||||
$firstStockItem->setQty($firstProductQty);
|
||||
$stockItemRepository->save($firstStockItem);
|
||||
$this->assertEquals(
|
||||
$firstProductQty,
|
||||
$productRepository->getById($firstProductId)->getExtensionAttributes()->getStockItem()->getQty(),
|
||||
'Precondition failed.'
|
||||
);
|
||||
$secondStockItem = $productRepository->getById($secondProductId)->getExtensionAttributes()->getStockItem();
|
||||
$secondStockItem->setQty($secondProductQty);
|
||||
$stockItemRepository->save($secondStockItem);
|
||||
$this->assertEquals(
|
||||
$secondProductQty,
|
||||
$productRepository->getById($secondProductId)->getExtensionAttributes()->getStockItem()->getQty(),
|
||||
'Precondition failed.'
|
||||
);
|
||||
|
||||
/** @var \Magento\Framework\Api\Search\FilterGroup $searchCriteriaGroup */
|
||||
$searchCriteriaGroup = $objectManager->create(\Magento\Framework\Api\Search\FilterGroup::class);
|
||||
/** @var \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria */
|
||||
$searchCriteria = $objectManager->create(\Magento\Framework\Api\SearchCriteriaInterface::class);
|
||||
$searchCriteria->setFilterGroups([$searchCriteriaGroup]);
|
||||
$products = $productRepository->getList($searchCriteria)->getItems();
|
||||
|
||||
/** Ensure that simple extension attributes were populated correctly */
|
||||
$this->assertEquals(
|
||||
$firstProductQty,
|
||||
$products[$firstProductId]->getExtensionAttributes()->getTestStockItemQty()
|
||||
);
|
||||
$this->assertEquals(
|
||||
$secondProductQty,
|
||||
$products[$secondProductId]->getExtensionAttributes()->getTestStockItemQty()
|
||||
);
|
||||
|
||||
/** Check population of complex extension attributes */
|
||||
$this->assertEquals(
|
||||
$firstProductQty,
|
||||
$products[$firstProductId]->getExtensionAttributes()->getTestStockItem()->getQty()
|
||||
);
|
||||
$this->assertNotEmpty($products[$firstProductId]->getExtensionAttributes()->getTestStockItem()->getItemId());
|
||||
|
||||
$this->assertArrayNotHasKey(
|
||||
'extension_attribute_test_stock_item_qty_qty',
|
||||
$products[$firstProductId]->getData(),
|
||||
"Selected extension field should be unset after it is added to extension attributes object."
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Customer/_files/customer.php
|
||||
* @magentoDataFixture Magento/Customer/_files/customer_group.php
|
||||
*/
|
||||
public function testGetListWithExtensionAttributesAbstractObject()
|
||||
{
|
||||
$customerId = 1;
|
||||
$customerGroupName = 'General';
|
||||
$taxClassId = 3;
|
||||
/** @var \Magento\Framework\ObjectManagerInterface */
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
|
||||
/** @var \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository */
|
||||
$customerRepository = $objectManager->get(\Magento\Customer\Api\CustomerRepositoryInterface::class);
|
||||
/** @var \Magento\Framework\Api\Search\FilterGroup $searchCriteriaGroup */
|
||||
$searchCriteriaGroup = $objectManager->create(\Magento\Framework\Api\Search\FilterGroup::class);
|
||||
/** @var \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria */
|
||||
$searchCriteria = $objectManager->create(\Magento\Framework\Api\SearchCriteriaInterface::class);
|
||||
$searchCriteria->setFilterGroups([$searchCriteriaGroup]);
|
||||
$customers = $customerRepository->getList($searchCriteria)->getItems();
|
||||
|
||||
/** Ensure that simple extension attributes were populated correctly */
|
||||
$customer = $customers[0];
|
||||
$this->assertEquals($customerId, $customer->getId(), 'Precondition failed');
|
||||
$this->assertEquals($customerGroupName, $customer->getExtensionAttributes()->getTestGroupCode());
|
||||
|
||||
/** Check population of complex extension attributes */
|
||||
$this->assertEquals($taxClassId, $customer->getExtensionAttributes()->getTestGroup()->getTaxClassId());
|
||||
$this->assertEquals($customerGroupName, $customer->getExtensionAttributes()->getTestGroup()->getCode());
|
||||
}
|
||||
|
||||
public function testGetListWithFilterBySimpleDummyAttributeWithMapping()
|
||||
{
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$groupRepository = $objectManager->create(\Magento\Customer\Api\GroupRepositoryInterface::class);
|
||||
/** @var \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder */
|
||||
$searchCriteriaBuilder = $objectManager->create(\Magento\Framework\Api\SearchCriteriaBuilder::class);
|
||||
$builder = $objectManager->create(\Magento\Framework\Api\FilterBuilder::class);
|
||||
$joinedExtensionAttribute = 'test_dummy_attribute';
|
||||
$joinedExtensionAttributeValue = 'website_id';
|
||||
$filter = $builder->setField($joinedExtensionAttribute)
|
||||
->setValue($joinedExtensionAttributeValue)
|
||||
->create();
|
||||
$searchCriteriaBuilder->addFilters([$filter]);
|
||||
$searchResults = $groupRepository->getList($searchCriteriaBuilder->create());
|
||||
$items = $searchResults->getItems();
|
||||
$this->assertCount(1, $items, 'Filtration by extension attribute does not work.');
|
||||
$expectedGroupCode = 'General';
|
||||
$this->assertEquals($expectedGroupCode, $items[0]->getCode(), 'Invalid group loaded.');
|
||||
$this->assertNotNull($items[0]->getExtensionAttributes(), "Extension attributes not loaded");
|
||||
$this->assertEquals(
|
||||
$joinedExtensionAttributeValue,
|
||||
$items[0]->getExtensionAttributes()->getTestDummyAttribute(),
|
||||
"Extension attributes were not loaded correctly"
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetListWithFilterByComplexDummyAttributeWithSetterMapping()
|
||||
{
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$groupRepository = $objectManager->create(\Magento\Customer\Api\GroupRepositoryInterface::class);
|
||||
/** @var \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder */
|
||||
$searchCriteriaBuilder = $objectManager->create(\Magento\Framework\Api\SearchCriteriaBuilder::class);
|
||||
$builder = $objectManager->create(\Magento\Framework\Api\FilterBuilder::class);
|
||||
$joinedExtensionAttribute = 'test_complex_dummy_attribute.frontend_label';
|
||||
$joinedExtensionAttributeValue = 'firstname';
|
||||
$filter = $builder->setField($joinedExtensionAttribute)
|
||||
->setValue($joinedExtensionAttributeValue)
|
||||
->create();
|
||||
$searchCriteriaBuilder->addFilters([$filter]);
|
||||
$searchResults = $groupRepository->getList($searchCriteriaBuilder->create());
|
||||
$items = $searchResults->getItems();
|
||||
$this->assertCount(1, $items, 'Filtration by extension attribute does not work.');
|
||||
$expectedGroupCode = 'General';
|
||||
$this->assertEquals($expectedGroupCode, $items[0]->getCode(), 'Invalid group loaded.');
|
||||
$this->assertNotNull($items[0]->getExtensionAttributes(), "Extension attributes not loaded");
|
||||
$this->assertNotNull(
|
||||
$items[0]->getExtensionAttributes()->getTestComplexDummyAttribute(),
|
||||
"Complex extension attribute not loaded"
|
||||
);
|
||||
$this->assertEquals(
|
||||
'user',
|
||||
$items[0]->getExtensionAttributes()->getTestComplexDummyAttribute()->getAttributeCode(),
|
||||
"Extension attributes were not loaded correctly"
|
||||
);
|
||||
$this->assertEquals(
|
||||
$joinedExtensionAttributeValue,
|
||||
$items[0]->getExtensionAttributes()->getTestComplexDummyAttribute()->getFrontendLabel(),
|
||||
"Extension attributes were not loaded correctly"
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Sales/_files/invoice.php
|
||||
*/
|
||||
public function testGetListWithExtensionAttributesAutoGeneratedRepository()
|
||||
{
|
||||
$this->markTestSkipped(
|
||||
'Invoice repository is not autogenerated anymore and does not have joined extension attributes'
|
||||
);
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
$searchCriteriaBuilder = $objectManager->create(\Magento\Framework\Api\SearchCriteriaBuilder::class);
|
||||
/** @var \Magento\Sales\Api\InvoiceRepositoryInterface $invoiceRepository */
|
||||
$invoiceRepository = $objectManager->create(\Magento\Sales\Api\InvoiceRepositoryInterface::class);
|
||||
$invoices = $invoiceRepository->getList($searchCriteriaBuilder->create())->getItems();
|
||||
$this->assertCount(1, $invoices, "Invalid number of loaded invoices.");
|
||||
$invoice = reset($invoices);
|
||||
|
||||
/** @var \Magento\Eav\Model\Entity\Attribute $joinedEntity */
|
||||
$joinedEntity = $objectManager->create(\Magento\Eav\Model\Entity\Attribute::class);
|
||||
$joinedEntity->load($invoice->getId());
|
||||
$joinedExtensionAttributeValue = $joinedEntity->getAttributeCode();
|
||||
|
||||
$this->assertNotNull($invoice->getExtensionAttributes(), "Extension attributes not loaded");
|
||||
$this->assertEquals(
|
||||
$joinedExtensionAttributeValue,
|
||||
$invoice->getExtensionAttributes()->getTestDummyAttribute(),
|
||||
"Extension attributes were not loaded correctly"
|
||||
);
|
||||
}
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Framework\Api;
|
||||
|
||||
class ExtensionAttributesFactoryTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/** @var \Magento\Framework\Api\ExtensionAttributesFactory */
|
||||
private $factory;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
/** @var \Magento\Framework\ObjectManagerInterface */
|
||||
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
|
||||
|
||||
$this->factory = $objectManager->create(
|
||||
\Magento\Framework\Api\ExtensionAttributesFactory::class,
|
||||
['objectManager' => $objectManager]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public function testCreateThrowExceptionIfInterfaceNotImplemented()
|
||||
{
|
||||
$this->expectException(\LogicException::class);
|
||||
|
||||
$this->factory->create(\Magento\Framework\Api\ExtensionAttributesFactoryTest::class);
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public function testCreateThrowExceptionIfInterfaceNotOverridden()
|
||||
{
|
||||
$this->expectException(\LogicException::class);
|
||||
|
||||
$this->factory->create(\Magento\TestModuleExtensionAttributes\Model\Data\FakeExtensibleOne::class);
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public function testCreateThrowExceptionIfReturnIsIncorrect()
|
||||
{
|
||||
$this->expectException(\LogicException::class);
|
||||
|
||||
$this->factory->create(\Magento\TestModuleExtensionAttributes\Model\Data\FakeExtensibleTwo::class);
|
||||
}
|
||||
|
||||
public function testCreate()
|
||||
{
|
||||
$this->assertInstanceOf(
|
||||
\Magento\TestModuleExtensionAttributes\Api\Data\FakeRegionExtension::class,
|
||||
$this->factory->create(\Magento\TestModuleExtensionAttributes\Model\Data\FakeRegion::class)
|
||||
);
|
||||
}
|
||||
|
||||
public function testCreateWithLogicException()
|
||||
{
|
||||
$this->expectException('LogicException');
|
||||
$this->expectExceptionMessage(
|
||||
"Class 'Magento\\Framework\\Api\\ExtensionAttributesFactoryTest' must implement an interface, "
|
||||
. "which extends from 'Magento\\Framework\\Api\\ExtensibleDataInterface'"
|
||||
);
|
||||
$this->factory->create(get_class($this));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Framework\Api;
|
||||
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
|
||||
/**
|
||||
* Test of building the Data Object
|
||||
*/
|
||||
class SortOrderBuilderTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var SortOrderBuilder
|
||||
*/
|
||||
private $interceptedBuilder;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->interceptedBuilder = Bootstrap::getObjectManager()->get(SortOrderBuilder::class . '\Interceptor');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Builder successfully creates object when Interceptor instance is provided.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCreate(): void
|
||||
{
|
||||
$this->assertEquals(SortOrder::class, get_class($this->interceptedBuilder->create()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?xml version="1.0"?>
|
||||
<!--
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
-->
|
||||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
|
||||
<extension_attributes for="Magento\Catalog\Api\Data\ProductInterface">
|
||||
<attribute code="stock_item" type="Magento\CatalogInventory\Api\Data\StockItemInterface">
|
||||
<resources>
|
||||
<resource ref="Magento_CatalogInventory::cataloginventory"/>
|
||||
</resources>
|
||||
<join reference_table="cataloginventory_stock_item"
|
||||
join_on_field="id"
|
||||
reference_field="id"
|
||||
>
|
||||
<field>qty</field>
|
||||
</join>
|
||||
</attribute>
|
||||
<attribute code="reviews" type="Magento\Reviews\Api\Data\Reviews[]">
|
||||
<join reference_table="reviews"
|
||||
reference_field="product_id"
|
||||
join_on_field="id"
|
||||
>
|
||||
<field>comment</field>
|
||||
<field>rating</field>
|
||||
<field>date</field>
|
||||
</join>
|
||||
</attribute>
|
||||
</extension_attributes>
|
||||
<extension_attributes for="Magento\Catalog\Api\Data\ProductLinkInterface">
|
||||
<attribute code="qty" type="float"/>
|
||||
</extension_attributes>
|
||||
</config>
|
||||
Reference in New Issue
Block a user