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