|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2015 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Framework\Api\Config; |
| 7 | + |
| 8 | +class MetadataConfigTest extends \PHPUnit_Framework_TestCase |
| 9 | +{ |
| 10 | + /** |
| 11 | + * @var \Magento\Framework\Api\Config\MetadataConfig |
| 12 | + */ |
| 13 | + protected $metadataConfig; |
| 14 | + |
| 15 | + /** |
| 16 | + * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Api\Config\Reader |
| 17 | + */ |
| 18 | + protected $serviceConfigReaderMock; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Api\AttributeMetadataBuilderInterface |
| 22 | + */ |
| 23 | + protected $attributeMetadataBuilderMock; |
| 24 | + |
| 25 | + /** |
| 26 | + * Prepare parameters |
| 27 | + */ |
| 28 | + public function setUp() |
| 29 | + { |
| 30 | + $this->serviceConfigReaderMock = $this->getMockBuilder('\Magento\Framework\Api\Config\Reader') |
| 31 | + ->disableOriginalConstructor() |
| 32 | + ->getMock(); |
| 33 | + $this->attributeMetadataBuilderMock = $this->getMockBuilder( |
| 34 | + '\Magento\Framework\Api\AttributeMetadataBuilderInterface' |
| 35 | + )->disableOriginalConstructor()->getMock(); |
| 36 | + |
| 37 | + $this->metadataConfig = new \Magento\Framework\Api\Config\MetadataConfig( |
| 38 | + $this->serviceConfigReaderMock, |
| 39 | + $this->attributeMetadataBuilderMock |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Test caching |
| 45 | + */ |
| 46 | + public function testCaching() |
| 47 | + { |
| 48 | + $dataObjectClassName = '\Magento\Customer\Model\Data\Address'; |
| 49 | + $attributeCode = 'street'; |
| 50 | + $allAttributes = [ |
| 51 | + $dataObjectClassName => [ |
| 52 | + $attributeCode => 'value', |
| 53 | + ] |
| 54 | + ]; |
| 55 | + $this->serviceConfigReaderMock->expects($this->once()) |
| 56 | + ->method('read') |
| 57 | + ->willReturn($allAttributes); |
| 58 | + |
| 59 | + $attributeMock = $this->getMock('\Magento\Framework\Api\MetadataObjectInterface'); |
| 60 | + $this->attributeMetadataBuilderMock->expects($this->exactly(2)) |
| 61 | + ->method('setAttributeCode') |
| 62 | + ->with($attributeCode); |
| 63 | + $this->attributeMetadataBuilderMock->expects($this->exactly(2)) |
| 64 | + ->method('create') |
| 65 | + ->willReturn($attributeMock); |
| 66 | + |
| 67 | + $attributes = $this->metadataConfig->getCustomAttributesMetadata($dataObjectClassName); |
| 68 | + $this->assertEquals($attributeMock, $attributes[$attributeCode]); |
| 69 | + $attributes = $this->metadataConfig->getCustomAttributesMetadata($dataObjectClassName); |
| 70 | + $this->assertEquals($attributeMock, $attributes[$attributeCode]); |
| 71 | + } |
| 72 | +} |
0 commit comments