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