RecordsTest   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 62
dl 0
loc 158
rs 10
c 0
b 0
f 0
wmc 13

13 Methods

Rating   Name   Duplication   Size   Complexity  
A testSetModel() 0 7 1
A setUp() 0 12 1
A testGetFullNameTable() 0 6 1
A testGenerateModelClass() 0 6 1
A testNewCollection() 0 5 1
A testGetPrimaryFK() 0 12 1
A testRequestFilters() 0 21 1
A providerGetPrimaryFK() 0 8 1
A testGetCollectionClass() 0 3 1
A providerGetController() 0 6 1
A testGetController() 0 7 1
A testGetUrlHelper() 0 5 1
A testGetPrimaryKey() 0 8 1
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')
0 ignored issues
show
Bug introduced by
The method setField() does not exist on Nip\Records\Filters\AbstractFilter. Since it exists in all sub-types, consider adding an abstract or default implementation to Nip\Records\Filters\AbstractFilter. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

95
                ->/** @scrutinizer ignore-call */ setField('title')
Loading history...
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);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $filtersArray is correct as $this->_object->requestFilters($request) targeting Nip\Records\RecordManager::requestFilters() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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()
0 ignored issues
show
Documentation Bug introduced by
It seems like Mockery::mock(Nip\Record...create('/'))->getMock() of type Mockery\LegacyMockInterface is incompatible with the declared type Nip\Records\RecordManager of property $_object.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
Deprecated Code introduced by
The function Mockery\LegacyMockInterface::shouldDeferMissing() has been deprecated: 2.0.0 Please use makePartial() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

168
        $this->_object = /** @scrutinizer ignore-deprecated */ m::mock(Records::class)->shouldDeferMissing()

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
169
            ->shouldReceive('getRequest')->andReturn(Request::create('/'))
170
            ->getMock();
171
172
        $this->_object->setDB($wrapper);
0 ignored issues
show
Bug introduced by
The method setDB() does not exist on Mockery\LegacyMockInterface. It seems like you code against a sub-type of Mockery\LegacyMockInterface such as Mockery\Mock. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

172
        $this->_object->/** @scrutinizer ignore-call */ 
173
                        setDB($wrapper);
Loading history...
173
        $this->_object->setTable('pages');
0 ignored issues
show
Bug introduced by
The method setTable() does not exist on Mockery\LegacyMockInterface. It seems like you code against a sub-type of Mockery\LegacyMockInterface such as Mockery\Mock. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

173
        $this->_object->/** @scrutinizer ignore-call */ 
174
                        setTable('pages');
Loading history...
174
    }
175
}
176