Added post note DAO

This commit is contained in:
Marcin Kurczewski 2014-10-25 12:27:04 +02:00
parent ccca71c1b8
commit e03ed35862
6 changed files with 175 additions and 0 deletions

View file

@ -0,0 +1,33 @@
<?php
namespace Szurubooru\Dao\EntityConverters;
use Szurubooru\Entities\Entity;
use Szurubooru\Entities\PostNote;
class PostNoteEntityConverter extends AbstractEntityConverter implements IEntityConverter
{
public function toArray(Entity $entity)
{
return
[
'id' => $entity->getId(),
'postId' => $entity->getPostId(),
'x' => $entity->getLeft(),
'y' => $entity->getTop(),
'width' => $entity->getWidth(),
'height' => $entity->getHeight(),
'text' => $entity->getText(),
];
}
public function toBasicEntity(array $array)
{
$entity = new PostNote($array['id']);
$entity->setPostId($array['postId']);
$entity->setLeft($array['x']);
$entity->setTop($array['y']);
$entity->setWidth($array['width']);
$entity->setHeight($array['height']);
$entity->setText($array['text']);
return $entity;
}
}

15
src/Dao/PostNoteDao.php Normal file
View file

@ -0,0 +1,15 @@
<?php
namespace Szurubooru\Dao;
use Szurubooru\Dao\EntityConverters\PostNoteEntityConverter;
use Szurubooru\DatabaseConnection;
class PostNoteDao extends AbstractDao implements ICrudDao
{
public function __construct(DatabaseConnection $databaseConnection)
{
parent::__construct(
$databaseConnection,
'postNotes',
new PostNoteEntityConverter());
}
}

72
src/Entities/PostNote.php Normal file
View file

@ -0,0 +1,72 @@
<?php
namespace Szurubooru\Entities;
final class PostNote extends Entity
{
private $postId;
private $left;
private $top;
private $width;
private $height;
private $text;
public function getPostId()
{
return $this->postId;
}
public function setPostId($postId)
{
$this->postId = $postId;
}
public function getLeft()
{
return $this->left;
}
public function setLeft($left)
{
$this->left = $left;
}
public function getTop()
{
return $this->top;
}
public function setTop($top)
{
$this->top = $top;
}
public function getWidth()
{
return $this->width;
}
public function setWidth($width)
{
$this->width = $width;
}
public function getHeight()
{
return $this->height;
}
public function setHeight($height)
{
$this->height = $height;
}
public function getText()
{
return $this->text;
}
public function setText($text)
{
$this->text = $text;
}
}

View file

@ -0,0 +1,23 @@
<?php
namespace Szurubooru\Upgrades;
use Szurubooru\DatabaseConnection;
class Upgrade27 implements IUpgrade
{
public function run(DatabaseConnection $databaseConnection)
{
$pdo = $databaseConnection->getPDO();
$driver = $databaseConnection->getDriver();
$pdo->exec(
'CREATE TABLE postNotes (
id INTEGER PRIMARY KEY ' . ($driver === 'mysql' ? 'AUTO_INCREMENT' : 'AUTOINCREMENT') . ',
postId INTEGER NOT NULL,
x INTEGER NOT NULL,
y INTEGER NOT NULL,
height INTEGER NOT NULL,
width INTEGER NOT NULL,
text TEXT NOT NULL
)');
}
}

View file

@ -42,6 +42,7 @@ return [
$container->get(\Szurubooru\Upgrades\Upgrade24::class), $container->get(\Szurubooru\Upgrades\Upgrade24::class),
$container->get(\Szurubooru\Upgrades\Upgrade25::class), $container->get(\Szurubooru\Upgrades\Upgrade25::class),
$container->get(\Szurubooru\Upgrades\Upgrade26::class), $container->get(\Szurubooru\Upgrades\Upgrade26::class),
$container->get(\Szurubooru\Upgrades\Upgrade27::class),
]; ];
}), }),

View file

@ -0,0 +1,31 @@
<?php
namespace Szurubooru\Tests\Dao;
use Szurubooru\Dao\PostNoteDao;
use Szurubooru\Entities\PostNote;
use Szurubooru\Tests\AbstractDatabaseTestCase;
final class PostNoteDaoTest extends AbstractDatabaseTestCase
{
public function testSettingValues()
{
$expected = new PostNote();
$expected->setPostId(5);
$expected->setLeft(5);
$expected->setTop(10);
$expected->setWidth(50);
$expected->setHeight(50);
$expected->setText('text');
$postNoteDao = $this->getPostNoteDao();
$postNoteDao->save($expected);
$actual = $postNoteDao->findById($expected->getId());
$this->assertEntitiesEqual($actual, $expected);
}
private function getPostNoteDao()
{
return new PostNoteDao($this->databaseConnection);
}
}