Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConfigTest.php
Go to the documentation of this file.
1 <?php
7 
12 
13 class ConfigTest extends \PHPUnit\Framework\TestCase
14 {
18  private $config;
19 
23  private $webapiCacheMock;
24 
28  private $configReaderMock;
29 
33  private $serializerMock;
34 
35  protected function setUp()
36  {
37  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
38 
39  $this->webapiCacheMock = $this->createMock(\Magento\Webapi\Model\Cache\Type\Webapi::class);
40  $this->configReaderMock = $this->createMock(\Magento\Webapi\Model\Config\Reader::class);
41  $this->serializerMock = $this->createMock(SerializerInterface::class);
42 
43  $this->config = $objectManager->getObject(
44  Config::class,
45  [
46  'cache' => $this->webapiCacheMock,
47  'configReader' => $this->configReaderMock,
48  'serializer' => $this->serializerMock
49  ]
50  );
51  }
52 
53  public function testGetServices()
54  {
55  $data = ['foo' => 'bar'];
56  $serializedData = 'serialized data';
57  $this->webapiCacheMock->expects($this->once())
58  ->method('load')
59  ->with(Config::CACHE_ID)
60  ->willReturn($serializedData);
61  $this->serializerMock->expects($this->once())
62  ->method('unserialize')
63  ->with($serializedData)
64  ->willReturn($data);
65  $this->config->getServices();
66  $this->assertEquals($data, $this->config->getServices());
67  }
68 
69  public function testGetServicesNoCache()
70  {
71  $data = ['foo' => 'bar'];
72  $serializedData = 'serialized data';
73  $this->webapiCacheMock->expects($this->once())
74  ->method('load')
75  ->with(Config::CACHE_ID)
76  ->willReturn(false);
77  $this->serializerMock->expects($this->never())
78  ->method('unserialize');
79  $this->configReaderMock->expects($this->once())
80  ->method('read')
81  ->willReturn($data);
82  $this->serializerMock->expects($this->once())
83  ->method('serialize')
84  ->with($data)
85  ->willReturn($serializedData);
86  $this->webapiCacheMock->expects($this->once())
87  ->method('save')
88  ->with(
89  $serializedData,
91  );
92 
93  $this->config->getServices();
94  $this->assertEquals($data, $this->config->getServices());
95  }
96 }
$objectManager
Definition: bootstrap.php:17