diff --git a/run-tests.sh b/run-tests.sh new file mode 100755 index 00000000..ff0959d1 --- /dev/null +++ b/run-tests.sh @@ -0,0 +1,2 @@ +#!/bin/sh +phpunit --strict --bootstrap src/AutoLoader.php tests/ diff --git a/src/AutoLoader.php b/src/AutoLoader.php new file mode 100644 index 00000000..0e2cfbfc --- /dev/null +++ b/src/AutoLoader.php @@ -0,0 +1,24 @@ +db = $mongoDb; + $this->collection = $mongoDb->selectCollection($collectionName); + $this->entityName = $entityName; + } + + public function save(&$entity) + { + $arrayEntity = $this->makeArray($entity); + if ($entity->id) + { + unset ($arrayEntity['_id']); + $this->collection->update(['_id' => new \MongoId($entity->id)], $arrayEntity, ['safe' => true]); + } + else + { + $this->collection->insert($arrayEntity, ['safe' => true]); + } + $entity = $this->makeEntity($arrayEntity); + return $entity; + } + + public function getAll() + { + $entities = []; + foreach ($this->collection->find() as $key => $arrayEntity) + { + $entity = $this->makeEntity($arrayEntity); + $entities[$key] = $entity; + } + return $entities; + } + + public function getById($postId) + { + $arrayEntity = $this->collection->findOne(['_id' => new \MongoId($postId)]); + return $this->makeEntity($arrayEntity); + } + + public function deleteAll() + { + $this->collection->remove(); + } + + public function deleteById($postId) + { + $this->collection->remove(['_id' => new \MongoId($postId)]); + } + + private function makeArray($entity) + { + $arrayEntity = (array) $entity; + if (isset($entity->id)) + { + $arrayEntity['_id'] = $arrayEntity['id']; + unset($arrayEntity['id']); + } + return $arrayEntity; + } + + private function makeEntity($arrayEntity) + { + $entity = \Szurubooru\Helpers\TypeHelper::arrayToClass($arrayEntity, $this->entityName); + if (isset($entity->_id)) + { + $entity->id = (string) $entity->_id; + unset($entity->_id); + } + return $entity; + } +} diff --git a/src/Dao/ICrudDao.php b/src/Dao/ICrudDao.php new file mode 100644 index 00000000..ae3a0c13 --- /dev/null +++ b/src/Dao/ICrudDao.php @@ -0,0 +1,15 @@ +db = $db; + } + + public function prepareForUsage() + { + $this->db->createCollection('posts'); + } + + public function removeAllData() + { + foreach ($this->db->getCollectionNames() as $collectionName) + $this->removeCollectionData($collectionName); + } + + private function removeCollectionData($collectionName) + { + $this->db->$collectionName->remove(); + $this->db->$collectionName->deleteIndexes(); + } +} diff --git a/tests/AbstractDatabaseTest.php b/tests/AbstractDatabaseTest.php new file mode 100644 index 00000000..b6399ed8 --- /dev/null +++ b/tests/AbstractDatabaseTest.php @@ -0,0 +1,26 @@ +connection = new \Mongo($connectingString); + $this->db = $this->connection->selectDb($database); + $this->upgradeService = new \Szurubooru\UpgradeService($this->db); + $this->upgradeService->prepareForUsage(); + } + + public function tearDown() + { + $this->upgradeService->removeAllData(); + } +} diff --git a/tests/Dao/PostDaoTest.php b/tests/Dao/PostDaoTest.php new file mode 100644 index 00000000..cc63fa38 --- /dev/null +++ b/tests/Dao/PostDaoTest.php @@ -0,0 +1,105 @@ +db); + + $post = new \Szurubooru\Entities\Post(); + $post->name = 'test2'; + + $postDao->save($post); + $post->name .= '3'; + $postDao->save($post); + + $otherPost = new \Szurubooru\Entities\Post(); + $otherPost->name = 'yo'; + $postDao->save($otherPost); + + $this->assertEquals('test23', $post->name); + $this->assertEquals('yo', $otherPost->name); + } + + public function testGettingAll() + { + $postDao = new \Szurubooru\Dao\PostDao($this->db); + + $post1 = new \Szurubooru\Entities\Post(); + $post1->name = 'test2'; + $post2 = new \Szurubooru\Entities\Post(); + $post2->name = 'test2'; + + $postDao->save($post1); + $postDao->save($post2); + + $actual = $postDao->getAll(); + $expected = [ + $post1->id => $post1, + $post2->id => $post2, + ]; + + $this->assertEquals($expected, $actual); + } + + public function testGettingById() + { + $postDao = new \Szurubooru\Dao\PostDao($this->db); + + $post1 = new \Szurubooru\Entities\Post(); + $post1->name = 'test2'; + $post2 = new \Szurubooru\Entities\Post(); + $post2->name = 'test2'; + + $postDao->save($post1); + $postDao->save($post2); + + $actualPost1 = $postDao->getById($post1->id); + $actualPost2 = $postDao->getById($post2->id); + $this->assertEquals($post1, $actualPost1); + $this->assertEquals($post2, $actualPost2); + } + + public function testDeletingAll() + { + $postDao = new \Szurubooru\Dao\PostDao($this->db); + + $post1 = new \Szurubooru\Entities\Post(); + $post1->name = 'test2'; + $post2 = new \Szurubooru\Entities\Post(); + $post2->name = 'test2'; + + $postDao->save($post1); + $postDao->save($post2); + + $postDao->deleteAll(); + + $actualPost1 = $postDao->getById($post1->id); + $actualPost2 = $postDao->getById($post2->id); + $this->assertEquals(null, $actualPost1); + $this->assertEquals(null, $actualPost2); + $this->assertEquals(0, count($postDao->getAll())); + } + + public function testDeletingById() + { + $postDao = new \Szurubooru\Dao\PostDao($this->db); + + $post1 = new \Szurubooru\Entities\Post(); + $post1->name = 'test2'; + $post2 = new \Szurubooru\Entities\Post(); + $post2->name = 'test2'; + + $postDao->save($post1); + $postDao->save($post2); + + $postDao->deleteById($post1->id); + + $actualPost1 = $postDao->getById($post1->id); + $actualPost2 = $postDao->getById($post2->id); + $this->assertEquals(null, $actualPost1); + $this->assertEquals($actualPost2, $actualPost2); + $this->assertEquals(1, count($postDao->getAll())); + } +}