diff --git a/src/Connection/MySqlConnection.php b/src/Connection/MySqlConnection.php index dcdd1a4..beb6dd6 100644 --- a/src/Connection/MySqlConnection.php +++ b/src/Connection/MySqlConnection.php @@ -85,7 +85,7 @@ public function delete(string $table, array $where): \PDOStatement public function formatFields(array $fields, string $table = '', string $prefix = ''): string { - if (!$fields) { + if ($fields === []) { throw new \InvalidArgumentException('No fields provided for the query'); } @@ -130,7 +130,7 @@ public function formatTable(string $table, string $alias = ''): string */ private function formatConditions(array $conditions, array & $parameters): string { - if (!$conditions) { + if ($conditions === []) { throw new \InvalidArgumentException('No conditions provided for the query'); } @@ -160,7 +160,7 @@ private function formatClause(string $field, $value, array & $parameters): strin return $value !== null; }); - if ($value) { + if ($value !== []) { $placeholders = $this->formatParameters($value, $parameters); return "($escaped IN $placeholders OR $escaped IS NULL)"; } @@ -250,7 +250,7 @@ private function formatLimit(?int $limit, array & $parameters): string */ private function formatAssignments(array $values, array & $parameters): string { - if (!$values) { + if ($values === []) { throw new \InvalidArgumentException('No values provided for the query'); } @@ -298,6 +298,10 @@ public function query(string $sql, array $parameters = []): \PDOStatement { $query = $this->getConnection()->prepare($sql); + if (! $query instanceof \PDOStatement) { + throw new \UnexpectedValueException('Unexpected value returned by prepare query'); + } + foreach ($parameters as $name => $value) { $this->bindQueryParameter($query, \is_int($name) ? $name + 1 : $name, $value); } diff --git a/src/Record.php b/src/Record.php index c1e7e3e..ded8d0e 100644 --- a/src/Record.php +++ b/src/Record.php @@ -213,7 +213,7 @@ public function associate(string $name, Model $model): void throw new \InvalidArgumentException('The associated record belongs to incorrect schema'); } - while ($keys) { + while ($keys !== []) { $value = $record[array_pop($fields)]; if ($value === null) { diff --git a/src/Relationship.php b/src/Relationship.php index ee6daab..9e7722b 100644 --- a/src/Relationship.php +++ b/src/Relationship.php @@ -275,7 +275,7 @@ public function fillSingleRecord(Record $record, Record $referencedRecord): void $keys = $this->getFields(); $fields = $this->getReferencedFields(); - while ($keys) { + while ($keys !== []) { if ((string) $record[array_pop($keys)] !== (string) $referencedRecord[array_pop($fields)]) { throw new \InvalidArgumentException('The provided records are not related'); } diff --git a/src/RelationshipFiller.php b/src/RelationshipFiller.php index 5ea754d..6d6f7b7 100644 --- a/src/RelationshipFiller.php +++ b/src/RelationshipFiller.php @@ -102,7 +102,7 @@ private function fillRelationships(array $records, array $relationships): void $loaded = empty($filled) ? [] : array_merge(... array_values($filled)); $options = array_keys(array_diff_key($options, $filled)); - if ($options) { + if ($options !== []) { $result = $this->connection->select($parent->getFields(), $parent->getTable(), [$field => $options]); $result->setFetchMode(\PDO::FETCH_ASSOC); diff --git a/tests/tests/MySqlIntegrationTest.php b/tests/tests/MySqlIntegrationTest.php index cc602a5..f45c9e4 100644 --- a/tests/tests/MySqlIntegrationTest.php +++ b/tests/tests/MySqlIntegrationTest.php @@ -4,6 +4,7 @@ use Simply\Database\Connection\Connection; use Simply\Database\Connection\MySqlConnection; +use Simply\Database\Connection\Provider\ConnectionProvider; use Simply\Database\Connection\Provider\GenericConnectionProvider; use Simply\Database\Connection\Provider\MySqlConnectionProvider; use Simply\Database\Test\TestCase\IntegrationTestCase; @@ -115,4 +116,21 @@ public function testDSNSupport(): void $connection = new MySqlConnectionProvider('/tmp/mysql.sock', 'database', 'username', 'password'); $this->assertContains('unix_socket', $property->getValue($connection)); } + + public function testHandlingFalsePrepare() + { + $pdo = $this->getMockBuilder(\PDO::class) + ->disableOriginalConstructor() + ->getMock(); + + $pdo->expects($this->once())->method('prepare')->willReturn(false); + + $connection = $this->createMock(ConnectionProvider::class); + $connection->method('getConnection')->willReturn($pdo); + + $database = new MySqlConnection($connection); + + $this->expectException(\UnexpectedValueException::class); + $database->query('SELECT * FROM `' . $this->personSchema->getTable() . '`'); + } }