1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
use \Mockery as m; |
5
|
|
|
use Pyjac\ORM\Model; |
6
|
|
|
|
7
|
|
|
class ModelTest extends PHPUnit_Framework_TestCase |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Instance of DatabaseConnection used in test. |
11
|
|
|
*/ |
12
|
|
|
protected $databaseConnection; |
13
|
|
|
|
14
|
|
|
protected $model; |
15
|
|
|
|
16
|
|
|
protected $sqlStatement; |
17
|
|
|
|
18
|
|
|
public function setUp() |
19
|
|
|
{ |
20
|
|
|
$databaseConnectionStringFactory = |
21
|
|
|
m::mock('Pyjac\ORM\DatabaseConnectionStringFactoryInterface'); |
22
|
|
|
|
23
|
|
|
$databaseConnectionStringFactory->shouldReceive('createDatabaseSourceString') |
24
|
|
|
->with(['DRIVER' => 'sqlite', 'HOSTNAME' => '127.0.0.1', 'USERNAME' => '', 'PASSWORD' => '', 'DBNAME' => 'potatoORM', 'PORT' => '54320'])->once()->andReturn('sqlite::memory:'); |
25
|
|
|
|
26
|
|
|
$this->databaseConnection = m::mock('Pyjac\ORM\DatabaseConnection'); |
27
|
|
|
|
28
|
|
|
$this->sqlStatement = m::mock('\PDOStatement'); |
29
|
|
|
|
30
|
|
|
/*$this->databaseConnection = m::mock('Pyjac\ORM\DatabaseConnection[getInstance,createConnection]',array($databaseConnectionStringFactory));*/ |
|
|
|
|
31
|
|
|
$this->model = $this->getMockForAbstractClass('Pyjac\ORM\Model', [$this->databaseConnection]); |
32
|
|
|
//= new DatabaseConnection($databaseConnectionStringFactory); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testGetTableNameReturnsCorrectTableName() |
36
|
|
|
{ |
37
|
|
|
$this->databaseConnection->shouldReceive('createConnection')->with('sqlite::memory:')->once()->andReturn(''); |
38
|
|
|
$this->model->expects($this->any()) |
39
|
|
|
->method('getTableName') |
40
|
|
|
->will($this->returnValue(strtolower(get_class($this->model).'s'))); |
41
|
|
|
|
42
|
|
|
$this->assertEquals($this->model->getTableName(), strtolower(get_class($this->model).'s')); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testGetReturnsAnObjectWhenIdIsFoundInDatabase() |
46
|
|
|
{ |
47
|
|
|
$this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement); |
48
|
|
|
$this->sqlStatement->shouldReceive('setFetchMode'); |
49
|
|
|
$this->sqlStatement->shouldReceive('execute'); |
50
|
|
|
$this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1); |
51
|
|
|
$this->sqlStatement->shouldReceive('fetch')->once()->andReturn(new stdClass()); |
52
|
|
|
|
53
|
|
|
$this->assertInstanceOf('stdClass', $this->model->get(1)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @expectedException Pyjac\ORM\Exception\ModelNotFoundException |
58
|
|
|
*/ |
59
|
|
|
public function testGetThrowsModelNotFoundExceptionWhenIdNotFoundInDatabase() |
60
|
|
|
{ |
61
|
|
|
$this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement); |
62
|
|
|
$this->sqlStatement->shouldReceive('setFetchMode'); |
63
|
|
|
$this->sqlStatement->shouldReceive('execute'); |
64
|
|
|
$this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(0); |
65
|
|
|
|
66
|
|
|
$this->assertInstanceOf('stdClass', $this->model->get(1)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testAllReturnsAnArrayOfObjectsWhenValuesAreInDatabase() |
70
|
|
|
{ |
71
|
|
|
$this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement); |
72
|
|
|
$this->sqlStatement->shouldReceive('execute'); |
73
|
|
|
$this->sqlStatement->shouldReceive('fetchAll')->once()->andReturn([new stdClass(), new stdClass()]); |
74
|
|
|
$this->assertContainsOnlyInstancesOf('stdClass', $this->model->all()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
View Code Duplication |
public function testUpdateChangesTheValueOfObjectInDatabase() |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$this->model->setProperties(['id' => 2, 'name' => 'pyjac', 'age' => '419']); |
80
|
|
|
$this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement); |
81
|
|
|
$this->sqlStatement->shouldReceive('execute'); |
82
|
|
|
$this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1); |
83
|
|
|
|
84
|
|
|
$this->assertEquals(1, $this->model->update()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
View Code Duplication |
public function testCreateObjectInDatabase() |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$this->model->setProperties(['id' => 2, 'name' => 'pyjac', 'age' => '419']); |
90
|
|
|
$this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement); |
91
|
|
|
$this->sqlStatement->shouldReceive('execute'); |
92
|
|
|
$this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1); |
93
|
|
|
|
94
|
|
|
$this->assertEquals(1, $this->model->create()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
View Code Duplication |
public function testSaveShouldCreateNewModelInDatabaseWhenIdNotPresent() |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
$this->model->setProperties(['name' => 'pyjac', 'age' => '419']); |
100
|
|
|
$this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement); |
101
|
|
|
$this->sqlStatement->shouldReceive('execute'); |
102
|
|
|
$this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1); |
103
|
|
|
$this->assertEquals(1, $this->model->save()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
View Code Duplication |
public function testSaveShouldUpdateModelInDatabaseIfIdIsPresent() |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
$this->model->setProperties(['id' => 2, 'name' => 'pyjac', 'age' => '419']); |
109
|
|
|
$this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement); |
110
|
|
|
$this->sqlStatement->shouldReceive('execute'); |
111
|
|
|
$this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1); |
112
|
|
|
$this->assertEquals(1, $this->model->save()); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function testGetPropertiesReturnsExpectedArrayValues() |
116
|
|
|
{ |
117
|
|
|
$values = ['id' => 2, 'name' => 'pyjac', 'age' => '419']; |
118
|
|
|
$this->model->setProperties($values); |
119
|
|
|
|
120
|
|
|
$this->assertEquals($values, $this->model->getProperties()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function testDeleteReturnsTrueWhenModelIsSuccessfullyRemovedFromDatabase() |
124
|
|
|
{ |
125
|
|
|
$this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement); |
126
|
|
|
$this->sqlStatement->shouldReceive('execute'); |
127
|
|
|
$this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1); |
128
|
|
|
|
129
|
|
|
$this->assertEquals(true, $this->model->delete(1)); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function testDeleteReturnsFalseWhenModelWasNotDeletedFromDatabase() |
133
|
|
|
{ |
134
|
|
|
$this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement); |
135
|
|
|
$this->sqlStatement->shouldReceive('execute'); |
136
|
|
|
$this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(0); |
137
|
|
|
|
138
|
|
|
$this->assertEquals(false, $this->model->delete(1)); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function testMagicMethodsReturnCorrectResult() |
142
|
|
|
{ |
143
|
|
|
$this->model->id = '10'; |
|
|
|
|
144
|
|
|
$this->model->name = 'Pyjac'; |
|
|
|
|
145
|
|
|
$this->model->age = '60'; |
|
|
|
|
146
|
|
|
|
147
|
|
|
$this->assertEquals($this->model->name, 'Pyjac'); |
|
|
|
|
148
|
|
|
$this->assertEquals($this->model->id, '10'); |
|
|
|
|
149
|
|
|
$this->assertEquals($this->model->age, '60'); |
|
|
|
|
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
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.