Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DataTest.php
Go to the documentation of this file.
1 <?php
8 
9 class DataTest extends \PHPUnit\Framework\TestCase
10 {
14  private $readerMock;
15 
19  private $cacheMock;
20 
24  private $serializerMock;
25 
26  protected function setUp()
27  {
28  $this->readerMock = $this->createMock(\Magento\Framework\Config\ReaderInterface::class);
29  $this->cacheMock = $this->createMock(\Magento\Framework\Config\CacheInterface::class);
30  $this->serializerMock = $this->createMock(\Magento\Framework\Serialize\SerializerInterface::class);
31  }
32 
33  public function testGetConfigNotCached()
34  {
35  $data = ['a' => 'b'];
36  $cacheId = 'test';
37  $this->cacheMock->expects($this->once())
38  ->method('load')
39  ->willReturn(false);
40  $this->readerMock->expects($this->once())
41  ->method('read')
42  ->willReturn($data);
43  $this->serializerMock->expects($this->once())
44  ->method('serialize')
45  ->with($data);
46  $config = new \Magento\Framework\Config\Data(
47  $this->readerMock,
48  $this->cacheMock,
49  $cacheId,
50  $this->serializerMock
51  );
52  $this->assertEquals($data, $config->get());
53  $this->assertEquals('b', $config->get('a'));
54  $this->assertEquals(null, $config->get('a/b'));
55  $this->assertEquals(33, $config->get('a/b', 33));
56  }
57 
58  public function testGetConfigCached()
59  {
60  $data = ['a' => 'b'];
61  $serializedData = '{"a":"b"}';
62  $cacheId = 'test';
63  $this->cacheMock->expects($this->once())
64  ->method('load')
65  ->willReturn($serializedData);
66  $this->readerMock->expects($this->never())
67  ->method('read');
68  $this->serializerMock->expects($this->once())
69  ->method('unserialize')
70  ->with($serializedData)
71  ->willReturn($data);
72  $config = new \Magento\Framework\Config\Data(
73  $this->readerMock,
74  $this->cacheMock,
75  $cacheId,
76  $this->serializerMock
77  );
78  $this->assertEquals($data, $config->get());
79  $this->assertEquals('b', $config->get('a'));
80  }
81 
82  public function testReset()
83  {
84  $serializedData = '';
85  $cacheId = 'test';
86  $this->cacheMock->expects($this->once())
87  ->method('load')
88  ->willReturn($serializedData);
89  $this->serializerMock->expects($this->once())
90  ->method('unserialize')
91  ->with($serializedData)
92  ->willReturn([]);
93  $this->cacheMock->expects($this->once())
94  ->method('remove')
95  ->with($cacheId);
96  $config = new \Magento\Framework\Config\Data(
97  $this->readerMock,
98  $this->cacheMock,
99  $cacheId,
100  $this->serializerMock
101  );
102  $config->reset();
103  }
104 }
$config
Definition: fraud_order.php:17