1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use \Mockery as m; |
4
|
|
|
use Pyjac\ORM\Model; |
5
|
|
|
|
6
|
|
|
class ModelTest extends PHPUnit_Framework_TestCase |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* Instance of DatabaseConnection used in test. |
10
|
|
|
*/ |
11
|
|
|
protected $databaseConnection; |
12
|
|
|
|
13
|
|
|
protected $model; |
14
|
|
|
|
15
|
|
|
protected $sqlStatement; |
16
|
|
|
|
17
|
|
|
public function setUp(){ |
18
|
|
|
|
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
|
|
|
* @expectedException Pyjac\ORM\Exception\ModelNotFoundException |
57
|
|
|
*/ |
58
|
|
View Code Duplication |
public function testGetThrowsModelNotFoundExceptionWhenIdNotFoundInDatabase() |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement); |
61
|
|
|
$this->sqlStatement->shouldReceive('setFetchMode'); |
62
|
|
|
$this->sqlStatement->shouldReceive('execute'); |
63
|
|
|
$this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(0); |
64
|
|
|
|
65
|
|
|
$this->assertInstanceOf('stdClass', $this->model->get(1)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testAllReturnsAnArrayOfObjectsWhenValuesAreInDatabase() |
69
|
|
|
{ |
70
|
|
|
$this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement); |
71
|
|
|
$this->sqlStatement->shouldReceive('execute'); |
72
|
|
|
$this->sqlStatement->shouldReceive('fetchAll')->once()->andReturn([new stdClass,new stdClass]); |
73
|
|
|
$this->assertContainsOnlyInstancesOf('stdClass', $this->model->all()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
View Code Duplication |
public function testUpdateChangesTheValueOfObjectInDatabase() |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$this->model->setProperties(['id' => 2, 'name' => 'pyjac', 'age' => '419']); |
79
|
|
|
$this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement); |
80
|
|
|
$this->sqlStatement->shouldReceive('execute'); |
81
|
|
|
$this->sqlStatement->shouldReceive('fetchAll')->once()->andReturn([new stdClass,new stdClass]); |
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('fetchAll')->once()->andReturn([new stdClass,new stdClass]); |
93
|
|
|
$this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1); |
94
|
|
|
|
95
|
|
|
$this->assertEquals(1, $this->model->create()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
View Code Duplication |
public function testDeleteReturnsTrueWhenModelIsSuccessfullyRemovedFromDatabase() |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
$this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement); |
102
|
|
|
$this->sqlStatement->shouldReceive('execute'); |
103
|
|
|
$this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1); |
104
|
|
|
|
105
|
|
|
$this->assertEquals(true, $this->model->delete(1)); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
View Code Duplication |
public function testDeleteReturnsFalseWhenModelWasNotDeletedFromDatabase() |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
$this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement); |
111
|
|
|
$this->sqlStatement->shouldReceive('execute'); |
112
|
|
|
$this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(0); |
113
|
|
|
|
114
|
|
|
$this->assertEquals(false, $this->model->delete(1)); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
} |
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.