@@ -18,13 +18,13 @@ |
||
| 18 | 18 | |
| 19 | 19 | switch ($driver) { |
| 20 | 20 | case 'sqlite': |
| 21 | - $dsn = $driver.'::memory:'; |
|
| 21 | + $dsn = $driver . '::memory:'; |
|
| 22 | 22 | break; |
| 23 | 23 | case 'mysql': |
| 24 | 24 | case 'postgres': |
| 25 | - if(strcasecmp($driver, 'postgres') == 0) $driver="pgsql"; |
|
| 26 | - $dsn = $driver.':host='.$config['HOSTNAME'].';dbname='.$config['DBNAME']; |
|
| 27 | - if(isset($config['PORT'])) $dsn .= ';port='.$config['PORT']; |
|
| 25 | + if (strcasecmp($driver, 'postgres') == 0) $driver = "pgsql"; |
|
| 26 | + $dsn = $driver . ':host=' . $config['HOSTNAME'] . ';dbname=' . $config['DBNAME']; |
|
| 27 | + if (isset($config['PORT'])) $dsn .= ';port=' . $config['PORT']; |
|
| 28 | 28 | break; |
| 29 | 29 | default: |
| 30 | 30 | throw new DatabaseDriverNotSupportedException; |
@@ -178,7 +178,7 @@ |
||
| 178 | 178 | /** |
| 179 | 179 | * Save the model data to the database. |
| 180 | 180 | * |
| 181 | - * @return boolean |
|
| 181 | + * @return integer |
|
| 182 | 182 | */ |
| 183 | 183 | public function save() |
| 184 | 184 | { |
@@ -29,7 +29,7 @@ discard block |
||
| 29 | 29 | */ |
| 30 | 30 | public function __construct(DatabaseConnectionInterface $databaseConnection = null) |
| 31 | 31 | { |
| 32 | - if($databaseConnection == null){ |
|
| 32 | + if ($databaseConnection == null) { |
|
| 33 | 33 | $this->databaseConnection = DatabaseConnection::getInstance()->databaseConnection; |
| 34 | 34 | } |
| 35 | 35 | $this->databaseConnection = $databaseConnection; |
@@ -80,7 +80,7 @@ discard block |
||
| 80 | 80 | public function getTableName() |
| 81 | 81 | { |
| 82 | 82 | $className = explode('\\', get_called_class()); |
| 83 | - $table = strtolower(end($className) .'s'); |
|
| 83 | + $table = strtolower(end($className) . 's'); |
|
| 84 | 84 | return $table; |
| 85 | 85 | } |
| 86 | 86 | /** |
@@ -109,7 +109,7 @@ discard block |
||
| 109 | 109 | $sqlStatement = $this->databaseConnection->prepare($sql); |
| 110 | 110 | $sqlStatement->setFetchMode(PDO::FETCH_CLASS, get_called_class()); |
| 111 | 111 | $sqlStatement->execute(); |
| 112 | - if($sqlStatement->rowCount() < 1){ |
|
| 112 | + if ($sqlStatement->rowCount() < 1) { |
|
| 113 | 113 | throw new ModelNotFoundException($id); |
| 114 | 114 | } |
| 115 | 115 | return $sqlStatement->fetch(); |
@@ -149,15 +149,15 @@ discard block |
||
| 149 | 149 | { |
| 150 | 150 | |
| 151 | 151 | $bindNameParameters = []; |
| 152 | - $sqlUpdate = "UPDATE " . $this->getTableName() . " SET " ; |
|
| 152 | + $sqlUpdate = "UPDATE " . $this->getTableName() . " SET "; |
|
| 153 | 153 | foreach ($this->properties as $columnName => $columnValue) { |
| 154 | - if($columnName == 'id') continue; |
|
| 154 | + if ($columnName == 'id') continue; |
|
| 155 | 155 | $bindColumnName = ':' . $columnName; |
| 156 | 156 | $sqlUpdate .= "$columnName = $bindColumnName,"; |
| 157 | 157 | $bindNameParameters[$bindColumnName] = $columnValue; |
| 158 | 158 | } |
| 159 | 159 | //Remove the last comma in sql command then join it to the other query part. |
| 160 | - $sqlUpdate = substr($sqlUpdate, 0, -1)." WHERE id = :id"; |
|
| 160 | + $sqlUpdate = substr($sqlUpdate, 0, -1) . " WHERE id = :id"; |
|
| 161 | 161 | $sqlStatement = $this->databaseConnection->prepare($sqlUpdate); |
| 162 | 162 | $bindNameParameters[":id"] = $this->properties['id']; |
| 163 | 163 | $sqlStatement->execute($bindNameParameters); |
@@ -175,19 +175,19 @@ discard block |
||
| 175 | 175 | $columnNames = ""; |
| 176 | 176 | $columnValues = ""; |
| 177 | 177 | $bindNameParameters = []; |
| 178 | - $sqlCreate = "INSERT" . " INTO " . $this->getTableName()." ("; |
|
| 178 | + $sqlCreate = "INSERT" . " INTO " . $this->getTableName() . " ("; |
|
| 179 | 179 | foreach ($this->properties as $columnName => $columnValue) { |
| 180 | 180 | |
| 181 | 181 | $bindColumnName = ':' . $columnName; |
| 182 | - $columnNames .= $columnName.","; |
|
| 183 | - $columnValues .= $bindColumnName.","; |
|
| 182 | + $columnNames .= $columnName . ","; |
|
| 183 | + $columnValues .= $bindColumnName . ","; |
|
| 184 | 184 | $bindNameParameters[$bindColumnName] = $columnValue; |
| 185 | 185 | } |
| 186 | 186 | // Remove ending comma and whitespace. |
| 187 | 187 | $columnNames = substr($columnNames, 0, -1); |
| 188 | 188 | $columnValues = substr($columnValues, 0, -1); |
| 189 | 189 | |
| 190 | - $sqlCreate .= $columnNames.') VALUES (' .$columnValues.')'; |
|
| 190 | + $sqlCreate .= $columnNames . ') VALUES (' . $columnValues . ')'; |
|
| 191 | 191 | $sqlStatement = $this->databaseConnection->prepare($sqlCreate); |
| 192 | 192 | $sqlStatement->execute($bindNameParameters); |
| 193 | 193 | return $sqlStatement->rowCount(); |
@@ -222,7 +222,7 @@ discard block |
||
| 222 | 222 | */ |
| 223 | 223 | public function delete($id) |
| 224 | 224 | { |
| 225 | - $sql = "DELETE" . " FROM " . self::getTableName()." WHERE id = ". $id; |
|
| 225 | + $sql = "DELETE" . " FROM " . self::getTableName() . " WHERE id = " . $id; |
|
| 226 | 226 | $sqlStatment = $this->databaseConnection->prepare($sql); |
| 227 | 227 | $sqlStatment->execute(); |
| 228 | 228 | return ($sqlStatment->rowCount() > 0) ? true : false; |
@@ -26,21 +26,21 @@ discard block |
||
| 26 | 26 | |
| 27 | 27 | public function testCreateDatabaseSourceStringReturnsCorrectPostgresDatabaseSourceString() |
| 28 | 28 | { |
| 29 | - $this->config['DRIVER']= 'postgres'; |
|
| 29 | + $this->config['DRIVER'] = 'postgres'; |
|
| 30 | 30 | $dsn = $this->databaseConnectionStringFactory->createDatabaseSourceString($this->config); |
| 31 | 31 | $this->assertEquals("pgsql:host=localhost;dbname=Pyjac", $dsn); |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | public function testCreateDatabaseSourceStringReturnsCorrectMYSqlDatabaseSourceString() |
| 35 | 35 | { |
| 36 | - $this->config['DRIVER']= 'mysql'; |
|
| 36 | + $this->config['DRIVER'] = 'mysql'; |
|
| 37 | 37 | $dsn = $this->databaseConnectionStringFactory->createDatabaseSourceString($this->config); |
| 38 | 38 | $this->assertEquals("mysql:host=localhost;dbname=Pyjac", $dsn); |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | public function testCreateDatabaseSourceStringReturnsCorrectSQLiteDatabaseSourceString() |
| 42 | 42 | { |
| 43 | - $this->config['DRIVER']= 'sqlite'; |
|
| 43 | + $this->config['DRIVER'] = 'sqlite'; |
|
| 44 | 44 | $dsn = $this->databaseConnectionStringFactory->createDatabaseSourceString($this->config); |
| 45 | 45 | $this->assertEquals("sqlite::memory:", $dsn); |
| 46 | 46 | } |
@@ -50,7 +50,7 @@ discard block |
||
| 50 | 50 | */ |
| 51 | 51 | public function testCreateDatabaseSourceStringThrowsDatabaseDriverNotSupportedExceptionWhenUnknownDriverIsPassed() |
| 52 | 52 | { |
| 53 | - $this->config['DRIVER']= 'pyjac'; |
|
| 53 | + $this->config['DRIVER'] = 'pyjac'; |
|
| 54 | 54 | $dsn = $this->databaseConnectionStringFactory->createDatabaseSourceString($this->config); |
| 55 | 55 | } |
| 56 | 56 | } |
| 57 | 57 | \ No newline at end of file |
@@ -12,7 +12,7 @@ |
||
| 12 | 12 | |
| 13 | 13 | |
| 14 | 14 | |
| 15 | - public function setUp(){ |
|
| 15 | + public function setUp() { |
|
| 16 | 16 | $databaseConnectionStringFactory = |
| 17 | 17 | m::mock('Pyjac\ORM\DatabaseConnectionStringFactoryInterface'); |
| 18 | 18 | $databaseConnectionStringFactory->shouldReceive('createDatabaseSourceString') |
@@ -73,7 +73,7 @@ |
||
| 73 | 73 | * Reference: https://jtreminio.com/2013/03/unit-testing-tutorial-part-3-testing-protected-private-methods-coverage-reports-and-crap/ |
| 74 | 74 | * Call protected/private method of a class. |
| 75 | 75 | * |
| 76 | - * @param object &$object Instantiated object that we will run method on. |
|
| 76 | + * @param object DatabaseConnection Instantiated object that we will run method on. |
|
| 77 | 77 | * @param string $methodName Method name to call |
| 78 | 78 | * @param array $parameters Array of parameters to pass into method. |
| 79 | 79 | * |
@@ -5,6 +5,9 @@ |
||
| 5 | 5 | class ModelNotFoundException extends \Exception |
| 6 | 6 | { |
| 7 | 7 | |
| 8 | + /** |
|
| 9 | + * @param integer $id |
|
| 10 | + */ |
|
| 8 | 11 | function __construct($id) |
| 9 | 12 | { |
| 10 | 13 | parent::__construct('The requested Model with ' . $id . ' does not exist'); |
@@ -14,7 +14,7 @@ discard block |
||
| 14 | 14 | |
| 15 | 15 | protected $sqlStatement; |
| 16 | 16 | |
| 17 | - public function setUp(){ |
|
| 17 | + public function setUp() { |
|
| 18 | 18 | |
| 19 | 19 | |
| 20 | 20 | $databaseConnectionStringFactory = |
@@ -28,7 +28,7 @@ discard block |
||
| 28 | 28 | $this->sqlStatement = m::mock('\PDOStatement'); |
| 29 | 29 | |
| 30 | 30 | /*$this->databaseConnection = m::mock('Pyjac\ORM\DatabaseConnection[getInstance,createConnection]',array($databaseConnectionStringFactory));*/ |
| 31 | - $this->model = $this->getMockForAbstractClass('Pyjac\ORM\Model',[$this->databaseConnection]); |
|
| 31 | + $this->model = $this->getMockForAbstractClass('Pyjac\ORM\Model', [$this->databaseConnection]); |
|
| 32 | 32 | //= new DatabaseConnection($databaseConnectionStringFactory); |
| 33 | 33 | } |
| 34 | 34 | |
@@ -37,9 +37,9 @@ discard block |
||
| 37 | 37 | $this->databaseConnection->shouldReceive('createConnection')->with('sqlite::memory:')->once()->andReturn(""); |
| 38 | 38 | $this->model->expects($this->any()) |
| 39 | 39 | ->method('getTableName') |
| 40 | - ->will($this->returnValue(strtolower(get_class($this->model).'s'))); |
|
| 40 | + ->will($this->returnValue(strtolower(get_class($this->model) . 's'))); |
|
| 41 | 41 | |
| 42 | - $this->assertEquals($this->model->getTableName(), strtolower(get_class($this->model).'s')); |
|
| 42 | + $this->assertEquals($this->model->getTableName(), strtolower(get_class($this->model) . 's')); |
|
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | public function testGetReturnsAnObjectWhenIdIsFoundInDatabase() |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | { |
| 70 | 70 | $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement); |
| 71 | 71 | $this->sqlStatement->shouldReceive('execute'); |
| 72 | - $this->sqlStatement->shouldReceive('fetchAll')->once()->andReturn([new stdClass,new stdClass]); |
|
| 72 | + $this->sqlStatement->shouldReceive('fetchAll')->once()->andReturn([new stdClass, new stdClass]); |
|
| 73 | 73 | $this->assertContainsOnlyInstancesOf('stdClass', $this->model->all()); |
| 74 | 74 | } |
| 75 | 75 | |
@@ -78,7 +78,7 @@ discard block |
||
| 78 | 78 | $this->model->setProperties(['id' => 2, 'name' => 'pyjac', 'age' => '419']); |
| 79 | 79 | $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement); |
| 80 | 80 | $this->sqlStatement->shouldReceive('execute'); |
| 81 | - $this->sqlStatement->shouldReceive('fetchAll')->once()->andReturn([new stdClass,new stdClass]); |
|
| 81 | + $this->sqlStatement->shouldReceive('fetchAll')->once()->andReturn([new stdClass, new stdClass]); |
|
| 82 | 82 | $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1); |
| 83 | 83 | |
| 84 | 84 | $this->assertEquals(1, $this->model->update()); |
@@ -89,7 +89,7 @@ discard block |
||
| 89 | 89 | $this->model->setProperties(['id' => 2, 'name' => 'pyjac', 'age' => '419']); |
| 90 | 90 | $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement); |
| 91 | 91 | $this->sqlStatement->shouldReceive('execute'); |
| 92 | - $this->sqlStatement->shouldReceive('fetchAll')->once()->andReturn([new stdClass,new stdClass]); |
|
| 92 | + $this->sqlStatement->shouldReceive('fetchAll')->once()->andReturn([new stdClass, new stdClass]); |
|
| 93 | 93 | $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1); |
| 94 | 94 | |
| 95 | 95 | $this->assertEquals(1, $this->model->create()); |