@@ -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; |
@@ -22,9 +22,13 @@ |
||
22 | 22 | break; |
23 | 23 | case 'mysql': |
24 | 24 | case 'postgres': |
25 | - if(strcasecmp($driver, 'postgres') == 0) $driver="pgsql"; |
|
25 | + if(strcasecmp($driver, 'postgres') == 0) { |
|
26 | + $driver="pgsql"; |
|
27 | + } |
|
26 | 28 | $dsn = $driver.':host='.$config['HOSTNAME'].';dbname='.$config['DBNAME']; |
27 | - if(isset($config['PORT'])) $dsn .= ';port='.$config['PORT']; |
|
29 | + if(isset($config['PORT'])) { |
|
30 | + $dsn .= ';port='.$config['PORT']; |
|
31 | + } |
|
28 | 32 | break; |
29 | 33 | default: |
30 | 34 | 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 | { |
@@ -137,7 +137,9 @@ |
||
137 | 137 | $bindNameParameters = []; |
138 | 138 | $sqlUpdate = "UPDATE " . $this->getTableName() . " SET " ; |
139 | 139 | foreach ($this->properties as $columnName => $columnValue) { |
140 | - if($columnName == 'id') continue; |
|
140 | + if($columnName == 'id') { |
|
141 | + continue; |
|
142 | + } |
|
141 | 143 | $bindColumnName = ':' . $columnName; |
142 | 144 | $sqlUpdate .= "$columnName = $bindColumnName,"; |
143 | 145 | $bindNameParameters[$bindColumnName] = $columnValue; |
@@ -13,21 +13,21 @@ discard block |
||
13 | 13 | |
14 | 14 | /** |
15 | 15 | * Store instance of database connection used. |
16 | - * @var [type] |
|
17 | - */ |
|
16 | + * @var [type] |
|
17 | + */ |
|
18 | 18 | protected $databaseConnection; |
19 | 19 | |
20 | 20 | /** |
21 | 21 | * The id of the model. |
22 | 22 | * |
23 | 23 | * @property string $id |
24 | - */ |
|
24 | + */ |
|
25 | 25 | |
26 | 26 | /** |
27 | 27 | * Create a model instance. |
28 | 28 | * |
29 | 29 | */ |
30 | - public function __construct(DatabaseConnectionInterface $databaseConnection = null) |
|
30 | + public function __construct(DatabaseConnectionInterface $databaseConnection = null) |
|
31 | 31 | { |
32 | 32 | if($databaseConnection == null){ |
33 | 33 | $this->databaseConnection = DatabaseConnection::getInstance()->databaseConnection; |
@@ -37,19 +37,19 @@ discard block |
||
37 | 37 | /** |
38 | 38 | * Sets into $properties the $key => $value pairs |
39 | 39 | * |
40 | - * @param string $key |
|
41 | - * @param string $val |
|
42 | - * |
|
43 | - */ |
|
40 | + * @param string $key |
|
41 | + * @param string $val |
|
42 | + * |
|
43 | + */ |
|
44 | 44 | public function __set($key, $val) |
45 | 45 | { |
46 | 46 | $this->properties[$key] = $val; |
47 | 47 | } |
48 | 48 | /** |
49 | - * @param string $key |
|
50 | - * |
|
51 | - * @return array |
|
52 | - */ |
|
49 | + * @param string $key |
|
50 | + * |
|
51 | + * @return array |
|
52 | + */ |
|
53 | 53 | public function __get($key) |
54 | 54 | { |
55 | 55 | return $this->properties[$key]; |
@@ -59,24 +59,24 @@ discard block |
||
59 | 59 | * |
60 | 60 | * @return array |
61 | 61 | */ |
62 | - public function getProperties() |
|
63 | - { |
|
64 | - return $this->properties; |
|
65 | - } |
|
62 | + public function getProperties() |
|
63 | + { |
|
64 | + return $this->properties; |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * Set model properties. |
|
67 | + /** |
|
68 | + * Set model properties. |
|
69 | + * |
|
70 | + */ |
|
71 | + public function setProperties(array $properties) |
|
72 | + { |
|
73 | + $this->properties = $properties; |
|
74 | + } |
|
75 | + /** |
|
76 | + * Gets the name of the child class with a 's'. |
|
69 | 77 | * |
78 | + * @return string |
|
70 | 79 | */ |
71 | - public function setProperties(array $properties) |
|
72 | - { |
|
73 | - $this->properties = $properties; |
|
74 | - } |
|
75 | - /** |
|
76 | - * Gets the name of the child class with a 's'. |
|
77 | - * |
|
78 | - * @return string |
|
79 | - */ |
|
80 | 80 | public function getTableName() |
81 | 81 | { |
82 | 82 | $className = explode('\\', get_called_class()); |
@@ -84,12 +84,12 @@ discard block |
||
84 | 84 | return $table; |
85 | 85 | } |
86 | 86 | /** |
87 | - * Find the particular model with the passed id. |
|
88 | - * |
|
89 | - * @param int $id |
|
90 | - * |
|
91 | - * @return object |
|
92 | - */ |
|
87 | + * Find the particular model with the passed id. |
|
88 | + * |
|
89 | + * @param int $id |
|
90 | + * |
|
91 | + * @return object |
|
92 | + */ |
|
93 | 93 | public static function find($id) |
94 | 94 | { |
95 | 95 | $model = new static; |
@@ -97,12 +97,12 @@ discard block |
||
97 | 97 | } |
98 | 98 | |
99 | 99 | /** |
100 | - * Get the particular model with the passed id. |
|
101 | - * |
|
102 | - * @param int $id |
|
103 | - * |
|
104 | - * @return object |
|
105 | - */ |
|
100 | + * Get the particular model with the passed id. |
|
101 | + * |
|
102 | + * @param int $id |
|
103 | + * |
|
104 | + * @return object |
|
105 | + */ |
|
106 | 106 | public function get($id) |
107 | 107 | { |
108 | 108 | $sql = "SELECT * FROM {$this->getTableName()} WHERE id={$id}"; |
@@ -116,10 +116,10 @@ discard block |
||
116 | 116 | } |
117 | 117 | |
118 | 118 | /** |
119 | - * Get all the models from the database. |
|
120 | - * |
|
121 | - * @return array |
|
122 | - */ |
|
119 | + * Get all the models from the database. |
|
120 | + * |
|
121 | + * @return array |
|
122 | + */ |
|
123 | 123 | public static function getAll() |
124 | 124 | { |
125 | 125 | $model = new static; |
@@ -127,10 +127,10 @@ discard block |
||
127 | 127 | } |
128 | 128 | |
129 | 129 | /** |
130 | - * Returns all the models from the database. |
|
131 | - * |
|
132 | - * @return array |
|
133 | - */ |
|
130 | + * Returns all the models from the database. |
|
131 | + * |
|
132 | + * @return array |
|
133 | + */ |
|
134 | 134 | public function all() |
135 | 135 | { |
136 | 136 | $sql = "SELECT * FROM {$this->getTableName()}"; |
@@ -144,7 +144,7 @@ discard block |
||
144 | 144 | * Update the model in the database. |
145 | 145 | * |
146 | 146 | * @return int |
147 | - */ |
|
147 | + */ |
|
148 | 148 | public function update() |
149 | 149 | { |
150 | 150 | |
@@ -165,10 +165,10 @@ discard block |
||
165 | 165 | } |
166 | 166 | |
167 | 167 | /** |
168 | - * Insert the model values into the database. |
|
169 | - * |
|
170 | - * @return int |
|
171 | - */ |
|
168 | + * Insert the model values into the database. |
|
169 | + * |
|
170 | + * @return int |
|
171 | + */ |
|
172 | 172 | public function create() |
173 | 173 | { |
174 | 174 | |
@@ -203,11 +203,11 @@ discard block |
||
203 | 203 | return isset($this->properties['id']) ? $this->update() : $this->create(); |
204 | 204 | } |
205 | 205 | |
206 | - /** |
|
207 | - * Delete a model from the database. |
|
208 | - * @param int $id |
|
209 | - * @return boolean |
|
210 | - */ |
|
206 | + /** |
|
207 | + * Delete a model from the database. |
|
208 | + * @param int $id |
|
209 | + * @return boolean |
|
210 | + */ |
|
211 | 211 | public static function destroy($id) |
212 | 212 | { |
213 | 213 | $model = new static; |
@@ -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; |
@@ -5,11 +5,11 @@ discard block |
||
5 | 5 | |
6 | 6 | class DatabaseConnectionStringFactoryTest extends PHPUnit_Framework_TestCase |
7 | 7 | { |
8 | - /** |
|
9 | - * The instance of DatabaseConnectionStringFactory used in the test. |
|
10 | - * |
|
11 | - * @var Pyjac\ORM\DatabaseConnectionStringFactory |
|
12 | - */ |
|
8 | + /** |
|
9 | + * The instance of DatabaseConnectionStringFactory used in the test. |
|
10 | + * |
|
11 | + * @var Pyjac\ORM\DatabaseConnectionStringFactory |
|
12 | + */ |
|
13 | 13 | protected $databaseConnectionStringFactoryTest; |
14 | 14 | |
15 | 15 | /** |
@@ -45,9 +45,9 @@ discard block |
||
45 | 45 | $this->assertEquals("sqlite::memory:", $dsn); |
46 | 46 | } |
47 | 47 | |
48 | - /** |
|
49 | - * @expectedException Pyjac\ORM\Exception\DatabaseDriverNotSupportedException |
|
50 | - */ |
|
48 | + /** |
|
49 | + * @expectedException Pyjac\ORM\Exception\DatabaseDriverNotSupportedException |
|
50 | + */ |
|
51 | 51 | public function testCreateDatabaseSourceStringThrowsDatabaseDriverNotSupportedExceptionWhenUnknownDriverIsPassed() |
52 | 52 | { |
53 | 53 | $this->config['DRIVER']= 'pyjac'; |
@@ -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 |
@@ -24,14 +24,14 @@ |
||
24 | 24 | ]; |
25 | 25 | } |
26 | 26 | public function testContainsReturnsFalseWhenStringNotFoundInArray() |
27 | - { |
|
27 | + { |
|
28 | 28 | $this->assertFalse(Helpers::contains("PHP rocks", $this->testArray)); |
29 | 29 | |
30 | - } |
|
30 | + } |
|
31 | 31 | |
32 | 32 | public function testContainsReturnsTrueWhenStringIsFoundInArray() |
33 | - { |
|
33 | + { |
|
34 | 34 | $this->assertTrue(Helpers::contains("Error while sending", $this->testArray)); |
35 | 35 | |
36 | - } |
|
36 | + } |
|
37 | 37 | } |
38 | 38 | \ 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,86 +5,86 @@ |
||
5 | 5 | |
6 | 6 | class DatabaseConnectionTest 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 | 13 | |
14 | 14 | |
15 | - public function setUp(){ |
|
16 | - $databaseConnectionStringFactory = |
|
15 | + public function setUp(){ |
|
16 | + $databaseConnectionStringFactory = |
|
17 | 17 | m::mock('Pyjac\ORM\DatabaseConnectionStringFactoryInterface'); |
18 | 18 | $databaseConnectionStringFactory->shouldReceive('createDatabaseSourceString') |
19 | - ->with(['DRIVER' => 'sqlite', 'HOSTNAME' => '127.0.0.1', 'USERNAME' => '', 'PASSWORD' => '', 'DBNAME' => 'potatoORM', 'PORT' => '54320'])->once()->andReturn('sqlite::memory:'); |
|
19 | + ->with(['DRIVER' => 'sqlite', 'HOSTNAME' => '127.0.0.1', 'USERNAME' => '', 'PASSWORD' => '', 'DBNAME' => 'potatoORM', 'PORT' => '54320'])->once()->andReturn('sqlite::memory:'); |
|
20 | 20 | |
21 | 21 | $this->databaseConnection = new DatabaseConnection($databaseConnectionStringFactory); |
22 | - } |
|
22 | + } |
|
23 | 23 | |
24 | - public function testCreateConnectionReturnsDatabaseConnection() |
|
25 | - { |
|
26 | - $dbInstance = $this->databaseConnection->createConnection('sqlite::memory:'); |
|
24 | + public function testCreateConnectionReturnsDatabaseConnection() |
|
25 | + { |
|
26 | + $dbInstance = $this->databaseConnection->createConnection('sqlite::memory:'); |
|
27 | 27 | |
28 | - $this->assertInstanceOf('PDO', $dbInstance); |
|
28 | + $this->assertInstanceOf('PDO', $dbInstance); |
|
29 | 29 | |
30 | - } |
|
30 | + } |
|
31 | 31 | |
32 | - public function testGetInstanceReturnsCorrectInstance() |
|
33 | - { |
|
34 | - $dbInstance = DatabaseConnection::getInstance(); |
|
32 | + public function testGetInstanceReturnsCorrectInstance() |
|
33 | + { |
|
34 | + $dbInstance = DatabaseConnection::getInstance(); |
|
35 | 35 | |
36 | - $this->assertInstanceOf('Pyjac\ORM\DatabaseConnection', $dbInstance); |
|
36 | + $this->assertInstanceOf('Pyjac\ORM\DatabaseConnection', $dbInstance); |
|
37 | 37 | |
38 | - } |
|
38 | + } |
|
39 | 39 | |
40 | - public function testSetOptionsAndGetOptionsReturnsCorrectValue() |
|
41 | - { |
|
42 | - $options = [ |
|
43 | - PDO::ATTR_CASE => PDO::CASE_NATURAL, |
|
44 | - PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
|
45 | - PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, |
|
46 | - PDO::ATTR_STRINGIFY_FETCHES => false, |
|
47 | - PDO::ATTR_EMULATE_PREPARES => false, |
|
48 | - ]; |
|
49 | - $this->databaseConnection->setDefaultOptions($options); |
|
40 | + public function testSetOptionsAndGetOptionsReturnsCorrectValue() |
|
41 | + { |
|
42 | + $options = [ |
|
43 | + PDO::ATTR_CASE => PDO::CASE_NATURAL, |
|
44 | + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
|
45 | + PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, |
|
46 | + PDO::ATTR_STRINGIFY_FETCHES => false, |
|
47 | + PDO::ATTR_EMULATE_PREPARES => false, |
|
48 | + ]; |
|
49 | + $this->databaseConnection->setDefaultOptions($options); |
|
50 | 50 | |
51 | - $this->assertEquals($this->databaseConnection->getDefaultOptions(), $options); |
|
52 | - } |
|
51 | + $this->assertEquals($this->databaseConnection->getDefaultOptions(), $options); |
|
52 | + } |
|
53 | 53 | |
54 | - public function testTryAgainIfCausedByLostConnectionCreateNewConnectionWhenReasonForExceptionIsConnectionLoss() |
|
55 | - { |
|
56 | - $e = new \Exception("Error while sending"); |
|
57 | - $result = $this->invokeMethod($this->databaseConnection, 'tryAgainIfCausedByLostConnection', [$e, 'sqlite::memory:', '', '', []]); |
|
54 | + public function testTryAgainIfCausedByLostConnectionCreateNewConnectionWhenReasonForExceptionIsConnectionLoss() |
|
55 | + { |
|
56 | + $e = new \Exception("Error while sending"); |
|
57 | + $result = $this->invokeMethod($this->databaseConnection, 'tryAgainIfCausedByLostConnection', [$e, 'sqlite::memory:', '', '', []]); |
|
58 | 58 | |
59 | - $this->assertInstanceOf('PDO', $result); |
|
60 | - } |
|
59 | + $this->assertInstanceOf('PDO', $result); |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
62 | + /** |
|
63 | 63 | * @expectedException \Exception |
64 | 64 | */ |
65 | - public function testTryAgainIfCausedByLostConnectionThrowsExceptionWhenReasonForExceptionIsNotConnectionLoss() |
|
66 | - { |
|
67 | - $e = new \Exception("PHP Rocks !!!"); |
|
68 | - $result = $this->invokeMethod($this->databaseConnection, 'tryAgainIfCausedByLostConnection', [$e, 'sqlite::memory:', '', '', []]); |
|
69 | - |
|
70 | - } |
|
71 | - |
|
72 | - /** |
|
73 | - * Reference: https://jtreminio.com/2013/03/unit-testing-tutorial-part-3-testing-protected-private-methods-coverage-reports-and-crap/ |
|
74 | - * Call protected/private method of a class. |
|
75 | - * |
|
76 | - * @param object &$object Instantiated object that we will run method on. |
|
77 | - * @param string $methodName Method name to call |
|
78 | - * @param array $parameters Array of parameters to pass into method. |
|
79 | - * |
|
80 | - * @return mixed Method return. |
|
81 | - */ |
|
82 | - public function invokeMethod(&$object, $methodName, array $parameters = array()) |
|
83 | - { |
|
84 | - $reflection = new \ReflectionClass(get_class($object)); |
|
85 | - $method = $reflection->getMethod($methodName); |
|
86 | - $method->setAccessible(true); |
|
87 | - |
|
88 | - return $method->invokeArgs($object, $parameters); |
|
89 | - } |
|
65 | + public function testTryAgainIfCausedByLostConnectionThrowsExceptionWhenReasonForExceptionIsNotConnectionLoss() |
|
66 | + { |
|
67 | + $e = new \Exception("PHP Rocks !!!"); |
|
68 | + $result = $this->invokeMethod($this->databaseConnection, 'tryAgainIfCausedByLostConnection', [$e, 'sqlite::memory:', '', '', []]); |
|
69 | + |
|
70 | + } |
|
71 | + |
|
72 | + /** |
|
73 | + * Reference: https://jtreminio.com/2013/03/unit-testing-tutorial-part-3-testing-protected-private-methods-coverage-reports-and-crap/ |
|
74 | + * Call protected/private method of a class. |
|
75 | + * |
|
76 | + * @param object &$object Instantiated object that we will run method on. |
|
77 | + * @param string $methodName Method name to call |
|
78 | + * @param array $parameters Array of parameters to pass into method. |
|
79 | + * |
|
80 | + * @return mixed Method return. |
|
81 | + */ |
|
82 | + public function invokeMethod(&$object, $methodName, array $parameters = array()) |
|
83 | + { |
|
84 | + $reflection = new \ReflectionClass(get_class($object)); |
|
85 | + $method = $reflection->getMethod($methodName); |
|
86 | + $method->setAccessible(true); |
|
87 | + |
|
88 | + return $method->invokeArgs($object, $parameters); |
|
89 | + } |
|
90 | 90 | } |
91 | 91 | \ No newline at end of file |
@@ -4,7 +4,7 @@ |
||
4 | 4 | |
5 | 5 | interface DatabaseConnectionInterface |
6 | 6 | { |
7 | - /** |
|
7 | + /** |
|
8 | 8 | * Get the instance of the class. |
9 | 9 | * |
10 | 10 | * @return Pyjac\ORM\DatabaseConnection |
@@ -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 | |
@@ -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 |
@@ -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 |