Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UrlRewriteCollectionTest.php
Go to the documentation of this file.
1 <?php
7 
9 
10 class UrlRewriteCollectionTest extends \PHPUnit\Framework\TestCase
11 {
15  protected $storeManager;
16 
20  protected $resource;
21 
25  protected $select;
26 
30  protected $connectionMock;
31 
35  protected $connection;
36 
40  protected $collection;
41 
42  protected function setUp()
43  {
44  $this->storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
45  $this->select = $this->createPartialMock(\Magento\Framework\DB\Select::class, ['from', 'where']);
46  $this->connectionMock = $this->createPartialMock(
47  \Magento\Framework\DB\Adapter\Pdo\Mysql::class,
48  ['select', 'prepareSqlCondition', 'quoteIdentifier']
49  );
50  $this->resource = $this->getMockForAbstractClass(
51  \Magento\Framework\Model\ResourceModel\Db\AbstractDb::class,
52  [],
53  '',
54  false,
55  true,
56  true,
57  ['getConnection', '__wakeup', 'getMainTable', 'getTable']
58  );
59 
60  $this->select->expects($this->any())
61  ->method('where')
62  ->will($this->returnSelf());
63  $this->connectionMock->expects($this->any())
64  ->method('select')
65  ->will($this->returnValue($this->select));
66  $this->connectionMock->expects($this->any())
67  ->method('quoteIdentifier')
68  ->will($this->returnArgument(0));
69  $this->resource->expects($this->any())
70  ->method('getConnection')
71  ->will($this->returnValue($this->connectionMock));
72  $this->resource->expects($this->any())
73  ->method('getMainTable')
74  ->will($this->returnValue('test_main_table'));
75  $this->resource->expects($this->any())
76  ->method('getTable')
77  ->with('test_main_table')
78  ->will($this->returnValue('test_main_table'));
79 
80  $this->collection = (new ObjectManager($this))->getObject(
81  \Magento\UrlRewrite\Model\ResourceModel\UrlRewriteCollection::class,
82  [
83  'storeManager' => $this->storeManager,
84  'resource' => $this->resource,
85  ]
86  );
87  }
88 
96  public function testAddStoreFilterIfStoreIsArray($storeId, $withAdmin, $condition)
97  {
98  $this->connectionMock->expects($this->once())
99  ->method('prepareSqlCondition')
100  ->with('store_id', ['in' => $condition]);
101 
102  $this->collection->addStoreFilter($storeId, $withAdmin);
103  }
104 
109  {
110  return [
111  [[112, 113], false, [112, 113]],
112  [[112, 113], true, [112, 113, 0]],
113  ];
114  }
115 
123  public function testAddStoreFilterIfStoreIsInt($storeId, $withAdmin, $condition)
124  {
125  $store = $this->createMock(\Magento\Store\Model\Store::class);
126  $store->expects($this->once())->method('getId')->will($this->returnValue($storeId));
127  $this->storeManager->expects($this->once())->method('getStore')->will($this->returnValue($store));
128 
129  $this->connectionMock->expects($this->once())
130  ->method('prepareSqlCondition')
131  ->with('store_id', ['in' => $condition]);
132 
133  $this->collection->addStoreFilter($storeId, $withAdmin);
134  }
135 
140  {
141  return [
142  [112, false, [112]],
143  [112, true, [112, 0]],
144  ];
145  }
146 }