diff options
author | Sean Molenaar <sean@seanmolenaar.eu> | 2020-11-14 00:09:38 +0100 |
---|---|---|
committer | Sean Molenaar <SMillerDev@users.noreply.github.com> | 2020-12-08 13:58:12 +0100 |
commit | 8abddeab4f541883721d912f97dec07bffdfc6b8 (patch) | |
tree | 8c176f9ca2fd9e757807481997f265212f154eb2 /tests/Unit/Db | |
parent | 3d98707be95322d16f5883d7a945d658d6316146 (diff) |
Remove usage of old Folder code
Signed-off-by: Sean Molenaar <sean@seanmolenaar.eu>
Diffstat (limited to 'tests/Unit/Db')
-rw-r--r-- | tests/Unit/Db/FolderMapperTest.php | 519 | ||||
-rw-r--r-- | tests/Unit/Db/MapperTestUtility.php | 19 |
2 files changed, 223 insertions, 315 deletions
diff --git a/tests/Unit/Db/FolderMapperTest.php b/tests/Unit/Db/FolderMapperTest.php index 803c84ebe..ab2fb3995 100644 --- a/tests/Unit/Db/FolderMapperTest.php +++ b/tests/Unit/Db/FolderMapperTest.php @@ -14,25 +14,23 @@ namespace OCA\News\Tests\Unit\Db; use OCA\News\Db\Folder; -use OCA\News\Db\FolderMapper; +use OCA\News\Db\FolderMapperV2; use OCA\News\Utility\Time; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Db\MultipleObjectsReturnedException; class FolderMapperTest extends MapperTestUtility { - /** @var FolderMapper */ - private $folderMapper; + /** @var FolderMapperV2 */ + private $class; /** @var Folder[] */ private $folders; - /** @var string */ - private $user; protected function setUp(): void { parent::setUp(); - $this->folderMapper = new FolderMapper($this->db, new Time()); + $this->class = new FolderMapperV2($this->db, new Time()); // create mock folders $folder1 = new Folder(); @@ -43,351 +41,242 @@ class FolderMapperTest extends MapperTestUtility $folder2->resetUpdatedFields(); $this->folders = [$folder1, $folder2]; - $this->user = 'hh'; - $this->twoRows = [ - ['id' => $this->folders[0]->getId()], - ['id' => $this->folders[1]->getId()] - ]; } /** - * @covers \OCA\News\Db\FolderMapper::find + * @covers \OCA\News\Db\FolderMapperV2::findAllFromUser */ - public function testFind() - { - $userId = 'john'; - $id = 3; - $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . - 'WHERE `id` = ? ' . - 'AND `user_id` = ?'; - - $this->db->expects($this->exactly(1)) - ->method('prepare') - ->with($sql, null, null) - ->will(($this->returnValue($this->query))); - - $this->query->expects($this->exactly(2)) - ->method('fetch') - ->willReturnOnConsecutiveCalls(['id' => 4], false); - - $this->query->expects($this->exactly(2)) - ->method('bindValue') - ->withConsecutive([1, 3, 1], [2, $userId, 2]); - - $this->query->expects($this->exactly(1)) - ->method('closeCursor'); - - $this->query->expects($this->once()) - ->method('execute') - ->with('') - ->will($this->returnValue([])); - - $result = $this->folderMapper->find($userId, $id); - $this->assertEquals($this->folders[0], $result); - } - - - public function testFindNotFound() + public function testFindAllFromUser() { - $userId = 'john'; - $id = 3; - $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . - 'WHERE `id` = ? ' . - 'AND `user_id` = ?'; - - $this->db->expects($this->exactly(1)) - ->method('prepare') - ->with($sql, null, null) - ->will(($this->returnValue($this->query))); - - $this->query->expects($this->exactly(1)) - ->method('fetch') - ->willReturnOnConsecutiveCalls(false); - - $this->query->expects($this->exactly(2)) - ->method('bindValue') - ->withConsecutive([1, $id, 1], [2, $userId, 2]); - - $this->query->expects($this->exactly(1)) - ->method('closeCursor'); - - $this->query->expects($this->once()) - ->method('execute') - ->with('') - ->will($this->returnValue([])); - - $this->expectException(DoesNotExistException::class); - $this->folderMapper->find($userId, $id); + $this->db->expects($this->once()) + ->method('getQueryBuilder') + ->willReturn($this->builder); + + $this->builder->expects($this->once()) + ->method('select') + ->with('*') + ->will($this->returnSelf()); + + $this->builder->expects($this->once()) + ->method('from') + ->with('news_folders') + ->will($this->returnSelf()); + + $this->builder->expects($this->once()) + ->method('where') + ->with('user_id = :user_id') + ->will($this->returnSelf()); + + $this->builder->expects($this->once()) + ->method('andWhere') + ->with('deleted_at = 0') + ->will($this->returnSelf()); + + $this->builder->expects($this->once()) + ->method('setParameter') + ->with(':user_id', 'jack') + ->will($this->returnSelf()); + + $this->builder->expects($this->once()) + ->method('execute') + ->will($this->returnValue($this->cursor)); + + $this->cursor->expects($this->exactly(3)) + ->method('fetch') + ->willReturnOnConsecutiveCalls( + ['id' => 4], + ['id' => 5], + null + ); + + $result = $this->class->findAllFromUser('jack', []); + $this->assertEquals($this->folders, $result); } - - public function testFindMoreThanOneResultFound() + /** + * @covers \OCA\News\Db\FolderMapperV2::findFromUser + */ + public function testFindFromUser() { - $userId = 'john'; - $id = 3; - $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . - 'WHERE `id` = ? ' . - 'AND `user_id` = ?'; - - $this->db->expects($this->exactly(1)) - ->method('prepare') - ->with($sql, null, null) - ->will(($this->returnValue($this->query))); - - $this->query->expects($this->exactly(2)) - ->method('fetch') - ->willReturnOnConsecutiveCalls(['id' => 4], ['id' => 5]); - - $this->query->expects($this->exactly(2)) - ->method('bindValue') - ->withConsecutive([1, 3, 1], [2, $userId, 2]); - - $this->query->expects($this->exactly(1)) - ->method('closeCursor'); - - $this->query->expects($this->once()) - ->method('execute') - ->with('') - ->will($this->returnValue([])); - - $this->expectException(MultipleObjectsReturnedException::class); - $this->folderMapper->find($userId, $id); + $this->db->expects($this->once()) + ->method('getQueryBuilder') + ->willReturn($this->builder); + + $this->builder->expects($this->once()) + ->method('select') + ->with('*') + ->will($this->returnSelf()); + + $this->builder->expects($this->once()) + ->method('from') + ->with('news_folders') + ->will($this->returnSelf()); + + $this->builder->expects($this->once()) + ->method('where') + ->with('user_id = :user_id') + ->will($this->returnSelf()); + + $this->builder->expects($this->exactly(2)) + ->method('andWhere') + ->withConsecutive(['id = :id'], ['deleted_at = 0']) + ->will($this->returnSelf()); + + $this->builder->expects($this->exactly(2)) + ->method('setParameter') + ->withConsecutive([':user_id', 'jack'], [':id', 1]) + ->will($this->returnSelf()); + + $this->builder->expects($this->once()) + ->method('execute') + ->will($this->returnValue($this->cursor)); + + $this->cursor->expects($this->exactly(2)) + ->method('fetch') + ->willReturnOnConsecutiveCalls( + ['id' => 4], + false + ); + + $result = $this->class->findFromUser('jack', 1); + $this->assertEquals($this->folders[0], $result); } - - - public function testFindAllFromUser() + /** + * @covers \OCA\News\Db\FolderMapperV2::findFromUser + */ + public function testFindFromUserEmpty() { - $userId = 'john'; - $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . - 'WHERE `user_id` = ? ' . - 'AND `deleted_at` = 0'; - - $this->db->expects($this->exactly(1)) - ->method('prepare') - ->with($sql, null, null) - ->will(($this->returnValue($this->query))); - - $this->query->expects($this->exactly(3)) - ->method('fetch') - ->willReturnOnConsecutiveCalls(['id' => 4], ['id' => 5]); - - $this->query->expects($this->exactly(1)) - ->method('bindValue') - ->withConsecutive([1, $userId, 2]); - - $this->query->expects($this->exactly(1)) - ->method('closeCursor'); - - $this->query->expects($this->once()) + $this->db->expects($this->once()) + ->method('getQueryBuilder') + ->willReturn($this->builder); + + $this->builder->expects($this->once()) + ->method('select') + ->with('*') + ->will($this->returnSelf()); + + $this->builder->expects($this->once()) + ->method('from') + ->with('news_folders') + ->will($this->returnSelf()); + + $this->builder->expects($this->once()) + ->method('where') + ->with('user_id = :user_id') + ->will($this->returnSelf()); + + $this->builder->expects($this->exactly(2)) + ->method('andWhere') + ->withConsecutive(['id = :id'], ['deleted_at = 0']) + ->will($this->returnSelf()); + + $this->builder->expects($this->exactly(2)) + ->method('setParameter') + ->withConsecutive([':user_id', 'jack'], [':id', 1]) + ->will($this->returnSelf()); + + $this->builder->expects($this->once()) ->method('execute') - ->will($this->returnValue([])); - - $result = $this->folderMapper->findAllFromUser($userId); - $this->assertEquals($this->folders, $result); - } + ->will($this->returnValue($this->cursor)); - - public function testFindByName() - { - $folderName = 'heheh'; - $userId = 'john'; - $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . - 'WHERE `name` = ? ' . - 'AND `user_id` = ?'; - - $this->db->expects($this->exactly(1)) - ->method('prepare') - ->with($sql, null, null) - ->will(($this->returnValue($this->query))); - - $this->query->expects($this->exactly(3)) + $this->cursor->expects($this->exactly(1)) ->method('fetch') - ->willReturnOnConsecutiveCalls(['id' => 4], ['id' => 5]); - - $this->query->expects($this->exactly(2)) - ->method('bindValue') - ->withConsecutive([1, $folderName, 2], [2, $userId, 2]); - - $this->query->expects($this->exactly(1)) - ->method('closeCursor'); - - $this->query->expects($this->once()) - ->method('execute') - ->will($this->returnValue([])); + ->willReturnOnConsecutiveCalls( + false + ); - $result = $this->folderMapper->findByName($folderName, $userId); - $this->assertEquals($this->folders, $result); + $this->expectException(DoesNotExistException::class); + $this->class->findFromUser('jack', 1); } - - public function testDelete() + /** + * @covers \OCA\News\Db\FolderMapperV2::findFromUser + */ + public function testFindFromUserDuplicate() { - $folder = new Folder(); - $folder->setId(3); - $sql = 'DELETE FROM `*PREFIX*news_folders` WHERE `id` = ?'; - $arguments = [$folder->getId()]; + $this->db->expects($this->once()) + ->method('getQueryBuilder') + ->willReturn($this->builder); - $sql2 = 'DELETE FROM `*PREFIX*news_feeds` WHERE `folder_id` = ?'; - $arguments2 = [$folder->getId()]; + $this->builder->expects($this->once()) + ->method('select') + ->with('*') + ->will($this->returnSelf()); - $sql3 = 'DELETE FROM `*PREFIX*news_items` WHERE `feed_id` NOT IN '. - '(SELECT `feeds`.`id` FROM `*PREFIX*news_feeds` `feeds`)'; + $this->builder->expects($this->once()) + ->method('from') + ->with('news_folders') + ->will($this->returnSelf()); - $this->db->expects($this->exactly(3)) - ->method('prepare') - ->withConsecutive( - [$sql, null, null], - [$sql2, null, null], - [$sql3, null, null] - ) - ->will(($this->returnValue($this->query))); + $this->builder->expects($this->once()) + ->method('where') + ->with('user_id = :user_id') + ->will($this->returnSelf()); - $this->query->expects($this->exactly(2)) - ->method('bindValue') - ->withConsecutive([1, 3, 1]); + $this->builder->expects($this->exactly(2)) + ->method('andWhere') + ->withConsecutive(['id = :id'], ['deleted_at = 0']) + ->will($this->returnSelf()); - $this->query->expects($this->exactly(3)) - ->method('closeCursor'); + $this->builder->expects($this->exactly(2)) + ->method('setParameter') + ->withConsecutive([':user_id', 'jack'], [':id', 1]) + ->will($this->returnSelf()); - $this->query->expects($this->exactly(3)) + $this->builder->expects($this->once()) ->method('execute') - ->will($this->returnValue([])); + ->will($this->returnValue($this->cursor)); - $this->folderMapper->delete($folder); - } - - - public function testGetPurgeDeleted() - { - $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . - 'WHERE `deleted_at` > 0 ' . - 'AND `deleted_at` < ? '; - - - $this->db->expects($this->exactly(1)) - ->method('prepare') - ->with($sql, null, null) - ->will(($this->returnValue($this->query))); - - $this->query->expects($this->exactly(3)) + $this->cursor->expects($this->exactly(2)) ->method('fetch') - ->willReturnOnConsecutiveCalls(['id' => 4], ['id' => 5]); - - $this->query->expects($this->exactly(1)) - ->method('bindValue') - ->withConsecutive([1, 110, 1]); + ->willReturnOnConsecutiveCalls( + ['id' => 1], + ['id' => 2] + ); - $this->query->expects($this->exactly(1)) - ->method('closeCursor'); - - $this->query->expects($this->once()) - ->method('execute') - ->will($this->returnValue([])); - - $result = $this->folderMapper->getToDelete(110); - - $this->assertEquals($this->folders, $result); + $this->expectException(MultipleObjectsReturnedException::class); + $this->class->findFromUser('jack', 1); } - - - public function testGetPurgeDeletedUser() + /** + * @covers \OCA\News\Db\FolderMapperV2::findAll + */ + public function testFindAll() { - $deleteOlderThan = 110; - $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . - 'WHERE `deleted_at` > 0 ' . - 'AND `deleted_at` < ? ' . - 'AND `user_id` = ?'; - - - $this->db->expects($this->exactly(1)) - ->method('prepare') - ->with($sql, null, null) - ->will(($this->returnValue($this->query))); - - $this->query->expects($this->exactly(3)) - ->method('fetch') - ->willReturnOnConsecutiveCalls(['id' => 4], ['id' => 5]); - - $this->query->expects($this->exactly(2)) - ->method('bindValue') - ->withConsecutive([1, 110, 1], [2, 'hh', 2]); - - $this->query->expects($this->exactly(1)) - ->method('closeCursor'); - - $this->query->expects($this->once()) + $this->db->expects($this->once()) + ->method('getQueryBuilder') + ->willReturn($this->builder); + + $this->builder->expects($this->once()) + ->method('select') + ->with('*') + ->will($this->returnSelf()); + + $this->builder->expects($this->once()) + ->method('from') + ->with('news_folders') + ->will($this->returnSelf()); + + $this->builder->expects($this->once()) + ->method('where') + ->with('deleted_at = 0') + ->will($this->returnSelf()); + + $this->builder->expects($this->once()) ->method('execute') - ->will($this->returnValue([])); - - $result = $this->folderMapper->getToDelete( - $deleteOlderThan, $this->user - ); + ->will($this->returnValue($this->cursor)); - $this->assertEquals($this->folders, $result); - } - - - public function testGetAllPurgeDeletedUser() - { - $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . - 'WHERE `deleted_at` > 0 ' . - 'AND `user_id` = ?'; - - $this->db->expects($this->exactly(1)) - ->method('prepare') - ->with($sql, null, null) - ->will(($this->returnValue($this->query))); - - $this->query->expects($this->exactly(3)) + $this->cursor->expects($this->exactly(3)) ->method('fetch') - ->willReturnOnConsecutiveCalls(['id' => 4], ['id' => 5]); - - $this->query->expects($this->exactly(1)) - ->method('bindValue') - ->withConsecutive([1, 'hh', 2]); - - $this->query->expects($this->exactly(1)) - ->method('closeCursor'); - - $this->query->expects($this->once()) - ->method('execute') - ->will($this->returnValue([])); - - $result = $this->folderMapper->getToDelete(null, $this->user); + ->willReturnOnConsecutiveCalls( + ['id' => 4], + ['id' => 5], + null + ); + $result = $this->class->findAll(); $this->assertEquals($this->folders, $result); } - - - public function testDeleteFromUser() - { - $userId = 'john'; - $sql = 'DELETE FROM `*PREFIX*news_folders` WHERE `user_id` = ?'; - - $this->db->expects($this->exactly(1)) - ->method('prepare') - ->with($sql, null, null) - ->will(($this->returnValue($this->query))); - - $this->query->expects($this->never()) - ->method('fetch'); - - $this->query->expects($this->exactly(1)) - ->method('bindValue') - ->withConsecutive([1, $userId, 2]); - - $this->query->expects($this->exactly(0)) - ->method('closeCursor'); - - $this->query->expects($this->once()) - ->method('execute') - ->will($this->returnValue([])); - - $this->folderMapper->deleteUser($userId); - } - - }
\ No newline at end of file diff --git a/tests/Unit/Db/MapperTestUtility.php b/tests/Unit/Db/MapperTestUtility.php index 695738179..725f0c3c0 100644 --- a/tests/Unit/Db/MapperTestUtility.php +++ b/tests/Unit/Db/MapperTestUtility.php @@ -24,6 +24,9 @@ namespace OCA\News\Tests\Unit\Db; use Doctrine\DBAL\Driver\PDOStatement; +use Doctrine\DBAL\Driver\Statement; +use OCA\News\Tests\Unit\Service\ServiceTest; +use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; use PHPUnit\Framework\MockObject\MockObject; @@ -45,6 +48,16 @@ abstract class MapperTestUtility extends TestCase */ protected $query; + /** + * @var MockObject|IQueryBuilder + */ + protected $builder; + + /** + * @var MockObject|Statement + */ + protected $cursor; + /** * Run this function before the actual test to either set or initialize the @@ -60,5 +73,11 @@ abstract class MapperTestUtility extends TestCase $this->query = $this->getMockBuilder(\PDOStatement::class) ->getMock(); + + $this->builder = $this->getMockBuilder(IQueryBuilder::class) + ->getMock(); + + $this->cursor = $this->getMockBuilder(Statement::class) + ->getMock(); } } |