Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractStorageTest.php
Go to the documentation of this file.
1 <?php
7 
8 class AbstractStorageTest extends \PHPUnit\Framework\TestCase
9 {
13  protected $urlRewriteFactory;
14 
18  protected $dataObjectHelper;
19 
23  protected $storage;
24 
25  protected function setUp()
26  {
27  $this->urlRewriteFactory = $this->getMockBuilder(\Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory::class)
28  ->setMethods(['create'])
29  ->disableOriginalConstructor()->getMock();
30  $this->dataObjectHelper = $this->getMockBuilder(\Magento\Framework\Api\DataObjectHelper::class)
31  ->disableOriginalConstructor()->getMock();
32 
33  $this->storage = $this->getMockForAbstractClass(
34  \Magento\UrlRewrite\Model\Storage\AbstractStorage::class,
35  [$this->urlRewriteFactory, $this->dataObjectHelper],
36  '',
37  true,
38  true,
39  true
40  );
41  }
42 
43  public function testFindAllByData()
44  {
45  $data = [['field1' => 'value1']];
46  $rows = [['row1'], ['row2']];
47  $urlRewrites = [['urlRewrite1'], ['urlRewrite2']];
48 
49  $this->storage->expects($this->once())
50  ->method('doFindAllByData')
51  ->with($data)
52  ->will($this->returnValue($rows));
53 
54  $this->dataObjectHelper->expects($this->at(0))
55  ->method('populateWithArray')
56  ->with($urlRewrites[0], $rows[0], \Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
57  ->will($this->returnSelf());
58 
59  $this->urlRewriteFactory->expects($this->at(0))
60  ->method('create')
61  ->will($this->returnValue($urlRewrites[0]));
62 
63  $this->dataObjectHelper->expects($this->at(1))
64  ->method('populateWithArray')
65  ->with($urlRewrites[1], $rows[1], \Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
66  ->will($this->returnSelf());
67 
68  $this->urlRewriteFactory->expects($this->at(1))
69  ->method('create')
70  ->will($this->returnValue($urlRewrites[1]));
71 
72  $this->assertEquals($urlRewrites, $this->storage->findAllByData($data));
73  }
74 
75  public function testFindOneByDataIfNotFound()
76  {
77  $data = [['field1' => 'value1']];
78 
79  $this->storage->expects($this->once())
80  ->method('doFindOneByData')
81  ->with($data)
82  ->will($this->returnValue(null));
83 
84  $this->assertNull($this->storage->findOneByData($data));
85  }
86 
87  public function testFindOneByDataIfFound()
88  {
89  $data = [['field1' => 'value1']];
90  $row = ['row1'];
91  $urlRewrite = ['urlRewrite1'];
92 
93  $this->storage->expects($this->once())
94  ->method('doFindOneByData')
95  ->with($data)
96  ->will($this->returnValue($row));
97 
98  $this->dataObjectHelper->expects($this->once())
99  ->method('populateWithArray')
100  ->with($urlRewrite, $row, \Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class)
101  ->will($this->returnSelf());
102 
103  $this->urlRewriteFactory->expects($this->any())
104  ->method('create')
105  ->will($this->returnValue($urlRewrite));
106 
107  $this->assertEquals($urlRewrite, $this->storage->findOneByData($data));
108  }
109 
110  public function testReplaceIfUrlsAreEmpty()
111  {
112  $this->storage->expects($this->never())->method('doReplace');
113 
114  $this->storage->replace([]);
115  }
116 
122  {
123  $this->storage
124  ->expects($this->once())
125  ->method('doReplace')
126  ->will($this->throwException(
127  new \Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException(__('Custom storage message'))
128  ));
129 
130  $this->storage->replace([['UrlRewrite1']]);
131  }
132 
138  {
139  $this->storage
140  ->expects($this->once())
141  ->method('doReplace')
142  ->will($this->throwException(
143  new \Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException()
144  ));
145 
146  $this->storage->replace([['UrlRewrite1']]);
147  }
148 
149  public function testReplace()
150  {
151  $urls = [['UrlRewrite1'], ['UrlRewrite2']];
152 
153  $this->storage
154  ->expects($this->once())
155  ->method('doReplace')
156  ->with($urls);
157 
158  $this->storage->replace($urls);
159  }
160 }
__()
Definition: __.php:13