1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Records\Tests; |
4
|
|
|
|
5
|
|
|
use Mockery as m; |
6
|
|
|
use Nip\Database\Connections\Connection; |
7
|
|
|
use Nip\Records\Collections\Collection; |
8
|
|
|
use Nip\Records\RecordManager as Records; |
9
|
|
|
use Nip\Request; |
10
|
|
|
use Nip\Records\Tests\AbstractTest; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class RecordsTest |
14
|
|
|
* @package Nip\Records\Tests |
15
|
|
|
*/ |
16
|
|
|
class RecordsTest extends AbstractTest |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Records |
21
|
|
|
*/ |
22
|
|
|
protected $_object; |
23
|
|
|
|
24
|
|
|
public function testSetModel() |
25
|
|
|
{ |
26
|
|
|
$this->_object->setModel('Row'); |
27
|
|
|
self::assertEquals($this->_object->getModel(), 'Row'); |
28
|
|
|
|
29
|
|
|
$this->_object->setModel('Row2'); |
30
|
|
|
self::assertEquals($this->_object->getModel(), 'Row2'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testGetFullNameTable() |
34
|
|
|
{ |
35
|
|
|
self::assertEquals('pages', $this->_object->getFullNameTable()); |
36
|
|
|
|
37
|
|
|
$this->_object->getDB()->setDatabase('database_name'); |
38
|
|
|
self::assertEquals('database_name.pages', $this->_object->getFullNameTable()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// tests |
42
|
|
|
|
43
|
|
|
public function testGenerateModelClass() |
44
|
|
|
{ |
45
|
|
|
self::assertEquals($this->_object->generateModelClass('Notifications\Table'), 'Notifications\Row'); |
46
|
|
|
self::assertEquals($this->_object->generateModelClass('Notifications_Tables'), 'Notifications_Table'); |
47
|
|
|
self::assertEquals($this->_object->generateModelClass('Notifications'), 'Notification'); |
48
|
|
|
self::assertEquals($this->_object->generateModelClass('Persons'), 'Person'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
public function providerGetController() |
55
|
|
|
{ |
56
|
|
|
return [ |
57
|
|
|
["notifications-tables", "Notifications_Tables"], |
58
|
|
|
["notifications-tables", "Notifications\\Tables\\Tables"], |
59
|
|
|
["notifications-tables", "App\\Models\\Notifications\\Tables\\Tables"], |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @dataProvider providerGetController |
65
|
|
|
* @param $controller |
66
|
|
|
* @param $class |
67
|
|
|
*/ |
68
|
|
|
public function testGetController($controller, $class) |
69
|
|
|
{ |
70
|
|
|
/** @var Records $records */ |
71
|
|
|
$records = new Records(); |
72
|
|
|
$records->setClassName($class); |
73
|
|
|
|
74
|
|
|
self::assertEquals($controller, $records->getController()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// public function testInitRelationsFromArrayBelongsToSimple() |
|
|
|
|
78
|
|
|
// { |
79
|
|
|
/** @var Records $users */ |
80
|
|
|
// $users = m::namedMock('Users', Records::class)->shouldDeferMissing() |
|
|
|
|
81
|
|
|
// ->shouldReceive('instance')->andReturnSelf() |
82
|
|
|
// ->getMock(); |
83
|
|
|
|
84
|
|
|
// $users->setPrimaryFK('id_user'); |
|
|
|
|
85
|
|
|
// |
86
|
|
|
// m::namedMock('User', Records::class); |
87
|
|
|
// m::namedMock('Articles', Records::class); |
88
|
|
|
|
89
|
|
|
// $this->_object->setPrimaryFK('id_object'); |
|
|
|
|
90
|
|
|
// |
91
|
|
|
// $this->_object->initRelationsFromArray('belongsTo', ['User']); |
92
|
|
|
// $this->_testInitRelationsFromArrayBelongsToUser('User'); |
93
|
|
|
// |
94
|
|
|
// $this->_object->initRelationsFromArray('belongsTo', [ |
95
|
|
|
// 'UserName' => ['with' => $users], |
96
|
|
|
// ]); |
97
|
|
|
// $this->_testInitRelationsFromArrayBelongsToUser('UserName'); |
98
|
|
|
// |
99
|
|
|
// self::assertSame($users, $this->_object->getRelation('User')->getWith()); |
100
|
|
|
// } |
101
|
|
|
|
102
|
|
|
public function testNewCollection() |
103
|
|
|
{ |
104
|
|
|
$collection = $this->_object->newCollection(); |
105
|
|
|
self::assertInstanceOf('Nip\Records\Collections\Collection', $collection); |
106
|
|
|
self::assertSame($this->_object, $collection->getManager()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testRequestFilters() |
110
|
|
|
{ |
111
|
|
|
$request = new Request(); |
112
|
|
|
$params = [ |
113
|
|
|
'title' => 'Test title', |
114
|
|
|
'name' => 'Test name', |
115
|
|
|
]; |
116
|
|
|
$request->query->add($params); |
117
|
|
|
|
118
|
|
|
$this->_object->getFilterManager()->addFilter( |
119
|
|
|
$this->_object->getFilterManager()->newFilter('Column\BasicFilter') |
|
|
|
|
120
|
|
|
->setField('title') |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
$this->_object->getFilterManager()->addFilter( |
124
|
|
|
$this->_object->getFilterManager()->newFilter('Column\BasicFilter') |
|
|
|
|
125
|
|
|
->setField('name') |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
$filtersArray = $this->_object->requestFilters($request); |
129
|
|
|
self::assertSame($filtersArray, $params); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
public function providerGetPrimaryFK() |
136
|
|
|
{ |
137
|
|
|
return [ |
138
|
|
|
["id_user", "Users"], |
139
|
|
|
["id_race_entry", "RaceEntries"], |
140
|
|
|
["id_notifications_table", "Notifications_Tables"], |
141
|
|
|
["id_notifications_table", "Notifications\\Tables\\Tables"], |
142
|
|
|
["id_notifications_table", "App\\Models\\Notifications\\Tables\\Tables"], |
143
|
|
|
]; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @dataProvider providerGetPrimaryFK |
148
|
|
|
* @param $primaryFK |
149
|
|
|
* @param $class |
150
|
|
|
*/ |
151
|
|
|
public function testGetPrimaryFK($primaryFK, $class) |
152
|
|
|
{ |
153
|
|
|
/** @var Records $records */ |
154
|
|
|
// $records = m::namedMock($class, 'Records')->shouldDeferMissing() |
|
|
|
|
155
|
|
|
// ->shouldReceive('instance')->andReturnSelf() |
156
|
|
|
// ->shouldReceive('getPrimaryKey')->andReturn('id') |
157
|
|
|
// ->getMock(); |
158
|
|
|
$records = new Records(); |
159
|
|
|
$records->setClassName($class); |
160
|
|
|
$records->setPrimaryKey('id'); |
161
|
|
|
|
162
|
|
|
self::assertEquals($primaryFK, $records->getPrimaryFK()); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function testGetPrimaryKey() |
166
|
|
|
{ |
167
|
|
|
$records = new Records(); |
168
|
|
|
$tableStructure = unserialize(file_get_contents(TEST_FIXTURE_PATH . '/database_structure/users.serialize')); |
169
|
|
|
$records->setTableStructure($tableStructure); |
170
|
|
|
$records->setPrimaryKey('id'); |
171
|
|
|
|
172
|
|
|
self::assertEquals('id', $records->getPrimaryKey()); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function testGetCollectionClass() |
176
|
|
|
{ |
177
|
|
|
self::assertEquals(Collection::class, $this->_object->getCollectionClass()); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
View Code Duplication |
protected function setUp() |
|
|
|
|
181
|
|
|
{ |
182
|
|
|
parent::setUp(); |
183
|
|
|
|
184
|
|
|
$wrapper = new Connection(null); |
|
|
|
|
185
|
|
|
|
186
|
|
|
$this->_object = m::mock(Records::class)->shouldDeferMissing() |
187
|
|
|
->shouldReceive('getRequest')->andReturn(Request::create('/')) |
188
|
|
|
->getMock(); |
189
|
|
|
|
190
|
|
|
$this->_object->setDB($wrapper); |
191
|
|
|
$this->_object->setTable('pages'); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.