Completed
Pull Request — master (#11)
by Elisha-Wigwe Chijioke
03:32
created

testUpdateFunctionThrowsException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
use Elchroy\PotatoORM\PotatoQuery;
4
use Mockery as m;
5
6
class PotatoQueryTest extends \PHPUnit_Framework_TestCase
7
{
8
    public $mockConnector;
9
    public $mockStatement;
10
    private $mockQuery;
11
12
    public function setUp()
13
    {
14
        $this->mockConnector = m::mock('Elchroy\PotatoORM\PotatoConnector');
15
        $this->mockConnection = m::mock('PDO');
16
        $this->mockConnector->shouldReceive('setConnection')->andReturn($this->mockConnection);
17
        $this->mockConnector->connection = $this->mockConnection;
0 ignored issues
show
Bug introduced by
Accessing connection on the interface Mockery\MockInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
18
        $this->mockStatement = m::mock('PDOStatement');
19
        $this->mockQuery = new PotatoQuery($this->mockConnector);
20
    }
21
22 View Code Duplication
    public function testGetFrom()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
    {
24
        $sql = 'SELECT name, price FROM orders';
25
        $this->mockConnection->shouldReceive('prepare')->once()->with($sql)->andReturn($this->mockStatement);
26
        $this->mockStatement->shouldReceive('execute');
27
        $this->mockStatement->shouldReceive('fetchAll')->with(\PDO::FETCH_CLASS)->andReturn(new stdClass());
28
29
        $result = $this->mockQuery->getFrom('orders', 'name, price');
30
        $this->assertInstanceOf('stdClass', $result);
31
    }
32
33
    /**
34
     * @expectedException Elchroy\PotatoORMExceptions\FaultyExecutionException
35
     *
36
     * @expectedExceptionMessage There was a problem with excecuting this query.
37
     */
38
    public function testGetFromThrowsExceptionForWrongExecution()
39
    {
40
        $sql = 'SELECT name, price FROM orders';
41
        $this->mockConnection->shouldReceive('prepare')->once()->with($sql)->andReturn($this->mockStatement);
42
        $this->mockStatement->shouldReceive('execute')->andThrow('Elchroy\PotatoORMExceptions\FaultyExecutionException', 'There was a problem with excecuting this query.');
43
        $result = $this->mockQuery->getFrom('orders', 'name, price');
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
44
    }
45
46
    /**
47
     * @expectedException Elchroy\PotatoORMExceptions\NoRecordException
48
     *
49
     * @expectedExceptionMessage The table (orders) is empty.
50
     */
51 View Code Duplication
    public function testGetFromThrowsExceptionForNoRecordFound()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    {
53
        $sql = 'SELECT name, price FROM orders';
54
        $this->mockConnection->shouldReceive('prepare')->once()->with($sql)->andReturn($this->mockStatement);
55
        $this->mockStatement->shouldReceive('execute');
56
        $this->mockStatement->shouldReceive('fetchAll')->with(\PDO::FETCH_CLASS)->andThrow('Elchroy\PotatoORMExceptions\NoRecordException', 'The table (orders) is empty.');
57
        // $this->mockStatement->shouldReceive('fetchObject')->with('orders')->andReturn(true);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
58
59
        $result = $this->mockQuery->getFrom('orders', 'name, price');
60
        $this->assertInstanceOf('stdClass', $result);
61
    }
62
63
    /**
64
     * @expectedException Elchroy\PotatoORMExceptions\FaultyOrNoTableException
65
     *
66
     * @expectedExceptionMessage There seems to be a problem. Please confirm if the 'people' table exists in the database.
67
     */
68
    public function testGetFromThrowsException()
69
    {
70
        $sql = 'SELECT name, price FROM people';
71
        $this->mockConnection->shouldReceive('prepare')->once()->with($sql)->andReturn(false);
72
        $result = $this->mockQuery->getFrom('people', 'name, price');
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
73
    }
74
75
    /**
76
     * @expectedException Elchroy\PotatoORMExceptions\FaultyOrNoTableException
77
     *
78
     * @expectedExceptionMessage There seems to be a problem. Please confirm if the 'people' table exists in the database.
79
     */
80 View Code Duplication
    public function testGetOneThrowsException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
    {
82
        $sql = 'SELECT * FROM people WHERE id = :id ';
83
        $this->mockConnection->shouldReceive('prepare')->once()->with($sql)->andReturn(false);
84
85
        $result = $this->mockQuery->getOne('people', 43);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
86
    }
87
88 View Code Duplication
    public function testGetOne()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
    {
90
        $sql = 'SELECT * FROM people WHERE id = :id ';
91
        $this->mockConnection->shouldReceive('prepare')->once()->with($sql)->andReturn($this->mockStatement);
92
        $this->mockStatement->shouldReceive('bindParam')->once()->with(':id', 43);
93
        $this->mockStatement->shouldReceive('execute');
94
        $this->mockStatement->shouldReceive('fetchObject')->with('people')->andReturn(new stdClass());
95
96
        $result = $this->mockQuery->getOne('people', 43);
97
        $this->assertInstanceOf('stdClass', $result);
98
    }
99
100
    public function testStoreInWorks()
101
    {
102
        $data = ['name' => 'Diamane', 'rooms' => 400];
103
        $sql = 'INSERT INTO hotels (name, rooms) VALUES (?, ?)';
104
        $this->mockConnection->shouldReceive('prepare')->once()->with($sql)->andReturn($this->mockStatement);
105
        $this->mockStatement->shouldReceive('bindParam')->once()->with(1, 'Diamane');
106
        $this->mockStatement->shouldReceive('bindParam')->once()->with(2, 400);
107
        $this->mockStatement->shouldReceive('execute')->andReturn(true);
108
109
        $result = $this->mockQuery->storeIn('hotels', $data);
0 ignored issues
show
Documentation introduced by
$data is of type array<string,string|inte...ng","rooms":"integer"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
110
        $this->assertEquals(true, $result);
111
    }
112
113
    /**
114
     * @expectedException Elchroy\PotatoORMExceptions\FaultyOrNoTableException
115
     *
116
     * @expectedExceptionMessage There seems to be a problem. Please confirm if the 'people' table exists in the database.
117
     */
118
    public function testStoreInThrowsException()
119
    {
120
        $data = ['name' => 'Diamane', 'rooms' => 400];
121
        $sql = 'INSERT INTO people (name, rooms) VALUES (?, ?)';
122
        $this->mockConnection->shouldReceive('prepare')->once()->with($sql)->andReturn(false);
123
124
        $result = $this->mockQuery->storeIn('people', $data);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Documentation introduced by
$data is of type array<string,string|inte...ng","rooms":"integer"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
125
    }
126
127
    public function testGetColumnsWorks()
128
    {
129
        $data = ['name' => 'Diamane', 'rooms' => 400];
130
        $columnsString = '(name, rooms)';
131
        $result = $this->mockQuery->getColumns($data);
132
        $this->assertEquals($columnsString, $result);
133
    }
134
135
    public function testCountWorks()
136
    {
137
        $data = ['name' => 'Diamane', 'rooms' => 400];
138
        $this->assertEquals(2, count($data));
139
    }
140
141 View Code Duplication
    public function testDeleteWorks()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
    {
143
        $sql = 'DELETE FROM people WHERE id = :id ';
144
        $this->mockConnection->shouldReceive('prepare')->with($sql)->andReturn($this->mockStatement);
145
        $this->mockStatement->shouldReceive('bindParam')->with(':id', 43);
146
        $this->mockStatement->shouldReceive('execute')->andReturn(true);
147
148
        $result = $this->mockQuery->deleteFrom('people', 43);
149
        $this->assertEquals(true, $result);
150
    }
151
152
    /**
153
     * @expectedException Elchroy\PotatoORMExceptions\FaultyOrNoTableException
154
     *
155
     * @expectedExceptionMessage There seems to be a problem. Please confirm if the 'people' table exists in the database.
156
     */
157 View Code Duplication
    public function testDeleteThrowsException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
158
    {
159
        $sql = 'DELETE FROM people WHERE id = :id ';
160
        $this->mockConnection->shouldReceive('prepare')->with($sql)->andReturn(false);
161
162
        $result = $this->mockQuery->deleteFrom('people', 43);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
163
    }
164
165
    /**
166
     * @expectedException Elchroy\PotatoORMExceptions\NoRecordException
167
     *
168
     * @expectedExceptionMessage Record 43 : Not found found in this table people.
169
     */
170 View Code Duplication
    public function testGetOneThrowsExceptionForNoRecord()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
171
    {
172
        $sql = 'SELECT * FROM people WHERE id = :id ';
173
        $this->mockConnection->shouldReceive('prepare')->once()->with($sql)->andReturn($this->mockStatement);
174
        $this->mockStatement->shouldReceive('bindParam')->once()->with(':id', 43);
175
        $this->mockStatement->shouldReceive('execute');
176
        $this->mockStatement->shouldReceive('fetchObject')->with('people')->andThrow('Elchroy\PotatoORMExceptions\NoRecordException', 'Record 43 : Not found found in this table people.');
177
178
        $result = $this->mockQuery->getOne('people', 43);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
179
    }
180
181
    /**
182
     * @expectedException Elchroy\PotatoORMExceptions\NoRecordException
183
     *
184
     * @expectedExceptionMessage Record 4 : Not found found in this table (states).
185
     */
186
    public function testNoRecordExceptionWithId()
187
    {
188
        $this->mockQuery->throwNoRecordException('states', 4);
189
    }
190
191
    /**
192
     * @expectedException Elchroy\PotatoORMExceptions\NoRecordException
193
     *
194
     * @expectedExceptionMessage The table (states) is empty.
195
     */
196
    public function testNoRecordExceptionWithoutId()
197
    {
198
        $this->mockQuery->throwNoRecordException('states');
199
    }
200
201
    public function testUpdateFunctionworks()
202
    {
203
        $sql = 'UPDATE hotels SET name = :name_val, location = :location_val WHERE id = :id_val';
204
        $this->mockConnection->shouldReceive('prepare')->with($sql)->andReturn($this->mockStatement);
205
        $this->mockStatement->shouldReceive('bindValue')->with(':name_val', 'Diamane');
206
        $this->mockStatement->shouldReceive('bindValue')->with(':location_val', 'CapeTown, S-Africa');
207
        $this->mockStatement->shouldReceive('bindValue')->with(':id_val', 32);
208
        $this->mockStatement->shouldReceive('execute')->andReturn(true);
209
210
        $result = $this->mockQuery->updateAt('hotels', ['id' => 32, 'name' => 'Diamane', 'location' => 'CapeTown, S-Africa']);
211
        $this->assertEquals(true, $result);
212
    }
213
214
    /**
215
     * @expectedException Elchroy\PotatoORMExceptions\FaultyOrNoTableException
216
     *
217
     * @expectedExceptionMessage There seems to be a problem. Please confirm if the 'people' table exists in the database.
218
     */
219
    public function testUpdateFunctionThrowsException()
220
    {
221
        $sql = 'UPDATE people SET name = :name_val, location = :location_val WHERE id = :id_val';
222
        $this->mockConnection->shouldReceive('prepare')->with($sql)->andReturn(false);
223
224
        $result = $this->mockQuery->updateAt('people', ['id' => 32, 'name' => 'Diamane', 'location' => 'CapeTown, S-Africa']);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
225
    }
226
227
    public function testputQuesMarksWorks()
228
    {
229
        $result = $this->mockQuery->putQuesMarks(3);
230
        $this->assertEquals('?, ?, ?', $result);
231
    }
232
}
233