Added proof of concept for Dao system
Fixup first commit
This commit is contained in:
parent
2527929f0b
commit
b7de8ae0bf
12 changed files with 334 additions and 0 deletions
2
run-tests.sh
Executable file
2
run-tests.sh
Executable file
|
@ -0,0 +1,2 @@
|
|||
#!/bin/sh
|
||||
phpunit --strict --bootstrap src/AutoLoader.php tests/
|
24
src/AutoLoader.php
Normal file
24
src/AutoLoader.php
Normal file
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
namespace Szurubooru;
|
||||
|
||||
final class AutoLoader
|
||||
{
|
||||
public static function init()
|
||||
{
|
||||
spl_autoload_register([__CLASS__, '_include']);
|
||||
}
|
||||
|
||||
public static function _include($className)
|
||||
{
|
||||
$className = str_replace('\\', DIRECTORY_SEPARATOR, $className);
|
||||
$className = str_replace('Szurubooru', '', $className);
|
||||
if (strpos($className, 'Tests') !== false)
|
||||
$className = dirname(__DIR__) . DIRECTORY_SEPARATOR . str_replace('Tests', 'tests', $className);
|
||||
else
|
||||
$className = __DIR__ . DIRECTORY_SEPARATOR . $className;
|
||||
$className .= '.php';
|
||||
include $className;
|
||||
}
|
||||
}
|
||||
|
||||
AutoLoader::init();
|
81
src/Dao/AbstractDao.php
Normal file
81
src/Dao/AbstractDao.php
Normal file
|
@ -0,0 +1,81 @@
|
|||
<?php
|
||||
namespace Szurubooru\Dao;
|
||||
|
||||
abstract class AbstractDao implements ICrudDao
|
||||
{
|
||||
protected $db;
|
||||
protected $collection;
|
||||
protected $entityName;
|
||||
|
||||
public function __construct(\MongoDB $mongoDb, $collectionName, $entityName)
|
||||
{
|
||||
$this->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;
|
||||
}
|
||||
}
|
15
src/Dao/ICrudDao.php
Normal file
15
src/Dao/ICrudDao.php
Normal file
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
namespace Szurubooru\Dao;
|
||||
|
||||
interface ICrudDao
|
||||
{
|
||||
public function getAll();
|
||||
|
||||
public function getById($objectId);
|
||||
|
||||
public function save(&$object);
|
||||
|
||||
public function deleteById($objectId);
|
||||
|
||||
public function deleteAll();
|
||||
}
|
10
src/Dao/PostDao.php
Normal file
10
src/Dao/PostDao.php
Normal file
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
namespace Szurubooru\Dao;
|
||||
|
||||
final class PostDao extends AbstractDao implements ICrudDao
|
||||
{
|
||||
public function __construct(\MongoDB $mongoDb)
|
||||
{
|
||||
parent::__construct($mongoDb, 'posts', '\Szurubooru\Entities\Post');
|
||||
}
|
||||
}
|
7
src/Entities/Entity.php
Normal file
7
src/Entities/Entity.php
Normal file
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
namespace Szurubooru\Entities;
|
||||
|
||||
abstract class Entity
|
||||
{
|
||||
public $id = null;
|
||||
}
|
7
src/Entities/Post.php
Normal file
7
src/Entities/Post.php
Normal file
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
namespace Szurubooru\Entities;
|
||||
|
||||
final class Post extends Entity
|
||||
{
|
||||
public $name;
|
||||
}
|
8
src/Entities/Tag.php
Normal file
8
src/Entities/Tag.php
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?php
|
||||
namespace Szurubooru\Entities;
|
||||
|
||||
final class Tag extends Entity
|
||||
{
|
||||
public $name;
|
||||
public $usages;
|
||||
}
|
20
src/Helpers/TypeHelper.php
Normal file
20
src/Helpers/TypeHelper.php
Normal file
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
namespace Szurubooru\Helpers;
|
||||
|
||||
final class TypeHelper
|
||||
{
|
||||
public static function arrayToClass($array, $className)
|
||||
{
|
||||
if (!$array)
|
||||
return null;
|
||||
|
||||
if (!class_exists($className, true))
|
||||
return null;
|
||||
|
||||
return unserialize(
|
||||
preg_replace(
|
||||
'/^O:[0-9]+:"[^"]+":/i',
|
||||
'O:' . strlen($className) . ':"' . $className . '":',
|
||||
serialize((object) $array)));
|
||||
}
|
||||
}
|
29
src/UpgradeService.php
Normal file
29
src/UpgradeService.php
Normal file
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
namespace Szurubooru;
|
||||
|
||||
final class UpgradeService
|
||||
{
|
||||
private $db;
|
||||
|
||||
public function __construct(\MongoDB $db)
|
||||
{
|
||||
$this->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();
|
||||
}
|
||||
}
|
26
tests/AbstractDatabaseTest.php
Normal file
26
tests/AbstractDatabaseTest.php
Normal file
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
namespace Szurubooru\Tests;
|
||||
|
||||
abstract class AbstractDatabaseTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $db;
|
||||
protected $connection;
|
||||
protected $upgradeService;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$host = 'localhost';
|
||||
$port = 27017;
|
||||
$database = 'test';
|
||||
$connectingString = sprintf('mongodb://%s:%d/%s', $host, $port, $database);
|
||||
$this->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();
|
||||
}
|
||||
}
|
105
tests/Dao/PostDaoTest.php
Normal file
105
tests/Dao/PostDaoTest.php
Normal file
|
@ -0,0 +1,105 @@
|
|||
<?php
|
||||
namespace Szurubooru\Tests\Dao;
|
||||
|
||||
final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTest
|
||||
{
|
||||
public function testSaving()
|
||||
{
|
||||
$postDao = new \Szurubooru\Dao\PostDao($this->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()));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue