1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Records\Tests; |
4
|
|
|
|
5
|
|
|
use Mockery as m; |
6
|
|
|
use Nip\Database\Connections\Connection; |
7
|
|
|
use Nip\Http\Request; |
8
|
|
|
use Nip\Records\Collections\Collection; |
9
|
|
|
use Nip\Records\RecordManager as Records; |
10
|
|
|
use Nip_Helper_Url; |
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 testNewCollection() |
78
|
|
|
{ |
79
|
|
|
$collection = $this->_object->newCollection(); |
80
|
|
|
self::assertInstanceOf('Nip\Records\Collections\Collection', $collection); |
81
|
|
|
self::assertSame($this->_object, $collection->getManager()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testRequestFilters() |
85
|
|
|
{ |
86
|
|
|
$request = new Request(); |
87
|
|
|
$params = [ |
88
|
|
|
'title' => 'Test title', |
89
|
|
|
'name' => 'Test name', |
90
|
|
|
]; |
91
|
|
|
$request->query->add($params); |
92
|
|
|
|
93
|
|
|
$this->_object->getFilterManager()->addFilter( |
94
|
|
|
$this->_object->getFilterManager()->newFilter('Column\BasicFilter') |
95
|
|
|
->setField('title') |
|
|
|
|
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
$this->_object->getFilterManager()->addFilter( |
99
|
|
|
$this->_object->getFilterManager()->newFilter('Column\BasicFilter') |
100
|
|
|
->setField('name') |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
$filtersArray = $this->_object->requestFilters($request); |
|
|
|
|
104
|
|
|
self::assertSame($filtersArray, $params); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
public function providerGetPrimaryFK() |
111
|
|
|
{ |
112
|
|
|
return [ |
113
|
|
|
["id_user", "Users"], |
114
|
|
|
["id_race_entry", "RaceEntries"], |
115
|
|
|
["id_notifications_table", "Notifications_Tables"], |
116
|
|
|
["id_notifications_table", "Notifications\\Tables\\Tables"], |
117
|
|
|
["id_notifications_table", "App\\Models\\Notifications\\Tables\\Tables"], |
118
|
|
|
]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @dataProvider providerGetPrimaryFK |
123
|
|
|
* @param $primaryFK |
124
|
|
|
* @param $class |
125
|
|
|
*/ |
126
|
|
|
public function testGetPrimaryFK($primaryFK, $class) |
127
|
|
|
{ |
128
|
|
|
/** @var Records $records */ |
129
|
|
|
// $records = m::namedMock($class, 'Records')->shouldDeferMissing() |
130
|
|
|
// ->shouldReceive('instance')->andReturnSelf() |
131
|
|
|
// ->shouldReceive('getPrimaryKey')->andReturn('id') |
132
|
|
|
// ->getMock(); |
133
|
|
|
$records = new Records(); |
134
|
|
|
$records->setClassName($class); |
135
|
|
|
$records->setPrimaryKey('id'); |
136
|
|
|
|
137
|
|
|
self::assertEquals($primaryFK, $records->getPrimaryFK()); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function testGetPrimaryKey() |
141
|
|
|
{ |
142
|
|
|
$records = new Records(); |
143
|
|
|
$tableStructure = unserialize(file_get_contents(TEST_FIXTURE_PATH . '/database_structure/users.serialize')); |
144
|
|
|
$records->setTableStructure($tableStructure); |
145
|
|
|
$records->setPrimaryKey('id'); |
146
|
|
|
|
147
|
|
|
self::assertEquals('id', $records->getPrimaryKey()); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function testGetCollectionClass() |
151
|
|
|
{ |
152
|
|
|
self::assertEquals(Collection::class, $this->_object->getCollectionClass()); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function testGetUrlHelper() |
156
|
|
|
{ |
157
|
|
|
$records = new Records(); |
158
|
|
|
$urlHelper = $records->Url(); |
159
|
|
|
self::assertInstanceOf(Nip_Helper_Url::class, $urlHelper); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
protected function setUp() : void |
163
|
|
|
{ |
164
|
|
|
parent::setUp(); |
165
|
|
|
|
166
|
|
|
$wrapper = new Connection(null); |
167
|
|
|
|
168
|
|
|
$this->_object = m::mock(Records::class)->shouldDeferMissing() |
|
|
|
|
169
|
|
|
->shouldReceive('getRequest')->andReturn(Request::create('/')) |
170
|
|
|
->getMock(); |
171
|
|
|
|
172
|
|
|
$this->_object->setDB($wrapper); |
|
|
|
|
173
|
|
|
$this->_object->setTable('pages'); |
|
|
|
|
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|