szurubooru/tests/Dao/PostDaoTest.php

226 lines
5.5 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-21 09:35:43 +02:00
private $tagDao;
private $fileServiceMock;
private $thumbnailServiceMock;
public function setUp()
{
parent::setUp();
2014-09-21 09:35:43 +02:00
$this->tagDao = new \Szurubooru\Dao\TagDao($this->databaseConnection);
$this->fileServiceMock = $this->mock(\Szurubooru\Services\FileService::class);
$this->thumbnailServiceMock = $this->mock(\Szurubooru\Services\ThumbnailService::class);
}
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
$expected = [
$post1->getId() => $post1,
$post2->getId() => $post2,
];
2014-09-21 09:35:43 +02:00
$this->assertEntitiesEqual($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-21 09:35:43 +02:00
$this->assertEntitiesEqual($post1, $actualPost1);
$this->assertEntitiesEqual($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());
2014-09-21 09:35:43 +02:00
$this->assertNull($actualPost1);
$this->assertNull($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());
2014-09-21 09:35:43 +02:00
$this->assertNull($actualPost1);
$this->assertEntitiesEqual($actualPost2, $actualPost2);
$this->assertEquals(1, count($postDao->findAll()));
}
2014-09-15 11:38:24 +02:00
public function testSavingTags()
{
2014-09-21 09:35:43 +02:00
$tag1 = new \Szurubooru\Entities\Tag();
$tag1->setName('tag1');
$tag2 = new \Szurubooru\Entities\Tag();
$tag2->setName('tag2');
$testTags = ['tag1' => $tag1, 'tag2' => $tag2];
2014-09-15 11:38:24 +02:00
$postDao = $this->getPostDao();
$post = $this->getPost();
$post->setTags($testTags);
$postDao->save($post);
$savedPost = $postDao->findById($post->getId());
2014-09-21 09:35:43 +02:00
$this->assertEntitiesEqual($testTags, $post->getTags());
$this->assertEquals(2, count($savedPost->getTags()));
2014-09-15 11:38:24 +02:00
$tagDao = $this->getTagDao();
$this->assertEquals(2, count($tagDao->findAll()));
}
public function testNotLoadingContentForNewPosts()
{
$postDao = $this->getPostDao();
$newlyCreatedPost = $this->getPost();
$this->assertNull($newlyCreatedPost->getContent());
}
public function testLoadingContentPostsForExistingPosts()
{
$postDao = $this->getPostDao();
$post = $this->getPost();
$postDao->save($post);
$post = $postDao->findById($post->getId());
$this->fileServiceMock
->expects($this->once())
->method('load')
->with($post->getContentPath())
->willReturn('whatever');
$this->assertEquals('whatever', $post->getContent());
}
public function testSavingContent()
{
$postDao = $this->getPostDao();
$post = $this->getPost();
$post->setContent('whatever');
$this->thumbnailServiceMock
->expects($this->exactly(2))
->method('deleteUsedThumbnails')
->withConsecutive(
[$post->getContentPath()],
[$post->getThumbnailSourceContentPath()]);
$this->fileServiceMock
->expects($this->once())
->method('save')
->with($post->getContentPath(), 'whatever');
$postDao->save($post);
}
public function testSavingContentAndThumbnail()
{
$postDao = $this->getPostDao();
$post = $this->getPost();
$post->setContent('whatever');
$post->setThumbnailSourceContent('an image of sharks');
$this->thumbnailServiceMock
->expects($this->exactly(2))
->method('deleteUsedThumbnails')
->withConsecutive(
[$post->getContentPath()],
[$post->getThumbnailSourceContentPath()]);
$this->fileServiceMock
->expects($this->exactly(2))
->method('save')
->withConsecutive(
[$post->getContentPath(), 'whatever'],
[$post->getThumbnailSourceContentPath(), 'an image of sharks']);
$postDao->save($post);
}
2014-09-15 11:38:24 +02:00
private function getPostDao()
{
return new \Szurubooru\Dao\PostDao(
$this->databaseConnection,
2014-09-21 09:35:43 +02:00
$this->tagDao,
$this->fileServiceMock,
$this->thumbnailServiceMock);
2014-09-15 11:38:24 +02:00
}
private function getTagDao()
{
2014-09-21 09:35:43 +02:00
return $this->tagDao;
2014-09-15 11:38:24 +02:00
}
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;
}
}