123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Eav\Test\Unit\Model;
- use Magento\Eav\Model\Cache\Type as Cache;
- use Magento\Eav\Model\Config;
- use Magento\Eav\Model\Entity\Attribute;
- use Magento\Eav\Model\Entity\Type;
- use Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection;
- use Magento\Framework\DataObject;
- use Magento\Framework\Serialize\SerializerInterface;
- class ConfigTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Eav\Model\Config
- */
- protected $config;
- /**
- * @var \Magento\Framework\App\CacheInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $cacheMock;
- /**
- * @var \Magento\Eav\Model\Entity\TypeFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $typeFactoryMock;
- /**
- * @var \Magento\Eav\Model\ResourceModel\Entity\Type\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $collectionFactoryMock;
- /**
- * @var \Magento\Framework\App\Cache\StateInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $cacheStateMock;
- /**
- * @var \Magento\Framework\Validator\UniversalFactory|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $universalFactoryMock;
- /**
- * @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- private $serializerMock;
- /**
- * @var Type|\PHPUnit_Framework_MockObject_MockObject
- */
- private $typeMock;
- protected function setUp()
- {
- $this->cacheMock = $this->createMock(\Magento\Framework\App\CacheInterface::class);
- $this->typeFactoryMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\TypeFactory::class)
- ->setMethods(['create'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->collectionFactoryMock =
- $this->getMockBuilder(\Magento\Eav\Model\ResourceModel\Entity\Type\CollectionFactory::class)
- ->setMethods(['create'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->cacheStateMock = $this->createMock(\Magento\Framework\App\Cache\StateInterface::class);
- $this->universalFactoryMock = $this->getMockBuilder(\Magento\Framework\Validator\UniversalFactory::class)
- ->setMethods(['create'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->serializerMock = $this->createMock(SerializerInterface::class);
- $this->typeMock = $this->createMock(Type::class);
- $this->config = new Config(
- $this->cacheMock,
- $this->typeFactoryMock,
- $this->collectionFactoryMock,
- $this->cacheStateMock,
- $this->universalFactoryMock,
- $this->serializerMock
- );
- }
- public function testGetAttributeCache()
- {
- $attributeData = [
- 'attribute_code' => 'attribute_code_1',
- 'attribute_id' => 1
- ];
- $attributeCollectionMock = $this->getMockBuilder(
- Collection::class
- )->disableOriginalConstructor()
- ->setMethods(['getData', 'setEntityTypeFilter'])
- ->getMock();
- $attributeCollectionMock->expects($this->any())
- ->method('setEntityTypeFilter')
- ->will($this->returnSelf());
- $attributeCollectionMock->expects($this->any())
- ->method('getData')
- ->willReturn([$attributeData]);
- $entityAttributeMock = $this->getMockBuilder(Attribute::class)
- ->setMethods(['setData', 'loadByCode', 'toArray'])
- ->disableOriginalConstructor()
- ->getMock();
- $entityAttributeMock->expects($this->atLeastOnce())->method('setData')
- ->willReturnSelf();
- $entityAttributeMock->expects($this->atLeastOnce())->method('loadByCode')
- ->willReturnSelf();
- $factoryCalls = [
- [
- Collection::class,
- [],
- $attributeCollectionMock
- ],
- [
- Attribute::class,
- [],
- $entityAttributeMock
- ],
- ];
- $entityTypeData = [
- 'entity_type_id' => 'entity_type_id',
- 'entity_type_code' => 'entity_type_code'
- ];
- $collectionStub = new DataObject([$entityTypeData]);
- $this->collectionFactoryMock
- ->expects($this->any())
- ->method('create')
- ->willReturn($collectionStub);
- $entityType = $this->getMockBuilder(Type::class)
- ->setMethods(['getEntity', 'setData', 'getData', 'getEntityTypeCode', 'getId'])
- ->disableOriginalConstructor()
- ->getMock();
- $entityType->method('getEntityTypeCode')
- ->willReturn('entity_type_code');
- $entityType->method('getId')
- ->willReturn(101);
- $this->typeFactoryMock
- ->expects($this->any())
- ->method('create')
- ->willReturn($entityType);
- $this->universalFactoryMock
- ->expects($this->atLeastOnce())
- ->method('create')
- ->will($this->returnValueMap($factoryCalls));
- $this->assertInstanceOf(Attribute::class, $this->config->getAttribute($entityType, 'attribute_code_1'));
- }
- /**
- * @return array
- */
- public function getAttributeCacheDataProvider()
- {
- return [
- 'cache-disabled' => [
- false,
- 0,
- 0,
- false,
- ],
- 'cache-miss' => [
- true,
- 1,
- 0,
- false,
- ],
- 'cached' => [
- true,
- 1,
- 1,
- 'attribute serialzied data',
- ],
- ];
- }
- /**
- * @param boolean $cacheEnabled
- * @param int $loadCalls
- * @param int $cachedValue
- * @param int $unserializeCalls
- * @dataProvider getAttributeCacheDataProvider
- * @return void
- */
- public function testGetAttributes($cacheEnabled)
- {
- $attributeData = [
- 'attribute_code' => 'attribute_code_1',
- 'attribute_id' => 1
- ];
- $attributeCollectionMock = $this->getMockBuilder(
- Collection::class
- )->disableOriginalConstructor()
- ->setMethods(['getData', 'setEntityTypeFilter'])
- ->getMock();
- $attributeCollectionMock
- ->expects($this->any())
- ->method('setEntityTypeFilter')
- ->will($this->returnSelf());
- $attributeCollectionMock
- ->expects($this->any())
- ->method('getData')
- ->willReturn([$attributeData]);
- $entityAttributeMock = $this->getMockBuilder(Attribute::class)
- ->setMethods(['setData', 'load', 'toArray'])
- ->disableOriginalConstructor()
- ->getMock();
- $entityAttributeMock->method('setData')
- ->willReturnSelf();
- $entityAttributeMock->method('load')
- ->willReturnSelf();
- $entityAttributeMock->method('toArray')
- ->willReturn($attributeData);
- $factoryCalls = [
- [
- Collection::class,
- [],
- $attributeCollectionMock
- ],
- [
- Attribute::class,
- [],
- $entityAttributeMock
- ],
- ];
- $this->cacheStateMock
- ->expects($this->atLeastOnce())
- ->method('isEnabled')
- ->with(Cache::TYPE_IDENTIFIER)
- ->willReturn($cacheEnabled);
- $entityTypeData = [
- 'entity_type_id' => 'entity_type_id',
- 'entity_type_code' => 'entity_type_code'
- ];
- $collectionStub = new DataObject([$entityTypeData]);
- $this->collectionFactoryMock
- ->expects($this->any())
- ->method('create')
- ->willReturn($collectionStub);
- $entityType = $this->getMockBuilder(Type::class)
- ->setMethods(['getEntity', 'setData', 'getData', 'getEntityTypeCode', 'getId'])
- ->disableOriginalConstructor()
- ->getMock();
- $entityType->method('getEntityTypeCode')
- ->willReturn('entity_type_code');
- $entityType->method('getId')
- ->willReturn(101);
- $this->typeFactoryMock
- ->expects($this->any())
- ->method('create')
- ->willReturn($entityType);
- $this->universalFactoryMock
- ->expects($this->atLeastOnce())
- ->method('create')
- ->will($this->returnValueMap($factoryCalls));
- $this->assertEquals(['attribute_code_1' => $entityAttributeMock], $this->config->getAttributes($entityType));
- }
- public function testClear()
- {
- $this->cacheMock->expects($this->once())
- ->method('clean')
- ->with(
- $this->equalTo(
- [
- Cache::CACHE_TAG,
- Attribute::CACHE_TAG,
- ]
- )
- );
- $this->config->clear();
- }
- public function testGetEntityTypeInstanceOfTypePassed()
- {
- $this->assertEquals(
- $this->typeMock,
- $this->config->getEntityType($this->typeMock)
- );
- }
- public function testGetEntityTypeCacheExists()
- {
- $entityTypeCode = 'catalog_product';
- $data = [
- $entityTypeCode => [
- 'entity_type_id' => 1
- ]
- ];
- $serializedData = 'serialized data';
- $this->cacheStateMock->expects($this->once())
- ->method('isEnabled')
- ->with(Cache::TYPE_IDENTIFIER)
- ->willReturn(true);
- $this->cacheMock->expects($this->once())
- ->method('load')
- ->with(Config::ENTITIES_CACHE_ID)
- ->willReturn($serializedData);
- $this->serializerMock->expects($this->once())
- ->method('unserialize')
- ->with($serializedData)
- ->willReturn($data);
- $this->typeMock->expects($this->exactly(2))
- ->method('getId')
- ->willReturn($data[$entityTypeCode]['entity_type_id']);
- $this->typeMock->expects($this->once())
- ->method('getEntityTypeCode')
- ->willReturn($entityTypeCode);
- $this->typeFactoryMock->expects($this->once())
- ->method('create')
- ->with(['data' => $data[$entityTypeCode]])
- ->willReturn($this->typeMock);
- $this->assertInstanceOf(
- Type::class,
- $this->config->getEntityType($entityTypeCode)
- );
- }
- public function testGetEntityTypeCacheDoesNotExist()
- {
- $entityTypeCode = 'catalog_product';
- $collectionData = [
- [
- 'entity_type_id' => 1,
- 'entity_type_code' => $entityTypeCode
- ]
- ];
- $data = [
- $entityTypeCode => [
- 'entity_type_id' => 1,
- 'entity_type_code' => $entityTypeCode,
- 'attribute_model' => Attribute::class
- ]
- ];
- $serializedData = 'serialized data';
- $this->cacheStateMock->expects($this->once())
- ->method('isEnabled')
- ->with(Cache::TYPE_IDENTIFIER)
- ->willReturn(true);
- $this->cacheMock->expects($this->once())
- ->method('load')
- ->with(Config::ENTITIES_CACHE_ID)
- ->willReturn(false);
- $this->serializerMock->expects($this->never())
- ->method('unserialize');
- $attributeCollectionMock = $this->createMock(Collection::class);
- $this->collectionFactoryMock->expects($this->once())
- ->method('create')
- ->willReturn($attributeCollectionMock);
- $attributeCollectionMock->expects($this->once())
- ->method('getData')
- ->willReturn($collectionData);
- $this->serializerMock->expects($this->once())
- ->method('serialize')
- ->with($data)
- ->willReturn($serializedData);
- $this->cacheMock->expects($this->once())
- ->method('save')
- ->with(
- $serializedData,
- Config::ENTITIES_CACHE_ID,
- [
- Cache::CACHE_TAG,
- Attribute::CACHE_TAG
- ]
- );
- $this->typeMock->expects($this->exactly(2))
- ->method('getId')
- ->willReturn($data[$entityTypeCode]['entity_type_id']);
- $this->typeMock->expects($this->once())
- ->method('getEntityTypeCode')
- ->willReturn($entityTypeCode);
- $this->typeFactoryMock->expects($this->once())
- ->method('create')
- ->with(['data' => $data[$entityTypeCode]])
- ->willReturn($this->typeMock);
- $this->assertInstanceOf(
- Type::class,
- $this->config->getEntityType($entityTypeCode)
- );
- }
- }
|