2014-08-28 10:45:55 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru\Tests\Dao;
|
|
|
|
|
2014-09-01 20:51:59 +02:00
|
|
|
final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
|
2014-08-28 10:45:55 +02:00
|
|
|
{
|
2014-09-10 19:16:06 +02:00
|
|
|
public function testCreating()
|
2014-08-28 10:45:55 +02:00
|
|
|
{
|
2014-08-30 17:10:45 +02:00
|
|
|
$postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection);
|
2014-08-28 10:45:55 +02:00
|
|
|
|
|
|
|
$post = new \Szurubooru\Entities\Post();
|
2014-09-13 23:58:13 +02:00
|
|
|
$post->setName('test');
|
2014-09-10 19:16:06 +02:00
|
|
|
$savedPost = $postDao->save($post);
|
2014-09-13 23:58:13 +02:00
|
|
|
$this->assertEquals('test', $post->getName());
|
|
|
|
$this->assertNotNull($savedPost->getId());
|
2014-09-10 19:16:06 +02:00
|
|
|
}
|
2014-08-28 10:45:55 +02:00
|
|
|
|
2014-09-10 19:16:06 +02:00
|
|
|
public function testUpdating()
|
|
|
|
{
|
|
|
|
$postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection);
|
|
|
|
$post = new \Szurubooru\Entities\Post();
|
2014-09-13 23:58:13 +02:00
|
|
|
$post->setName('test');
|
2014-09-10 19:16:06 +02:00
|
|
|
$post = $postDao->save($post);
|
2014-09-13 23:58:13 +02:00
|
|
|
$this->assertEquals('test', $post->getName());
|
|
|
|
$id = $post->getId();
|
|
|
|
$post->setName($post->getName() . '2');
|
2014-09-10 19:16:06 +02:00
|
|
|
$post = $postDao->save($post);
|
2014-09-13 23:58:13 +02:00
|
|
|
$this->assertEquals('test2', $post->getName());
|
|
|
|
$this->assertEquals($id, $post->getId());
|
2014-08-28 10:45:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGettingAll()
|
|
|
|
{
|
2014-08-30 17:10:45 +02:00
|
|
|
$postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection);
|
2014-08-28 10:45:55 +02:00
|
|
|
|
|
|
|
$post1 = new \Szurubooru\Entities\Post();
|
2014-09-13 23:58:13 +02:00
|
|
|
$post1->setName('test2');
|
2014-08-28 10:45:55 +02:00
|
|
|
$post2 = new \Szurubooru\Entities\Post();
|
2014-09-13 23:58:13 +02:00
|
|
|
$post2->setName('test2');
|
2014-08-28 10:45:55 +02:00
|
|
|
|
|
|
|
$postDao->save($post1);
|
|
|
|
$postDao->save($post2);
|
|
|
|
|
2014-09-13 23:58:13 +02:00
|
|
|
$actual = $postDao->findAll();
|
2014-08-28 10:45:55 +02:00
|
|
|
$expected = [
|
2014-09-13 23:58:13 +02:00
|
|
|
$post1->getId() => $post1,
|
|
|
|
$post2->getId() => $post2,
|
2014-08-28 10:45:55 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGettingById()
|
|
|
|
{
|
2014-08-30 17:10:45 +02:00
|
|
|
$postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection);
|
2014-08-28 10:45:55 +02:00
|
|
|
|
|
|
|
$post1 = new \Szurubooru\Entities\Post();
|
2014-09-13 23:58:13 +02:00
|
|
|
$post1->setName('test2');
|
2014-08-28 10:45:55 +02:00
|
|
|
$post2 = new \Szurubooru\Entities\Post();
|
2014-09-13 23:58:13 +02:00
|
|
|
$post2->setName('test2');
|
2014-08-28 10:45:55 +02:00
|
|
|
|
|
|
|
$postDao->save($post1);
|
|
|
|
$postDao->save($post2);
|
|
|
|
|
2014-09-13 23:58:13 +02:00
|
|
|
$actualPost1 = $postDao->findById($post1->getId());
|
|
|
|
$actualPost2 = $postDao->findById($post2->getId());
|
2014-08-28 10:45:55 +02:00
|
|
|
$this->assertEquals($post1, $actualPost1);
|
|
|
|
$this->assertEquals($post2, $actualPost2);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDeletingAll()
|
|
|
|
{
|
2014-08-30 17:10:45 +02:00
|
|
|
$postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection);
|
2014-08-28 10:45:55 +02:00
|
|
|
|
|
|
|
$post1 = new \Szurubooru\Entities\Post();
|
2014-09-13 23:58:13 +02:00
|
|
|
$post1->setName('test2');
|
2014-08-28 10:45:55 +02:00
|
|
|
$post2 = new \Szurubooru\Entities\Post();
|
2014-09-13 23:58:13 +02:00
|
|
|
$post2->setName('test2');
|
2014-08-28 10:45:55 +02:00
|
|
|
|
|
|
|
$postDao->save($post1);
|
|
|
|
$postDao->save($post2);
|
|
|
|
|
|
|
|
$postDao->deleteAll();
|
|
|
|
|
2014-09-13 23:58:13 +02:00
|
|
|
$actualPost1 = $postDao->findById($post1->getId());
|
|
|
|
$actualPost2 = $postDao->findById($post2->getId());
|
2014-08-28 10:45:55 +02:00
|
|
|
$this->assertEquals(null, $actualPost1);
|
|
|
|
$this->assertEquals(null, $actualPost2);
|
2014-09-13 23:58:13 +02:00
|
|
|
$this->assertEquals(0, count($postDao->findAll()));
|
2014-08-28 10:45:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDeletingById()
|
|
|
|
{
|
2014-08-30 17:10:45 +02:00
|
|
|
$postDao = new \Szurubooru\Dao\PostDao($this->databaseConnection);
|
2014-08-28 10:45:55 +02:00
|
|
|
|
|
|
|
$post1 = new \Szurubooru\Entities\Post();
|
2014-09-13 23:58:13 +02:00
|
|
|
$post1->setName('test2');
|
2014-08-28 10:45:55 +02:00
|
|
|
$post2 = new \Szurubooru\Entities\Post();
|
2014-09-13 23:58:13 +02:00
|
|
|
$post2->setName('test2');
|
2014-08-28 10:45:55 +02:00
|
|
|
|
|
|
|
$postDao->save($post1);
|
|
|
|
$postDao->save($post2);
|
|
|
|
|
2014-09-13 23:58:13 +02:00
|
|
|
$postDao->deleteById($post1->getId());
|
2014-08-28 10:45:55 +02:00
|
|
|
|
2014-09-13 23:58:13 +02:00
|
|
|
$actualPost1 = $postDao->findById($post1->getId());
|
|
|
|
$actualPost2 = $postDao->findById($post2->getId());
|
2014-08-28 10:45:55 +02:00
|
|
|
$this->assertEquals(null, $actualPost1);
|
|
|
|
$this->assertEquals($actualPost2, $actualPost2);
|
2014-09-13 23:58:13 +02:00
|
|
|
$this->assertEquals(1, count($postDao->findAll()));
|
2014-08-28 10:45:55 +02:00
|
|
|
}
|
|
|
|
}
|