Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AttributeMetadataCacheTest.php
Go to the documentation of this file.
1 <?php
7 
18 
19 class AttributeMetadataCacheTest extends \PHPUnit\Framework\TestCase
20 {
24  private $cacheMock;
25 
29  private $stateMock;
30 
34  private $attributeMetadataHydratorMock;
35 
39  private $serializerMock;
40 
44  private $attributeMetadataCache;
45 
46  protected function setUp()
47  {
48  $objectManager = new ObjectManager($this);
49  $this->cacheMock = $this->createMock(CacheInterface::class);
50  $this->stateMock = $this->createMock(StateInterface::class);
51  $this->serializerMock = $this->createMock(SerializerInterface::class);
52  $this->attributeMetadataHydratorMock = $this->createMock(AttributeMetadataHydrator::class);
53  $this->attributeMetadataCache = $objectManager->getObject(
54  AttributeMetadataCache::class,
55  [
56  'cache' => $this->cacheMock,
57  'state' => $this->stateMock,
58  'serializer' => $this->serializerMock,
59  'attributeMetadataHydrator' => $this->attributeMetadataHydratorMock
60  ]
61  );
62  }
63 
64  public function testLoadCacheDisabled()
65  {
66  $entityType = 'EntityType';
67  $suffix = 'none';
68  $this->stateMock->expects($this->once())
69  ->method('isEnabled')
70  ->with(Type::TYPE_IDENTIFIER)
71  ->willReturn(false);
72  $this->cacheMock->expects($this->never())
73  ->method('load');
74  $this->assertFalse($this->attributeMetadataCache->load($entityType, $suffix));
75  // Make sure isEnabled called once
76  $this->attributeMetadataCache->load($entityType, $suffix);
77  }
78 
79  public function testLoadNoCache()
80  {
81  $entityType = 'EntityType';
82  $suffix = 'none';
84  $this->stateMock->expects($this->once())
85  ->method('isEnabled')
86  ->with(Type::TYPE_IDENTIFIER)
87  ->willReturn(true);
88  $this->cacheMock->expects($this->once())
89  ->method('load')
90  ->with($cacheKey)
91  ->willReturn(false);
92  $this->assertFalse($this->attributeMetadataCache->load($entityType, $suffix));
93  }
94 
95  public function testLoad()
96  {
97  $entityType = 'EntityType';
98  $suffix = 'none';
100  $serializedString = 'serialized string';
101  $attributeMetadataOneData = [
102  'attribute_code' => 'attribute_code',
103  'frontend_input' => 'hidden',
104  ];
105  $attributesMetadataData = [$attributeMetadataOneData];
106  $this->stateMock->expects($this->once())
107  ->method('isEnabled')
108  ->with(Type::TYPE_IDENTIFIER)
109  ->willReturn(true);
110  $this->cacheMock->expects($this->once())
111  ->method('load')
112  ->with($cacheKey)
113  ->willReturn($serializedString);
114  $this->serializerMock->expects($this->once())
115  ->method('unserialize')
116  ->with($serializedString)
117  ->willReturn($attributesMetadataData);
119  $attributeMetadataMock = $this->createMock(AttributeMetadataInterface::class);
120  $this->attributeMetadataHydratorMock->expects($this->at(0))
121  ->method('hydrate')
122  ->with($attributeMetadataOneData)
123  ->willReturn($attributeMetadataMock);
124  $attributesMetadata = $this->attributeMetadataCache->load($entityType, $suffix);
125  $this->assertInternalType(
126  \PHPUnit\Framework\Constraint\IsType::TYPE_ARRAY,
127  $attributesMetadata
128  );
129  $this->assertArrayHasKey(
130  0,
131  $attributesMetadata
132  );
133  $this->assertInstanceOf(
134  AttributeMetadataInterface::class,
135  $attributesMetadata[0]
136  );
137  }
138 
139  public function testSaveCacheDisabled()
140  {
141  $entityType = 'EntityType';
142  $suffix = 'none';
143  $attributes = [['foo'], ['bar']];
144  $this->stateMock->expects($this->once())
145  ->method('isEnabled')
146  ->with(Type::TYPE_IDENTIFIER)
147  ->willReturn(false);
148  $this->attributeMetadataCache->save($entityType, $attributes, $suffix);
149  $this->assertEquals(
150  $attributes,
151  $this->attributeMetadataCache->load($entityType, $suffix)
152  );
153  }
154 
155  public function testSave()
156  {
157  $entityType = 'EntityType';
158  $suffix = 'none';
160  $serializedString = 'serialized string';
161  $attributeMetadataOneData = [
162  'attribute_code' => 'attribute_code',
163  'frontend_input' => 'hidden',
164  ];
165  $this->stateMock->expects($this->once())
166  ->method('isEnabled')
167  ->with(Type::TYPE_IDENTIFIER)
168  ->willReturn(true);
169 
171  $attributeMetadataMock = $this->createMock(AttributeMetadataInterface::class);
172  $attributesMetadata = [$attributeMetadataMock];
173  $this->attributeMetadataHydratorMock->expects($this->once())
174  ->method('extract')
175  ->with($attributeMetadataMock)
176  ->willReturn($attributeMetadataOneData);
177  $this->serializerMock->expects($this->once())
178  ->method('serialize')
179  ->with([$attributeMetadataOneData])
180  ->willReturn($serializedString);
181  $this->cacheMock->expects($this->once())
182  ->method('save')
183  ->with(
184  $serializedString,
185  $cacheKey,
186  [
190  ]
191  );
192  $this->attributeMetadataCache->save($entityType, $attributesMetadata, $suffix);
193  $this->assertSame(
194  $attributesMetadata,
195  $this->attributeMetadataCache->load($entityType, $suffix)
196  );
197  }
198 
199  public function testCleanCacheDisabled()
200  {
201  $this->stateMock->expects($this->once())
202  ->method('isEnabled')
203  ->with(Type::TYPE_IDENTIFIER)
204  ->willReturn(false);
205  $this->cacheMock->expects($this->never())
206  ->method('clean');
207  $this->attributeMetadataCache->clean();
208  }
209 
210  public function testClean()
211  {
212  $this->stateMock->expects($this->once())
213  ->method('isEnabled')
214  ->with(Type::TYPE_IDENTIFIER)
215  ->willReturn(true);
216  $this->cacheMock->expects($this->once())
217  ->method('clean');
218  $this->attributeMetadataCache->clean();
219  }
220 }
$suffix
Definition: name.phtml:27
$objectManager
Definition: bootstrap.php:17
$attributes
Definition: matrix.phtml:13