Skip to content

Commit 0a083be

Browse files
committed
Fixing syntax issues in \Magento\Store\Test\Unit\Model\ResourceModel\WebsiteTest
New unit test \Magento\Store\Test\Unit\Model\ResourceModel\StoreTest
1 parent 55a5748 commit 0a083be

File tree

2 files changed

+222
-21
lines changed

2 files changed

+222
-21
lines changed
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Store\Test\Unit\Model\ResourceModel;
8+
9+
10+
use Magento\Framework\App\ResourceConnection;
11+
use Magento\Framework\DB\Select;
12+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
13+
use Magento\Store\Model\ResourceModel\Store;
14+
use Magento\Framework\DB\Adapter\AdapterInterface;
15+
16+
class StoreTest extends \PHPUnit\Framework\TestCase
17+
{
18+
/** @var Store */
19+
protected $model;
20+
21+
/**
22+
* @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
23+
*/
24+
protected $resourceMock;
25+
26+
/** @var Select | \PHPUnit_Framework_MockObject_MockObject */
27+
protected $select;
28+
29+
/**
30+
* @var AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
31+
*/
32+
protected $connectionMock;
33+
34+
public function setUp()
35+
{
36+
$objectManagerHelper = new ObjectManager($this);
37+
$this->select = $this->createMock(Select::class);
38+
$this->resourceMock = $this->createPartialMock(
39+
ResourceConnection::class,
40+
[
41+
'getConnection',
42+
'getTableName'
43+
]
44+
);
45+
$this->connectionMock = $this->createPartialMock(
46+
\Magento\Framework\DB\Adapter\Pdo\Mysql::class,
47+
[
48+
'isTableExists',
49+
'select',
50+
'fetchAll',
51+
'fetchOne',
52+
'from',
53+
'getCheckSql',
54+
'where',
55+
'quoteIdentifier',
56+
'quote'
57+
]
58+
);
59+
60+
$contextMock = $this->createMock(\Magento\Framework\Model\ResourceModel\Db\Context::class);
61+
$contextMock->expects($this->once())->method('getResources')->willReturn($this->resourceMock);
62+
$configCacheTypeMock = $this->createMock('\Magento\Framework\App\Cache\Type\Config');
63+
$this->model = $objectManagerHelper->getObject(
64+
Store::class,
65+
[
66+
'context' => $contextMock,
67+
'configCacheType' => $configCacheTypeMock
68+
]
69+
);
70+
}
71+
72+
public function testCountAll($countAdmin = false)
73+
{
74+
$mainTable = 'store';
75+
$tableIdentifier = 'code';
76+
$tableIdentifierValue = 'admin';
77+
$count = 1;
78+
79+
$this->resourceMock->expects($this->once())
80+
->method('getConnection')
81+
->willReturn($this->connectionMock);
82+
83+
$this->connectionMock->expects($this->once())
84+
->method('select')
85+
->willReturn($this->select);
86+
87+
$this->resourceMock->expects($this->once())
88+
->method('getTableName')
89+
->willReturn($mainTable);
90+
91+
$this->select->expects($this->once())
92+
->method('from')
93+
->with($mainTable, 'COUNT(*)')
94+
->willReturnSelf();
95+
96+
$this->connectionMock->expects($this->any())
97+
->method('quoteIdentifier')
98+
->with($tableIdentifier)
99+
->willReturn($tableIdentifier);
100+
101+
$this->connectionMock->expects($this->once())
102+
->method('quote')
103+
->with($tableIdentifierValue)
104+
->willReturn($tableIdentifierValue);
105+
106+
$this->select->expects($this->any())
107+
->method('where')
108+
->with(sprintf('%s <> %s', $tableIdentifier, $tableIdentifierValue))
109+
->willReturnSelf();
110+
111+
$this->connectionMock->expects($this->once())
112+
->method('fetchOne')
113+
->with($this->select)
114+
->willReturn($count);
115+
116+
$this->assertEquals($count, $this->model->countAll($countAdmin));
117+
}
118+
119+
public function testReadAllStores()
120+
{
121+
$mainTable = 'store';
122+
$data = [
123+
["store_id" => "0", "code" => "admin", "website_id" => 0, "name" => "Admin"],
124+
["store_id" => "1", "code" => "default", "website_id" => 1, "name" => "Default Store View"]
125+
];
126+
127+
$this->resourceMock->expects($this->atLeastOnce())
128+
->method('getConnection')
129+
->willReturn($this->connectionMock);
130+
131+
$this->resourceMock->expects($this->atLeastOnce())
132+
->method('getTableName')
133+
->willReturn($mainTable);
134+
135+
$this->connectionMock->expects($this->once())
136+
->method('isTableExists')
137+
->with($mainTable)
138+
->willReturn(true);
139+
140+
$this->connectionMock->expects($this->once())
141+
->method('select')
142+
->willReturn($this->select);
143+
144+
$this->select->expects($this->once())
145+
->method('from')
146+
->with($mainTable)
147+
->willReturnSelf();
148+
149+
$this->connectionMock->expects($this->once())
150+
->method('fetchAll')
151+
->with($this->select)
152+
->willReturn($data);
153+
154+
$this->assertEquals($data, $this->model->readAllStores());
155+
}
156+
157+
public function testReadAllStoresNoDbTable()
158+
{
159+
$mainTable = 'no_store_table';
160+
$data = [];
161+
162+
$this->resourceMock->expects($this->once())
163+
->method('getConnection')
164+
->willReturn($this->connectionMock);
165+
166+
$this->resourceMock->expects($this->once())
167+
->method('getTableName')
168+
->willReturn($mainTable);
169+
170+
$this->connectionMock->expects($this->once())
171+
->method('isTableExists')
172+
->with($mainTable)
173+
->willReturn(false);
174+
175+
$this->connectionMock->expects($this->never())
176+
->method('select')
177+
->willReturn($this->select);
178+
179+
$this->select->expects($this->never())
180+
->method('from')
181+
->with($mainTable)
182+
->willReturnSelf();
183+
184+
$this->connectionMock->expects($this->never())
185+
->method('fetchAll')
186+
->with($this->select)
187+
->willReturn($data);
188+
189+
$this->assertEquals($data, $this->model->readAllStores());
190+
}
191+
}

