@@ -203,7 +203,7 @@ |
||
| 203 | 203 | /** |
| 204 | 204 | * Save the model data to the database. |
| 205 | 205 | * |
| 206 | - * @return bool |
|
| 206 | + * @return integer |
|
| 207 | 207 | */ |
| 208 | 208 | public function save() |
| 209 | 209 | { |
@@ -17,22 +17,22 @@ discard block |
||
| 17 | 17 | */ |
| 18 | 18 | protected $databaseConnection; |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * The id of the model. |
|
| 22 | - * |
|
| 23 | - * @property string $id |
|
| 24 | - */ |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Create a model instance. |
|
| 28 | - */ |
|
| 29 | - public function __construct(DatabaseConnectionInterface $databaseConnection = null) |
|
| 30 | - { |
|
| 31 | - if ($databaseConnection == null) { |
|
| 32 | - $this->databaseConnection = DatabaseConnection::getInstance()->databaseConnection; |
|
| 33 | - } |
|
| 34 | - $this->databaseConnection = $databaseConnection; |
|
| 35 | - } |
|
| 20 | + /** |
|
| 21 | + * The id of the model. |
|
| 22 | + * |
|
| 23 | + * @property string $id |
|
| 24 | + */ |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Create a model instance. |
|
| 28 | + */ |
|
| 29 | + public function __construct(DatabaseConnectionInterface $databaseConnection = null) |
|
| 30 | + { |
|
| 31 | + if ($databaseConnection == null) { |
|
| 32 | + $this->databaseConnection = DatabaseConnection::getInstance()->databaseConnection; |
|
| 33 | + } |
|
| 34 | + $this->databaseConnection = $databaseConnection; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | 37 | /** |
| 38 | 38 | * Sets into $properties the $key => $value pairs. |
@@ -55,23 +55,23 @@ discard block |
||
| 55 | 55 | return $this->properties[$key]; |
| 56 | 56 | } |
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Get all the model properties. |
|
| 60 | - * |
|
| 61 | - * @return array |
|
| 62 | - */ |
|
| 63 | - public function getProperties() |
|
| 64 | - { |
|
| 65 | - return $this->properties; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Set model properties. |
|
| 70 | - */ |
|
| 71 | - public function setProperties(array $properties) |
|
| 72 | - { |
|
| 73 | - $this->properties = $properties; |
|
| 74 | - } |
|
| 58 | + /** |
|
| 59 | + * Get all the model properties. |
|
| 60 | + * |
|
| 61 | + * @return array |
|
| 62 | + */ |
|
| 63 | + public function getProperties() |
|
| 64 | + { |
|
| 65 | + return $this->properties; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Set model properties. |
|
| 70 | + */ |
|
| 71 | + public function setProperties(array $properties) |
|
| 72 | + { |
|
| 73 | + $this->properties = $properties; |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | 76 | /** |
| 77 | 77 | * Gets the name of the child class with a 's'. |
@@ -152,17 +152,17 @@ discard block |
||
| 152 | 152 | public function update() |
| 153 | 153 | { |
| 154 | 154 | $bindNameParameters = []; |
| 155 | - $sqlUpdate = 'UPDATE '.$this->getTableName().' SET '; |
|
| 155 | + $sqlUpdate = 'UPDATE ' . $this->getTableName() . ' SET '; |
|
| 156 | 156 | foreach ($this->properties as $columnName => $columnValue) { |
| 157 | 157 | if ($columnName == 'id') { |
| 158 | 158 | continue; |
| 159 | 159 | } |
| 160 | - $bindColumnName = ':'.$columnName; |
|
| 160 | + $bindColumnName = ':' . $columnName; |
|
| 161 | 161 | $sqlUpdate .= "$columnName = $bindColumnName,"; |
| 162 | 162 | $bindNameParameters[$bindColumnName] = $columnValue; |
| 163 | 163 | } |
| 164 | 164 | //Remove the last comma in sql command then join it to the other query part. |
| 165 | - $sqlUpdate = substr($sqlUpdate, 0, -1).' WHERE id = :id'; |
|
| 165 | + $sqlUpdate = substr($sqlUpdate, 0, -1) . ' WHERE id = :id'; |
|
| 166 | 166 | $sqlStatement = $this->databaseConnection->prepare($sqlUpdate); |
| 167 | 167 | $bindNameParameters[':id'] = $this->properties['id']; |
| 168 | 168 | $sqlStatement->execute($bindNameParameters); |
@@ -180,18 +180,18 @@ discard block |
||
| 180 | 180 | $columnNames = ''; |
| 181 | 181 | $columnValues = ''; |
| 182 | 182 | $bindNameParameters = []; |
| 183 | - $sqlCreate = 'INSERT'.' INTO '.$this->getTableName().' ('; |
|
| 183 | + $sqlCreate = 'INSERT' . ' INTO ' . $this->getTableName() . ' ('; |
|
| 184 | 184 | foreach ($this->properties as $columnName => $columnValue) { |
| 185 | - $bindColumnName = ':'.$columnName; |
|
| 186 | - $columnNames .= $columnName.','; |
|
| 187 | - $columnValues .= $bindColumnName.','; |
|
| 185 | + $bindColumnName = ':' . $columnName; |
|
| 186 | + $columnNames .= $columnName . ','; |
|
| 187 | + $columnValues .= $bindColumnName . ','; |
|
| 188 | 188 | $bindNameParameters[$bindColumnName] = $columnValue; |
| 189 | 189 | } |
| 190 | 190 | // Remove ending comma and whitespace. |
| 191 | 191 | $columnNames = substr($columnNames, 0, -1); |
| 192 | 192 | $columnValues = substr($columnValues, 0, -1); |
| 193 | 193 | |
| 194 | - $sqlCreate .= $columnNames.') VALUES ('.$columnValues.')'; |
|
| 194 | + $sqlCreate .= $columnNames . ') VALUES (' . $columnValues . ')'; |
|
| 195 | 195 | $sqlStatement = $this->databaseConnection->prepare($sqlCreate); |
| 196 | 196 | $sqlStatement->execute($bindNameParameters); |
| 197 | 197 | |
@@ -231,7 +231,7 @@ discard block |
||
| 231 | 231 | */ |
| 232 | 232 | public function delete($id) |
| 233 | 233 | { |
| 234 | - $sql = 'DELETE'.' FROM '.self::getTableName().' WHERE id = '.$id; |
|
| 234 | + $sql = 'DELETE' . ' FROM ' . self::getTableName() . ' WHERE id = ' . $id; |
|
| 235 | 235 | $sqlStatment = $this->databaseConnection->prepare($sql); |
| 236 | 236 | $sqlStatment->execute(); |
| 237 | 237 | |
@@ -69,7 +69,7 @@ |
||
| 69 | 69 | * Reference: https://jtreminio.com/2013/03/unit-testing-tutorial-part-3-testing-protected-private-methods-coverage-reports-and-crap/ |
| 70 | 70 | * Call protected/private method of a class. |
| 71 | 71 | * |
| 72 | - * @param object &$object Instantiated object that we will run method on. |
|
| 72 | + * @param object DatabaseConnection Instantiated object that we will run method on. |
|
| 73 | 73 | * @param string $methodName Method name to call |
| 74 | 74 | * @param array $parameters Array of parameters to pass into method. |
| 75 | 75 | * |
@@ -15,7 +15,7 @@ |
||
| 15 | 15 | $databaseConnectionStringFactory = |
| 16 | 16 | m::mock('Pyjac\ORM\DatabaseConnectionStringFactoryInterface'); |
| 17 | 17 | $databaseConnectionStringFactory->shouldReceive('createDatabaseSourceString') |
| 18 | - ->with(['DRIVER' => 'sqlite', 'HOSTNAME' => '127.0.0.1', 'USERNAME' => '', 'PASSWORD' => '', 'DBNAME' => 'potatoORM', 'PORT' => '54320'])->once()->andReturn('sqlite::memory:'); |
|
| 18 | + ->with(['DRIVER' => 'sqlite', 'HOSTNAME' => '127.0.0.1', 'USERNAME' => '', 'PASSWORD' => '', 'DBNAME' => 'potatoORM', 'PORT' => '54320'])->once()->andReturn('sqlite::memory:'); |
|
| 19 | 19 | |
| 20 | 20 | $this->databaseConnection = new DatabaseConnection($databaseConnectionStringFactory); |
| 21 | 21 | } |
@@ -22,16 +22,16 @@ |
||
| 22 | 22 | |
| 23 | 23 | switch ($driver) { |
| 24 | 24 | case 'sqlite': |
| 25 | - $dsn = $driver.'::memory:'; |
|
| 25 | + $dsn = $driver . '::memory:'; |
|
| 26 | 26 | break; |
| 27 | 27 | case 'mysql': |
| 28 | 28 | case 'postgres': |
| 29 | 29 | if (strcasecmp($driver, 'postgres') == 0) { |
| 30 | 30 | $driver = 'pgsql'; |
| 31 | 31 | } |
| 32 | - $dsn = $driver.':host='.$config['HOSTNAME'].';dbname='.$config['DBNAME']; |
|
| 32 | + $dsn = $driver . ':host=' . $config['HOSTNAME'] . ';dbname=' . $config['DBNAME']; |
|
| 33 | 33 | if (isset($config['PORT'])) { |
| 34 | - $dsn .= ';port='.$config['PORT']; |
|
| 34 | + $dsn .= ';port=' . $config['PORT']; |
|
| 35 | 35 | } |
| 36 | 36 | break; |
| 37 | 37 | default: |
@@ -7,6 +7,6 @@ |
||
| 7 | 7 | { |
| 8 | 8 | public function __construct($id) |
| 9 | 9 | { |
| 10 | - parent::__construct('The requested Model with '.$id.' does not exist'); |
|
| 10 | + parent::__construct('The requested Model with ' . $id . ' does not exist'); |
|
| 11 | 11 | } |
| 12 | 12 | } |
@@ -21,7 +21,7 @@ discard block |
||
| 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 | |
@@ -36,8 +36,8 @@ discard block |
||
| 36 | 36 | { |
| 37 | 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 | } |
@@ -37,9 +37,9 @@ |
||
| 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() |