szurubooru/tests/Dao/PostDaoTest.php

140 lines
3.4 KiB
PHP
Raw Normal View History

<?php
namespace Szurubooru\Tests\Dao;
2014-09-01 20:51:59 +02:00
final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
{
2014-09-10 19:16:06 +02:00
public function testCreating()
{
2014-09-15 11:38:24 +02:00
$postDao = $this->getPostDao();
2014-09-15 11:38:24 +02:00
$post = $this->getPost();
2014-09-10 19:16:06 +02:00
$savedPost = $postDao->save($post);
$this->assertEquals('test', $post->getName());
$this->assertNotNull($savedPost->getId());
2014-09-10 19:16:06 +02:00
}
2014-09-10 19:16:06 +02:00
public function testUpdating()
{
2014-09-15 11:38:24 +02:00
$postDao = $this->getPostDao();
$post = $this->getPost();
2014-09-10 19:16:06 +02:00
$post = $postDao->save($post);
$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);
$this->assertEquals('test2', $post->getName());
$this->assertEquals($id, $post->getId());
}
public function testGettingAll()
{
2014-09-15 11:38:24 +02:00
$postDao = $this->getPostDao();
2014-09-15 11:38:24 +02:00
$post1 = $this->getPost();
$post2 = $this->getPost();
$postDao->save($post1);
$postDao->save($post2);
$actual = $postDao->findAll();
2014-09-15 11:38:24 +02:00
foreach ($actual as $post)
$post->resetLazyLoaders();
$expected = [
$post1->getId() => $post1,
$post2->getId() => $post2,
];
$this->assertEquals($expected, $actual);
}
public function testGettingById()
{
2014-09-15 11:38:24 +02:00
$postDao = $this->getPostDao();
2014-09-15 11:38:24 +02:00
$post1 = $this->getPost();
$post2 = $this->getPost();
$postDao->save($post1);
$postDao->save($post2);
$actualPost1 = $postDao->findById($post1->getId());
$actualPost2 = $postDao->findById($post2->getId());
2014-09-15 11:38:24 +02:00
$actualPost1->resetLazyLoaders();
$actualPost2->resetLazyLoaders();
$this->assertEquals($post1, $actualPost1);
$this->assertEquals($post2, $actualPost2);
}
public function testDeletingAll()
{
2014-09-15 11:38:24 +02:00
$postDao = $this->getPostDao();
2014-09-15 11:38:24 +02:00
$post1 = $this->getPost();
$post2 = $this->getPost();
$postDao->save($post1);
$postDao->save($post2);
$postDao->deleteAll();
$actualPost1 = $postDao->findById($post1->getId());
$actualPost2 = $postDao->findById($post2->getId());
$this->assertEquals(null, $actualPost1);
$this->assertEquals(null, $actualPost2);
$this->assertEquals(0, count($postDao->findAll()));
}
public function testDeletingById()
{
2014-09-15 11:38:24 +02:00
$postDao = $this->getPostDao();
2014-09-15 11:38:24 +02:00
$post1 = $this->getPost();
$post2 = $this->getPost();
$postDao->save($post1);
$postDao->save($post2);
$postDao->deleteById($post1->getId());
$actualPost1 = $postDao->findById($post1->getId());
$actualPost2 = $postDao->findById($post2->getId());
$this->assertEquals(null, $actualPost1);
$this->assertEquals($actualPost2, $actualPost2);
$this->assertEquals(1, count($postDao->findAll()));
}
2014-09-15 11:38:24 +02:00
public function testSavingTags()
{
$testTags = ['tag1', 'tag2'];
$postDao = $this->getPostDao();
$post = $this->getPost();
$post->setTags($testTags);
$postDao->save($post);
$savedPost = $postDao->findById($post->getId());
$this->assertEquals($testTags, $post->getTags());
$this->assertEquals($post->getTags(), $savedPost->getTags());
$tagDao = $this->getTagDao();
$this->assertEquals(2, count($tagDao->findAll()));
}
private function getPostDao()
{
return new \Szurubooru\Dao\PostDao($this->databaseConnection);
}
private function getTagDao()
{
return new \Szurubooru\Dao\TagDao($this->databaseConnection);
}
private function getPost()
{
$post = new \Szurubooru\Entities\Post();
$post->setName('test');
$post->setUploadTime('whatever');
$post->setSafety(\Szurubooru\Entities\Post::POST_SAFETY_SAFE);
$post->setContentType(\Szurubooru\Entities\Post::POST_TYPE_YOUTUBE);
$post->setContentChecksum('whatever');
return $post;
}
}