app/code/Magento/Store/Test/Unit/Model/ResourceModel/WebsiteTest.php

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php
2-
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
36

47
namespace Magento\Store\Test\Unit\Model\ResourceModel;
58

@@ -11,15 +14,15 @@
1114
class WebsiteTest extends \PHPUnit\Framework\TestCase
1215
{
1316
/** @var Website */
14-
private $model;
17+
protected $model;
1518

1619
/**
1720
* @var \Magento\Framework\App\ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
1821
*/
1922
protected $resourceMock;
2023

2124
/** @var Select | \PHPUnit_Framework_MockObject_MockObject */
22-
private $select;
25+
protected $select;
2326

2427
/**
2528
* @var \Magento\Framework\DB\Adapter\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
@@ -30,27 +33,34 @@ public function setUp()
3033
{
3134
$objectManagerHelper = new ObjectManager($this);
3235
$this->select = $this->createMock(\Magento\Framework\DB\Select::class);
33-
$this->resourceMock = $this->createPartialMock(ResourceConnection::class, [
34-
'getConnection',
35-
'getMainTable',
36-
'getTable',
37-
'getTableName'
38-
]);
39-
$this->connectionMock = $this->createPartialMock(\Magento\Framework\DB\Adapter\Pdo\Mysql::class, [
40-
'isTableExists',
41-
'select',
42-
'fetchAll',
43-
'fetchOne',
44-
'from',
45-
'getCheckSql',
46-
'joinLeft',
47-
'where'
48-
]);
36+
$this->resourceMock = $this->createPartialMock(
37+
ResourceConnection::class,
38+
[
39+
'getConnection',
40+
'getTableName'
41+
]
42+
);
43+
$this->connectionMock = $this->createPartialMock(
44+
\Magento\Framework\DB\Adapter\Pdo\Mysql::class,
45+
[
46+
'isTableExists',
47+
'select',
48+
'fetchAll',
49+
'fetchOne',
50+
'from',
51+
'getCheckSql',
52+
'joinLeft',
53+
'where'
54+
]
55+
);
4956
$contextMock = $this->createMock(\Magento\Framework\Model\ResourceModel\Db\Context::class);
5057
$contextMock->expects($this->once())->method('getResources')->willReturn($this->resourceMock);
51-
$this->model = $objectManagerHelper->getObject(Website::class, [
58+
$this->model = $objectManagerHelper->getObject(
59+
Website::class,
60+
[
5261
'context' => $contextMock
53-
]);
62+
]
63+
);
5464
}
5565

5666
public function testReadAllWebsites()

0 commit comments

Comments
 (0)