From 02314cc18f8c1b06744b68ed045f174055d9c4c8 Mon Sep 17 00:00:00 2001 From: Gerben Oolbekkink Date: Fri, 11 Jan 2019 20:34:27 +0100 Subject: [PATCH] Fix tests, improve coverage --- src/Entity/DynamicEntity.php | 36 ---------------- .../PersistentEntityComputedAttributeTest.php | 5 +-- tests/Persistence/DatabaseTest.php | 41 ++++++++++++++++++- tests/Persistence/QueryBuilderTest.php | 5 +++ 4 files changed, 45 insertions(+), 42 deletions(-) diff --git a/src/Entity/DynamicEntity.php b/src/Entity/DynamicEntity.php index 1857ea6..9a50eb2 100644 --- a/src/Entity/DynamicEntity.php +++ b/src/Entity/DynamicEntity.php @@ -69,40 +69,4 @@ public function getAttributeDefinition($attribute_name) { public function getPrimaryKey() { return array_values($this->definition->primary_key); } - - /** - * @param string $attribute - * @param mixed $value - */ - public function __set($attribute, $value) { - $this->$attribute = $value; - } - - /** - * @param string $attribute - * @return mixed - */ - public function __get($attribute) { - if (property_exists(get_class($this), $attribute)) { - return $this->$attribute; - } - return null; - } - - /** - * @param string $attribute - * @return bool - */ - public function __isset($attribute) { - return $this->__get($attribute) !== null; - } - - /** - * @param string $attribute - */ - public function __unset($attribute) { - if ($this->__isset($attribute)) { - unset($this->$attribute); - } - } } diff --git a/tests/Entity/PersistentEntityComputedAttributeTest.php b/tests/Entity/PersistentEntityComputedAttributeTest.php index 50b051e..ca1e88d 100644 --- a/tests/Entity/PersistentEntityComputedAttributeTest.php +++ b/tests/Entity/PersistentEntityComputedAttributeTest.php @@ -47,10 +47,7 @@ public function testComputedJsonSerialize() { public function testWrongComputedAttribute() { $entity = new MyComputedAttributeEntity(); - $this->expectExceptionMessage('Attribute not found: wrong_attribute'); - $this->expectException(\CsrDelft\Orm\Exception\CsrOrmException::class); - - $entity->wrong_attribute; + $this->assertEquals(null, $entity->wrong_attribute); } public function testGetComputedAttributes() { diff --git a/tests/Persistence/DatabaseTest.php b/tests/Persistence/DatabaseTest.php index 56cff14..7787336 100644 --- a/tests/Persistence/DatabaseTest.php +++ b/tests/Persistence/DatabaseTest.php @@ -1,4 +1,6 @@ -createFlatXMLDataSet(__DIR__ . '/../resources/DatabaseTest.xml'); } + /** + * @throws CsrOrmException + */ public function testSqlSelect() { $database = new Database($this->getConnection()->getConnection()); @@ -41,6 +46,9 @@ public function testSqlInsert() { $this->assertEquals(['user' => 'John Doe'], $dataset->getRow($dataset->getRowCount() - 1)); } + /** + * @throws CsrOrmException + */ public function testSqlExists() { $database = new Database($this->getConnection()->getConnection()); @@ -67,6 +75,35 @@ public function testSqlUpdate() { $this->assertEquals(['user' => 'pete'], $dataset->getRow(0)); } + /** + * @throws CsrOrmException + * @throws Exception + */ + public function testInsertMultiple() { + $database = new Database($this->getConnection()->getConnection()); + $dataset = $this->getConnection()->createQueryTable('guestbook', 'SELECT user FROM guestbook'); + + $database->sqlInsertMultiple('guestbook', [['user', 'id'], ['pete', 3], ['jan', 4]]); + + $this->assertEquals(4, $dataset->getRowCount()); + } + + /** + * @throws CsrOrmException + * @throws Exception + */ + public function testInsertMultipleReplace() { + $database = new Database($this->getConnection()->getConnection()); + $dataset = $this->getConnection()->createQueryTable('guestbook', 'SELECT user FROM guestbook'); + + $database->sqlInsertMultiple('guestbook', [['user', 'id'], ['pete', 1], ['jan', 4]], true); + + $this->assertEquals(3, $dataset->getRowCount()); + } + + /** + * @throws CsrOrmException + */ public function testSqlDelete() { $database = new Database($this->getConnection()->getConnection()); $dataset = $this->getConnection()->createQueryTable('guestbook', 'SELECT * FROM guestbook'); @@ -115,7 +152,7 @@ public function testTransactionRollback() { throw new MyException("testException"); }); $this->fail("Exception expected"); - } catch (MyException $ex) { + } /** @noinspection PhpRedundantCatchClauseInspection */ catch (MyException $ex) { $this->assertEquals("testException", $ex->getMessage(), 'Exception is rethrown'); } diff --git a/tests/Persistence/QueryBuilderTest.php b/tests/Persistence/QueryBuilderTest.php index 228ad04..4bcac1b 100644 --- a/tests/Persistence/QueryBuilderTest.php +++ b/tests/Persistence/QueryBuilderTest.php @@ -192,5 +192,10 @@ public function testInterpolateQuery() { ); } + public function testBuildEnum() { + $query_builder = new QueryBuilder(); + + $this->assertEquals("enum('a','b')", $query_builder->buildEnum(['a', 'b'])); + } }