@@ -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; |
@@ -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') |
@@ -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; |
@@ -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 | |
@@ -104,7 +104,7 @@ discard block |
||
104 | 104 | |
105 | 105 | public function testSaveShouldUpdateModelInDatabaseIfIdIsPresent() |
106 | 106 | { |
107 | - $this->model->setProperties(['id' => 2,'name' => 'pyjac', 'age' => '419']); |
|
107 | + $this->model->setProperties(['id' => 2, 'name' => 'pyjac', 'age' => '419']); |
|
108 | 108 | $this->databaseConnection->shouldReceive('prepare')->once()->andReturn($this->sqlStatement); |
109 | 109 | $this->sqlStatement->shouldReceive('execute'); |
110 | 110 | $this->sqlStatement->shouldReceive('rowCount')->once()->andReturn(1); |
@@ -140,7 +140,7 @@ discard block |
||
140 | 140 | $this->model->age = '60'; |
141 | 141 | |
142 | 142 | $this->assertEquals($this->model->name, 'Pyjac'); |
143 | - $this->assertEquals($this->model->id , '10'); |
|
143 | + $this->assertEquals($this->model->id, '10'); |
|
144 | 144 | $this->assertEquals($this->model->age, '60'); |
145 | 145 | } |
146 | 146 |