From b46c6df6322e09c05a34acca69403fd4ebd238ba Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Sat, 10 Oct 2020 00:23:15 +0200 Subject: Fix usage of at() in unittests Signed-off-by: Sean Molenaar --- AUTHORS.md | 7 +- tests/Unit/Command/ExploreGeneratorTest.php | 8 +- tests/Unit/Controller/FeedControllerTest.php | 19 +- tests/Unit/Controller/ItemApiControllerTest.php | 101 ++++------- tests/Unit/Controller/ItemControllerTest.php | 87 +++------ tests/Unit/Controller/PageControllerTest.php | 33 ++-- tests/Unit/Db/FolderMapperTest.php | 228 +++++++++++++++++++++--- tests/Unit/Db/MapperTestUtility.php | 202 ++------------------- tests/Unit/Service/FeedServiceTest.php | 195 +++++++------------- tests/Unit/Service/FolderServiceTest.php | 14 +- 10 files changed, 370 insertions(+), 524 deletions(-) diff --git a/AUTHORS.md b/AUTHORS.md index e9064ce67..fab6e9c07 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -6,8 +6,8 @@ * [Gregor Tätzner](mailto:gregor@freenet.de) * [Morris Jobke](mailto:hey@morrisjobke.de) * [Sean Molenaar](mailto:SMillerDev@users.noreply.github.com) -* [Jan-Christoph Borchardt](mailto:hey@jancborchardt.net) * [Sean Molenaar](mailto:sean@seanmolenaar.eu) +* [Jan-Christoph Borchardt](mailto:hey@jancborchardt.net) * [Daniel Schaal](mailto:daniel@schaal.email) * [Davide Saurino](mailto:davide.saurino@alcacoop.it) * [raghunayyar](mailto:me@iraghu.com) @@ -15,9 +15,9 @@ * [Bernhard Posselt](mailto:bep@foryouandyourcustomers.com) * [Thomas Müller](mailto:thomas.mueller@tmit.eu) * [Hoàng Đức Hiếu](mailto:hdhoang@zahe.me) +* [anoy](mailto:anoymouserver+github@mailbox.org) * [Daniel Opitz](mailto:git@copynpaste.de) * [Sean Molenaar](mailto:sean@m2mobi.com) -* [anoy](mailto:anoymouserver+github@mailbox.org) * [rakekniven](mailto:mark.ziegler@rakekniven.de) * [David-Development](mailto:david-dev@live.de) * [IBBoard](mailto:dev@ibboard.co.uk) @@ -43,6 +43,7 @@ * [Andreas Fischer](mailto:bantu@owncloud.com) * [David Guillot](mailto:david@guillot.me) * [Gioele Falcetti](mailto:thegio.f@gmail.com) +* [Jan C. Borchardt](mailto:hey@jancborchardt.net) * [John Kristensen](mailto:john@jerrykan.com) * [Konrad Graefe](mailto:konradgraefe@aol.com) * [Loki3000](mailto:github@labcms.ru) @@ -112,11 +113,13 @@ * [Roeland Jago Douma](mailto:roeland@famdouma.nl) * [Simon](mailto:sschubert89@gmail.com) * [Simon Gilliot](mailto:simon@gilliot.fr) +* [Sturtz Network](mailto:sturtz110751@indianola.k12.ia.us) * [SuliacLEGUILLOU](mailto:suliac.leguillou@pm.me) * [Talon24](mailto:talontalon24@gmail.com) * [TexasGamer](mailto:tpgaubert@gmail.com) * [The Gitter Badger](mailto:badger@gitter.im) * [Thomas Wouters](mailto:twouters@users.noreply.github.com) +* [Tilo Spannagel](mailto:development@tilosp.de) * [Timo Schmidt](mailto:timo@xinterchange.net) * [Welling Guzmán](mailto:WellingGuzman@users.noreply.github.com) * [Xaver Maierhofer](mailto:xaver.maierhofer@xwissen.info) diff --git a/tests/Unit/Command/ExploreGeneratorTest.php b/tests/Unit/Command/ExploreGeneratorTest.php index 213633549..4512e206c 100644 --- a/tests/Unit/Command/ExploreGeneratorTest.php +++ b/tests/Unit/Command/ExploreGeneratorTest.php @@ -136,13 +136,9 @@ class ExploreGeneratorTest extends TestCase { ->with('votes') ->willReturn(100); - $this->consoleOutput->expects($this->at(0)) + $this->consoleOutput->expects($this->exactly(2)) ->method('writeln') - ->with($this->stringContains('')); - - $this->consoleOutput->expects($this->at(1)) - ->method('writeln') - ->with($this->stringContains('Failure')); + ->withConsecutive(['Failed to fetch feed info:'], ['Failure']); self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]); } diff --git a/tests/Unit/Controller/FeedControllerTest.php b/tests/Unit/Controller/FeedControllerTest.php index 7498d0ccb..f5c51b93e 100644 --- a/tests/Unit/Controller/FeedControllerTest.php +++ b/tests/Unit/Controller/FeedControllerTest.php @@ -142,22 +142,13 @@ class FeedControllerTest extends TestCase private function activeInitMocks($id, $type) { - $this->settings->expects($this->at(0)) + $this->settings->expects($this->exactly(2)) ->method('getUserValue') - ->with( - $this->equalTo($this->user), - $this->equalTo($this->appName), - $this->equalTo('lastViewedFeedId') - ) - ->will($this->returnValue($id)); - $this->settings->expects($this->at(1)) - ->method('getUserValue') - ->with( - $this->equalTo($this->user), - $this->equalTo($this->appName), - $this->equalTo('lastViewedFeedType') + ->withConsecutive( + [$this->user, $this->appName, 'lastViewedFeedId'], + [$this->user, $this->appName, 'lastViewedFeedType'] ) - ->will($this->returnValue($type)); + ->willReturnOnConsecutiveCalls($id, $type); } diff --git a/tests/Unit/Controller/ItemApiControllerTest.php b/tests/Unit/Controller/ItemApiControllerTest.php index d2653b92a..75fd6bbef 100644 --- a/tests/Unit/Controller/ItemApiControllerTest.php +++ b/tests/Unit/Controller/ItemApiControllerTest.php @@ -308,19 +308,11 @@ class ItemApiControllerTest extends TestCase public function testReadMultiple() { - $this->itemService->expects($this->at(0)) + $this->itemService->expects($this->exactly(2)) ->method('read') - ->with( - $this->equalTo(2), - $this->equalTo(true), - $this->equalTo($this->user->getUID()) - ); - $this->itemService->expects($this->at(1)) - ->method('read') - ->with( - $this->equalTo(4), - $this->equalTo(true), - $this->equalTo($this->user->getUID()) + ->withConsecutive( + [2, true, $this->user->getUID()], + [4, true, $this->user->getUID()] ); $this->itemAPI->readMultiple([2, 4]); } @@ -328,35 +320,24 @@ class ItemApiControllerTest extends TestCase public function testReadMultipleDoesntCareAboutException() { - $this->itemService->expects($this->at(0)) - ->method('read') - ->will($this->throwException(new ServiceNotFoundException(''))); - $this->itemService->expects($this->at(1)) + $this->itemService->expects($this->exactly(2)) ->method('read') - ->with( - $this->equalTo(4), - $this->equalTo(true), - $this->equalTo($this->user->getUID()) - ); + ->withConsecutive( + [2, true, $this->user->getUID()], + [4, true, $this->user->getUID()] + ) + ->willReturnOnConsecutiveCalls($this->throwException(new ServiceNotFoundException('')), null); $this->itemAPI->readMultiple([2, 4]); } public function testUnreadMultiple() { - $this->itemService->expects($this->at(0)) - ->method('read') - ->with( - $this->equalTo(2), - $this->equalTo(false), - $this->equalTo($this->user->getUID()) - ); - $this->itemService->expects($this->at(1)) + $this->itemService->expects($this->exactly(2)) ->method('read') - ->with( - $this->equalTo(4), - $this->equalTo(false), - $this->equalTo($this->user->getUID()) + ->withConsecutive( + [2, false, $this->user->getUID()], + [4, false, $this->user->getUID()] ); $this->itemAPI->unreadMultiple([2, 4]); } @@ -375,21 +356,11 @@ class ItemApiControllerTest extends TestCase ] ]; - $this->itemService->expects($this->at(0)) - ->method('star') - ->with( - $this->equalTo(2), - $this->equalTo('a'), - $this->equalTo(true), - $this->equalTo($this->user->getUID()) - ); - $this->itemService->expects($this->at(1)) + $this->itemService->expects($this->exactly(2)) ->method('star') - ->with( - $this->equalTo(4), - $this->equalTo('b'), - $this->equalTo(true), - $this->equalTo($this->user->getUID()) + ->withConsecutive( + [2, 'a', true, $this->user->getUID()], + [4, 'b', true, $this->user->getUID()] ); $this->itemAPI->starMultiple($ids); } @@ -408,17 +379,14 @@ class ItemApiControllerTest extends TestCase ] ]; - $this->itemService->expects($this->at(0)) - ->method('star') - ->will($this->throwException(new ServiceNotFoundException(''))); - $this->itemService->expects($this->at(1)) + $this->itemService->expects($this->exactly(2)) ->method('star') - ->with( - $this->equalTo(4), - $this->equalTo('b'), - $this->equalTo(true), - $this->equalTo($this->user->getUID()) - ); + ->withConsecutive( + [2, 'a', true, $this->user->getUID()], + [4, 'b', true, $this->user->getUID()] + ) + ->willReturnOnConsecutiveCalls($this->throwException(new ServiceNotFoundException('')), null); + $this->itemAPI->starMultiple($ids); } @@ -436,22 +404,13 @@ class ItemApiControllerTest extends TestCase ] ]; - $this->itemService->expects($this->at(0)) - ->method('star') - ->with( - $this->equalTo(2), - $this->equalTo('a'), - $this->equalTo(false), - $this->equalTo($this->user->getUID()) - ); - $this->itemService->expects($this->at(1)) + $this->itemService->expects($this->exactly(2)) ->method('star') - ->with( - $this->equalTo(4), - $this->equalTo('b'), - $this->equalTo(false), - $this->equalTo($this->user->getUID()) + ->withConsecutive( + [2, 'a', false, $this->user->getUID()], + [4, 'b', false, $this->user->getUID()] ); + $this->itemAPI->unstarMultiple($ids); } diff --git a/tests/Unit/Controller/ItemControllerTest.php b/tests/Unit/Controller/ItemControllerTest.php index c69191423..c015c33a1 100644 --- a/tests/Unit/Controller/ItemControllerTest.php +++ b/tests/Unit/Controller/ItemControllerTest.php @@ -36,6 +36,7 @@ class ItemControllerTest extends TestCase private $itemService; private $feedService; private $request; + private $user; private $controller; private $newestItemId; @@ -98,41 +99,27 @@ class ItemControllerTest extends TestCase public function testReadMultiple() { - $this->itemService->expects($this->at(0)) + $this->itemService->expects($this->exactly(2)) ->method('read') - ->with( - $this->equalTo(2), - $this->equalTo(true), - $this->equalTo($this->user) - ); - $this->itemService->expects($this->at(1)) - ->method('read') - ->with( - $this->equalTo(4), - $this->equalTo(true), - $this->equalTo($this->user) + ->withConsecutive( + [2, true, $this->user], + [4, true, $this->user] ); + $this->controller->readMultiple([2, 4]); } public function testReadMultipleDontStopOnException() { - $this->itemService->expects($this->at(0)) + + $this->itemService->expects($this->exactly(2)) ->method('read') - ->with( - $this->equalTo(2), - $this->equalTo(true), - $this->equalTo($this->user) + ->withConsecutive( + [2, true, $this->user], + [4, true, $this->user] ) - ->will($this->throwException(new ServiceNotFoundException('yo'))); - $this->itemService->expects($this->at(1)) - ->method('read') - ->with( - $this->equalTo(4), - $this->equalTo(true), - $this->equalTo($this->user) - ); + ->willReturnOnConsecutiveCalls($this->throwException(new ServiceNotFoundException('yo')), null); $this->controller->readMultiple([2, 4]); } @@ -190,39 +177,20 @@ class ItemControllerTest extends TestCase } - private function itemsApiExpects($id, $type, $oldestFirst='1') + private function itemsApiExpects($id, $type, $oldestFirst = '1') { - $this->settings->expects($this->at(0)) + $this->settings->expects($this->exactly(2)) ->method('getUserValue') - ->with( - $this->equalTo($this->user), - $this->equalTo($this->appName), - $this->equalTo('showAll') - ) - ->will($this->returnValue('1')); - $this->settings->expects($this->at(1)) - ->method('getUserValue') - ->with( - $this->equalTo($this->user), - $this->equalTo($this->appName), - $this->equalTo('oldestFirst') + ->withConsecutive( + [$this->user, $this->appName, 'showAll'], + [$this->user, $this->appName, 'oldestFirst'] ) - ->will($this->returnValue($oldestFirst)); - $this->settings->expects($this->at(2)) - ->method('setUserValue') - ->with( - $this->equalTo($this->user), - $this->equalTo($this->appName), - $this->equalTo('lastViewedFeedId'), - $this->equalTo($id) - ); - $this->settings->expects($this->at(3)) + ->willReturnOnConsecutiveCalls('1', $oldestFirst); + $this->settings->expects($this->exactly(2)) ->method('setUserValue') - ->with( - $this->equalTo($this->user), - $this->equalTo($this->appName), - $this->equalTo('lastViewedFeedType'), - $this->equalTo($type) + ->withConsecutive( + [$this->user, $this->appName, 'lastViewedFeedId', $id], + [$this->user, $this->appName, 'lastViewedFeedType', $type] ); } @@ -256,16 +224,7 @@ class ItemControllerTest extends TestCase $this->itemService->expects($this->once()) ->method('findAllItems') - ->with( - $this->equalTo(2), - $this->equalTo(FeedType::FEED), - $this->equalTo(3), - $this->equalTo(0), - $this->equalTo(true), - $this->equalTo(false), - $this->equalTo($this->user), - $this->equalTo([]) - ) + ->with(2, FeedType::FEED, 3, 0, true, false, $this->user, []) ->will($this->returnValue($result['items'])); $response = $this->controller->index(FeedType::FEED, 2, 3); diff --git a/tests/Unit/Controller/PageControllerTest.php b/tests/Unit/Controller/PageControllerTest.php index fe263f242..2afe2d590 100644 --- a/tests/Unit/Controller/PageControllerTest.php +++ b/tests/Unit/Controller/PageControllerTest.php @@ -234,21 +234,15 @@ class PageControllerTest extends TestCase */ public function testUpdateSettings() { - $this->settings->expects($this->at(0)) - ->method('setUserValue') - ->with('becka', 'news', 'showAll', '1'); - $this->settings->expects($this->at(1)) - ->method('setUserValue') - ->with('becka', 'news', 'compact', '1'); - $this->settings->expects($this->at(2)) - ->method('setUserValue') - ->with('becka', 'news', 'preventReadOnScroll', '0'); - $this->settings->expects($this->at(3)) - ->method('setUserValue') - ->with('becka', 'news', 'oldestFirst', '1'); - $this->settings->expects($this->at(4)) + $this->settings->expects($this->exactly(5)) ->method('setUserValue') - ->with('becka', 'news', 'compactExpand', '1'); + ->withConsecutive( + ['becka', 'news', 'showAll', '1'], + ['becka', 'news', 'compact', '1'], + ['becka', 'news', 'preventReadOnScroll', '0'], + ['becka', 'news', 'oldestFirst', '1'], + ['becka', 'news', 'compactExpand', '1'] + ); $this->controller->updateSettings(true, true, false, true, true); } @@ -256,13 +250,12 @@ class PageControllerTest extends TestCase public function testExplore() { $in = ['test']; - $this->settings->expects($this->at(0)) - ->method('setUserValue') - ->with('becka', 'news', 'lastViewedFeedId', 0); - - $this->settings->expects($this->at(1)) + $this->settings->expects($this->exactly(2)) ->method('setUserValue') - ->with('becka', 'news', 'lastViewedFeedType', FeedType::EXPLORE); + ->withConsecutive( + ['becka', 'news', 'lastViewedFeedId', 0], + ['becka', 'news', 'lastViewedFeedType', FeedType::EXPLORE] + ); $this->recommended->expects($this->once()) ->method('forLanguage') diff --git a/tests/Unit/Db/FolderMapperTest.php b/tests/Unit/Db/FolderMapperTest.php index deb235ff7..803c84ebe 100644 --- a/tests/Unit/Db/FolderMapperTest.php +++ b/tests/Unit/Db/FolderMapperTest.php @@ -50,21 +50,40 @@ class FolderMapperTest extends MapperTestUtility ]; } - + /** + * @covers \OCA\News\Db\FolderMapper::find + */ public function testFind() { $userId = 'john'; $id = 3; - $rows = [['id' => $this->folders[0]->getId()]]; $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . 'WHERE `id` = ? ' . 'AND `user_id` = ?'; - $this->setMapperResult($sql, [$id, $userId], $rows); + $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); - } @@ -76,7 +95,26 @@ class FolderMapperTest extends MapperTestUtility 'WHERE `id` = ? ' . 'AND `user_id` = ?'; - $this->setMapperResult($sql, [$id, $userId]); + $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); @@ -87,12 +125,30 @@ class FolderMapperTest extends MapperTestUtility { $userId = 'john'; $id = 3; - $rows = $this->twoRows; $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . 'WHERE `id` = ? ' . 'AND `user_id` = ?'; - $this->setMapperResult($sql, [$id, $userId], $rows); + $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); @@ -103,12 +159,29 @@ class FolderMapperTest extends MapperTestUtility public function testFindAllFromUser() { $userId = 'john'; - $rows = $this->twoRows; $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . 'WHERE `user_id` = ? ' . 'AND `deleted_at` = 0'; - $this->setMapperResult($sql, [$userId], $rows); + $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()) + ->method('execute') + ->will($this->returnValue([])); $result = $this->folderMapper->findAllFromUser($userId); $this->assertEquals($this->folders, $result); @@ -119,12 +192,29 @@ class FolderMapperTest extends MapperTestUtility { $folderName = 'heheh'; $userId = 'john'; - $rows = $this->twoRows; $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . 'WHERE `name` = ? ' . 'AND `user_id` = ?'; - $this->setMapperResult($sql, [$folderName, $userId], $rows); + $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, $folderName, 2], [2, $userId, 2]); + + $this->query->expects($this->exactly(1)) + ->method('closeCursor'); + + $this->query->expects($this->once()) + ->method('execute') + ->will($this->returnValue([])); $result = $this->folderMapper->findByName($folderName, $userId); $this->assertEquals($this->folders, $result); @@ -145,9 +235,25 @@ class FolderMapperTest extends MapperTestUtility $sql3 = 'DELETE FROM `*PREFIX*news_items` WHERE `feed_id` NOT IN '. '(SELECT `feeds`.`id` FROM `*PREFIX*news_feeds` `feeds`)'; - $this->setMapperResult($sql, $arguments, [], null, null, true); - $this->setMapperResult($sql2, $arguments2, [], null, null, true); - $this->setMapperResult($sql3, [], [], null, null, true); + $this->db->expects($this->exactly(3)) + ->method('prepare') + ->withConsecutive( + [$sql, null, null], + [$sql2, null, null], + [$sql3, null, null] + ) + ->will(($this->returnValue($this->query))); + + $this->query->expects($this->exactly(2)) + ->method('bindValue') + ->withConsecutive([1, 3, 1]); + + $this->query->expects($this->exactly(3)) + ->method('closeCursor'); + + $this->query->expects($this->exactly(3)) + ->method('execute') + ->will($this->returnValue([])); $this->folderMapper->delete($folder); } @@ -155,13 +261,32 @@ class FolderMapperTest extends MapperTestUtility public function testGetPurgeDeleted() { - $rows = $this->twoRows; - $deleteOlderThan = 110; $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . 'WHERE `deleted_at` > 0 ' . 'AND `deleted_at` < ? '; - $this->setMapperResult($sql, [$deleteOlderThan], $rows); - $result = $this->folderMapper->getToDelete($deleteOlderThan); + + + $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, 110, 1]); + + $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); } @@ -170,13 +295,33 @@ class FolderMapperTest extends MapperTestUtility public function testGetPurgeDeletedUser() { - $rows = $this->twoRows; $deleteOlderThan = 110; $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . 'WHERE `deleted_at` > 0 ' . 'AND `deleted_at` < ? ' . 'AND `user_id` = ?'; - $this->setMapperResult($sql, [$deleteOlderThan, $this->user], $rows); + + + $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()) + ->method('execute') + ->will($this->returnValue([])); + $result = $this->folderMapper->getToDelete( $deleteOlderThan, $this->user ); @@ -187,12 +332,30 @@ class FolderMapperTest extends MapperTestUtility public function testGetAllPurgeDeletedUser() { - $rows = $this->twoRows; - $sql = 'SELECT * FROM `*PREFIX*news_folders` ' . 'WHERE `deleted_at` > 0 ' . 'AND `user_id` = ?'; - $this->setMapperResult($sql, [$this->user], $rows); + + $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, '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); $this->assertEquals($this->folders, $result); @@ -204,7 +367,24 @@ class FolderMapperTest extends MapperTestUtility $userId = 'john'; $sql = 'DELETE FROM `*PREFIX*news_folders` WHERE `user_id` = ?'; - $this->setMapperResult($sql, [$userId]); + $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); } diff --git a/tests/Unit/Db/MapperTestUtility.php b/tests/Unit/Db/MapperTestUtility.php index b414b43e3..695738179 100644 --- a/tests/Unit/Db/MapperTestUtility.php +++ b/tests/Unit/Db/MapperTestUtility.php @@ -23,20 +23,27 @@ namespace OCA\News\Tests\Unit\Db; +use Doctrine\DBAL\Driver\PDOStatement; use OCP\IDBConnection; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; + /** * Simple utility class for testing mappers */ abstract class MapperTestUtility extends TestCase { + + /** + * @var MockObject|IDBConnection + */ protected $db; - private $query; - private $queryAt; - private $prepareAt; - private $fetchAt; - private $iterators; + + /** + * @var MockObject|PDOStatement + */ + protected $query; /** @@ -48,187 +55,10 @@ abstract class MapperTestUtility extends TestCase parent::setUp(); $this->db = $this->getMockBuilder(IDBConnection::class) - ->disableOriginalConstructor() - ->getMock(); - - $this->query = $this->createMock(\PDOStatement::class); - $this->queryAt = 0; - $this->prepareAt = 0; - $this->iterators = []; - $this->fetchAt = 0; - } - - /** - * Checks if an array is associative - * - * @param array $array - * @return bool true if associative - */ - private function isAssocArray(array $array) - { - return array_values($array) !== $array; - } - - /** - * Returns the correct PDO constant based on the value type - * - * @param $value - * @return PDO constant - */ - private function getPDOType($value) - { - switch (gettype($value)) { - case 'integer': - return \PDO::PARAM_INT; - case 'boolean': - return \PDO::PARAM_BOOL; - default: - return \PDO::PARAM_STR; - } - } - - /** - * Create mocks and set expected results for database queries - * - * @param string $sql the sql query that you expect to receive - * @param array $arguments the expected arguments for the prepare query - * method - * @param array $returnRows the rows that should be returned for the result - * of the database query. If not provided, it wont - * be assumed that fetch will be called on the - * result - */ - protected function setMapperResult($sql, $arguments=array(), $returnRows=array(), - $limit=null, $offset=null, $expectClose=false - ) { - if($limit === null && $offset === null) { - $this->db->expects($this->at($this->prepareAt)) - ->method('prepare') - ->with($this->equalTo($sql)) - ->will(($this->returnValue($this->query))); - } elseif($limit !== null && $offset === null) { - $this->db->expects($this->at($this->prepareAt)) - ->method('prepare') - ->with($this->equalTo($sql), $this->equalTo($limit)) - ->will(($this->returnValue($this->query))); - } elseif($limit === null && $offset !== null) { - $this->db->expects($this->at($this->prepareAt)) - ->method('prepare') - ->with( - $this->equalTo($sql), - $this->equalTo(null), - $this->equalTo($offset) - ) - ->will(($this->returnValue($this->query))); - } else { - $this->db->expects($this->at($this->prepareAt)) - ->method('prepare') - ->with( - $this->equalTo($sql), - $this->equalTo($limit), - $this->equalTo($offset) - ) - ->will(($this->returnValue($this->query))); - } - - $this->iterators[] = new ArgumentIterator($returnRows); - - $iterators = $this->iterators; - $fetchAt = $this->fetchAt; + ->disableOriginalConstructor() + ->getMock(); - $this->query->expects($this->any()) - ->method('fetch') - ->will( - $this->returnCallback( - function () use ($iterators, $fetchAt) { - $iterator = $iterators[$fetchAt]; - $result = $iterator->next(); - - if($result === false) { - $fetchAt++; - } - - $this->queryAt++; - - return $result; - } - ) - ); - - if ($this->isAssocArray($arguments)) { - foreach($arguments as $key => $argument) { - $pdoConstant = $this->getPDOType($argument); - $this->query->expects($this->at($this->queryAt)) - ->method('bindValue') - ->with( - $this->equalTo($key), - $this->equalTo($argument), - $this->equalTo($pdoConstant) - ); - $this->queryAt++; - } - } else { - $index = 1; - foreach($arguments as $argument) { - $pdoConstant = $this->getPDOType($argument); - $this->query->expects($this->at($this->queryAt)) - ->method('bindValue') - ->with( - $this->equalTo($index), - $this->equalTo($argument), - $this->equalTo($pdoConstant) - ); - $index++; - $this->queryAt++; - } - } - - $this->query->expects($this->at($this->queryAt)) - ->method('execute') - ->will( - $this->returnCallback( - function ($sql, $p=null, $o=null, $s=null) { - - } - ) - ); - $this->queryAt++; - - - - if ($expectClose) { - $closing = $this->at($this->queryAt); - } else { - $closing = $this->any(); - } - $this->query->expects($closing)->method('closeCursor'); - $this->queryAt++; - - $this->prepareAt++; - $this->fetchAt++; - } - - -} - - -class ArgumentIterator -{ - - private $arguments; - - public function __construct($arguments) - { - $this->arguments = $arguments; - } - - public function next() - { - $result = array_shift($this->arguments); - if($result === null) { - return false; - } else { - return $result; - } + $this->query = $this->getMockBuilder(\PDOStatement::class) + ->getMock(); } } diff --git a/tests/Unit/Service/FeedServiceTest.php b/tests/Unit/Service/FeedServiceTest.php index f93a1e286..3b39019c1 100644 --- a/tests/Unit/Service/FeedServiceTest.php +++ b/tests/Unit/Service/FeedServiceTest.php @@ -207,36 +207,28 @@ class FeedServiceTest extends TestCase } ) ); - $this->itemMapper->expects($this->at(0)) + $this->itemMapper->expects($this->exactly(2)) ->method('findByGuidHash') - ->with( - $this->equalTo($item2->getGuidHash()), - $this->equalTo($item2->getFeedId()), - $this->equalTo($this->user) + ->withConsecutive( + [$item2->getGuidHash(), $item2->getFeedId(), $this->user], + [$item1->getGuidHash(), $item1->getFeedId(), $this->user] ) ->will($this->throwException($ex)); - $this->purifier->expects($this->at(0)) + + $this->purifier->expects($this->exactly(2)) ->method('purify') - ->with($this->equalTo($return[1][1]->getBody())) - ->will($this->returnValue($return[1][1]->getBody())); - $this->itemMapper->expects($this->at(1)) - ->method('insert') - ->with($this->equalTo($return[1][1])); - $this->itemMapper->expects($this->at(2)) - ->method('findByGuidHash') - ->with( - $this->equalTo($item1->getGuidHash()), - $this->equalTo($item1->getFeedId()), - $this->equalTo($this->user) + ->withConsecutive( + [$return[1][1]->getBody()], + [$return[1][0]->getBody()] ) - ->will($this->throwException($ex)); - $this->purifier->expects($this->at(1)) - ->method('purify') - ->with($this->equalTo($return[1][0]->getBody())) - ->will($this->returnValue($return[1][0]->getBody())); - $this->itemMapper->expects($this->at(3)) + ->willReturnOnConsecutiveCalls( + $return[1][1]->getBody(), + $return[1][0]->getBody() + ); + + $this->itemMapper->expects($this->exactly(2)) ->method('insert') - ->with($this->equalTo($return[1][0])); + ->withConsecutive([$return[1][1]], [$return[1][0]]); $feed = $this->feedService->create( $url, $folderId, $this->user, null, @@ -293,28 +285,20 @@ class FeedServiceTest extends TestCase } ) ); - $this->itemMapper->expects($this->at(0)) + $this->itemMapper->expects($this->exactly(2)) ->method('findByGuidHash') - ->with( - $this->equalTo($item2->getGuidHash()), - $this->equalTo($item2->getFeedId()), - $this->equalTo($this->user) + ->withConsecutive( + [$item2->getGuidHash(), $item2->getFeedId(), $this->user], + [$item1->getGuidHash(), $item1->getFeedId(), $this->user] ) - ->will($this->throwException($ex)); - $this->purifier->expects($this->at(0)) + ->willReturnOnConsecutiveCalls($this->throwException($ex), null); + $this->purifier->expects($this->exactly(1)) ->method('purify') - ->with($this->equalTo($return[1][1]->getBody())) - ->will($this->returnValue($return[1][1]->getBody())); - $this->itemMapper->expects($this->at(1)) + ->withConsecutive([$return[1][1]->getBody()]) + ->willReturnOnConsecutiveCalls($return[1][1]->getBody()); + $this->itemMapper->expects($this->exactly(1)) ->method('insert') - ->with($this->equalTo($return[1][1])); - $this->itemMapper->expects($this->at(2)) - ->method('findByGuidHash') - ->with( - $this->equalTo($item1->getGuidHash()), - $this->equalTo($item1->getFeedId()), - $this->equalTo($this->user) - ); + ->withConsecutive([$return[1][1]]); $feed = $this->feedService->create($url, $folderId, $this->user); @@ -364,7 +348,7 @@ class FeedServiceTest extends TestCase $fetchReturn = [$feed, $items]; - $this->feedMapper->expects($this->at(0)) + $this->feedMapper->expects($this->exactly(2)) ->method('find') ->with($this->user, $feed->getId()) ->will($this->returnValue($feed)); @@ -378,7 +362,7 @@ class FeedServiceTest extends TestCase $this->equalTo('') ) ->will($this->returnValue($fetchReturn)); - $this->feedMapper->expects($this->at(1)) + $this->feedMapper->expects($this->exactly(1)) ->method('update') ->with($this->equalTo($feed)); $this->itemMapper->expects($this->once()) @@ -389,7 +373,7 @@ class FeedServiceTest extends TestCase $this->equalTo($this->user) ) ->will($this->throwException($ex)); - $this->purifier->expects($this->at(0)) + $this->purifier->expects($this->exactly(1)) ->method('purify') ->with($this->equalTo($items[0]->getBody())) ->will($this->returnValue($items[0]->getBody())); @@ -397,11 +381,6 @@ class FeedServiceTest extends TestCase ->method('insert') ->with($this->equalTo($items[0])); - $this->feedMapper->expects($this->at(2)) - ->method('find') - ->with($this->user, $feed->getId()) - ->will($this->returnValue($feed)); - $return = $this->feedService->update($this->user, $feed->getId()); @@ -428,7 +407,7 @@ class FeedServiceTest extends TestCase $fetchReturn = [$feed, $items]; - $this->feedMapper->expects($this->at(0)) + $this->feedMapper->expects($this->exactly(2)) ->method('find') ->with($this->user, $feed->getId()) ->will($this->returnValue($feed)); @@ -442,7 +421,7 @@ class FeedServiceTest extends TestCase $this->equalTo('') ) ->will($this->returnValue($fetchReturn)); - $this->feedMapper->expects($this->at(1)) + $this->feedMapper->expects($this->exactly(1)) ->method('update') ->with($this->equalTo($feed)); $this->itemMapper->expects($this->once()) @@ -453,7 +432,7 @@ class FeedServiceTest extends TestCase $this->equalTo($this->user) ) ->will($this->returnValue($items[0])); - $this->purifier->expects($this->at(0)) + $this->purifier->expects($this->exactly(1)) ->method('purify') ->with($this->equalTo($items[0]->getBody())) ->will($this->returnValue($items[0]->getBody())); @@ -461,11 +440,6 @@ class FeedServiceTest extends TestCase ->method('update') ->with($this->equalTo($items[0])); - $this->feedMapper->expects($this->at(2)) - ->method('find') - ->with($this->user, $feed->getId()) - ->will($this->returnValue($feed)); - $return = $this->feedService->update($this->user, $feed->getId(), true); @@ -513,7 +487,7 @@ class FeedServiceTest extends TestCase return $item; } - public function testUpdateUpdatesWhenUpdateddateIsNewer() + public function testUpdateUpdatesWhenUpdatedDateIsNewer() { $feed = $this->createUpdateFeed(); $item = $this->createUpdateItem(); @@ -523,26 +497,23 @@ class FeedServiceTest extends TestCase $fetchReturn = [$feed, $items]; - $this->feedMapper->expects($this->at(0)) + $this->feedMapper->expects($this->exactly(2)) ->method('find') ->will($this->returnValue($feed)); $this->fetcher->expects($this->once()) ->method('fetch') ->will($this->returnValue($fetchReturn)); - $this->feedMapper->expects($this->at(1)) + $this->feedMapper->expects($this->exactly(1)) ->method('update'); $this->itemMapper->expects($this->once()) ->method('findByGuidHash') ->will($this->returnValue($item2)); - $this->purifier->expects($this->at(0)) + $this->purifier->expects($this->exactly(1)) ->method('purify') ->will($this->returnValue($items[0]->getBody())); $this->itemMapper->expects($this->once()) ->method('update') ->with($this->equalTo($item2)); - $this->feedMapper->expects($this->at(2)) - ->method('find') - ->will($this->returnValue($feed)); $return = $this->feedService->update($this->user, $feed->getId()); @@ -564,26 +535,23 @@ class FeedServiceTest extends TestCase $fetchReturn = [$feed, $items]; - $this->feedMapper->expects($this->at(0)) + $this->feedMapper->expects($this->exactly(2)) ->method('find') ->will($this->returnValue($feed)); $this->fetcher->expects($this->once()) ->method('fetch') ->will($this->returnValue($fetchReturn)); - $this->feedMapper->expects($this->at(1)) + $this->feedMapper->expects($this->exactly(1)) ->method('update'); $this->itemMapper->expects($this->once()) ->method('findByGuidHash') ->will($this->returnValue($item2)); - $this->purifier->expects($this->at(0)) + $this->purifier->expects($this->exactly(1)) ->method('purify') ->will($this->returnValue($items[0]->getBody())); $this->itemMapper->expects($this->once()) ->method('update') ->with($this->equalTo($item3)); - $this->feedMapper->expects($this->at(2)) - ->method('find') - ->will($this->returnValue($feed)); $return = $this->feedService->update($this->user, $feed->getId()); @@ -635,37 +603,32 @@ class FeedServiceTest extends TestCase $feed->setUpdateErrorCount(0); $feed->setLastUpdateError(''); - $exptectedFeed = new Feed(); - $exptectedFeed->setId(3); - $exptectedFeed->setUrl('http://example.com'); - $exptectedFeed->setUpdateErrorCount(1); - $exptectedFeed->setLastUpdateError('hi'); + $expectedFeed = new Feed(); + $expectedFeed->setId(3); + $expectedFeed->setUrl('http://example.com'); + $expectedFeed->setUpdateErrorCount(1); + $expectedFeed->setLastUpdateError('hi'); $ex = new ReadErrorException('hi'); - $this->feedMapper->expects($this->at(0)) + $this->feedMapper->expects($this->exactly(2)) ->method('find') ->with($this->user, $feed->getId()) - ->will($this->returnValue($feed)); + ->willReturnOnConsecutiveCalls($feed, $expectedFeed); $this->fetcher->expects($this->once()) ->method('fetch') ->will($this->throwException($ex)); $this->logger->expects($this->any()) ->method('debug'); - $this->feedMapper->expects($this->at(1)) + $this->feedMapper->expects($this->exactly(1)) ->method('update') - ->with($exptectedFeed) - ->will($this->returnValue($exptectedFeed)); - - $this->feedMapper->expects($this->at(2)) - ->method('find') - ->with($this->user, $feed->getId()) - ->will($this->returnValue($exptectedFeed)); + ->with($expectedFeed) + ->will($this->returnValue($expectedFeed)); $return = $this->feedService->update($this->user, $feed->getId()); - $this->assertEquals($return, $exptectedFeed); + $this->assertEquals($return, $expectedFeed); } @@ -676,7 +639,7 @@ class FeedServiceTest extends TestCase $ex = new DoesNotExistException(''); - $this->feedMapper->expects($this->at(0)) + $this->feedMapper->expects($this->exactly(1)) ->method('find') ->with($this->user, $feed->getId()) ->will($this->throwException($ex)); @@ -696,7 +659,7 @@ class FeedServiceTest extends TestCase $ex = new DoesNotExistException(''); - $this->feedMapper->expects($this->at(0)) + $this->feedMapper->expects($this->exactly(1)) ->method('find') ->with($this->user, $feed->getId()) ->will($this->returnValue($feed)); @@ -735,11 +698,11 @@ class FeedServiceTest extends TestCase $fetchReturn = [$feed, $items]; $ex = new DoesNotExistException(''); - $this->feedMapper->expects($this->at(0)) + $this->feedMapper->expects($this->exactly(2)) ->method('find') ->with($this->user, $feed->getId()) - ->will($this->returnValue($feed)); - $this->feedMapper->expects($this->at(1)) + ->willReturnOnConsecutiveCalls($feed, $this->throwException($ex)); + $this->feedMapper->expects($this->exactly(1)) ->method('update') ->with($this->equalTo($feed)); $this->fetcher->expects($this->once()) @@ -752,12 +715,7 @@ class FeedServiceTest extends TestCase $this->equalTo($feed->getId()), $this->equalTo($this->user) ) - ->will($this->returnValue($item2));; - - $this->feedMapper->expects($this->at(2)) - ->method('find') - ->with($this->user, $feed->getId()) - ->will($this->throwException($ex)); + ->will($this->returnValue($item2)); $this->expectException(ServiceNotFoundException::class); $this->feedService->update($this->user, $feed->getId()); @@ -977,21 +935,18 @@ class FeedServiceTest extends TestCase ); - $this->itemMapper->expects($this->at(0)) + $this->itemMapper->expects($this->exactly(2)) ->method('findByGuidHash') - ->will($this->throwException(new DoesNotExistException('yo'))); + ->withConsecutive(['03c7c0ace395d80182db07ae2c30f034', 3, $this->user], ['03c7c0ace395d80182db07ae2c30f034', 3, $this->user]) + ->willReturnOnConsecutiveCalls($this->throwException(new DoesNotExistException('yo')), $item); $this->purifier->expects($this->once()) ->method('purify') ->with($this->equalTo($item->getBody())) ->will($this->returnValue($item->getBody())); - $this->itemMapper->expects($this->at(1)) + $this->itemMapper->expects($this->exactly(1)) ->method('insert') ->with($this->equalTo($item)); - - $this->itemMapper->expects($this->at(2)) - ->method('findByGuidHash') - ->will($this->returnValue($item)); - $this->itemMapper->expects($this->at(3)) + $this->itemMapper->expects($this->exactly(1)) ->method('update') ->with($this->equalTo($item)); @@ -1056,12 +1011,9 @@ class FeedServiceTest extends TestCase ->method('getToDelete') ->with($this->equalTo($time), $this->equalTo($this->user)) ->will($this->returnValue($feeds)); - $this->feedMapper->expects($this->at(1)) + $this->feedMapper->expects($this->exactly(2)) ->method('delete') - ->with($this->equalTo($feed1)); - $this->feedMapper->expects($this->at(2)) - ->method('delete') - ->with($this->equalTo($feed2)); + ->withConsecutive([$feed1], [$feed2]); $this->feedService->purgeDeleted($this->user); } @@ -1079,12 +1031,9 @@ class FeedServiceTest extends TestCase ->method('getToDelete') ->with($this->equalTo(null), $this->equalTo($this->user)) ->will($this->returnValue($feeds)); - $this->feedMapper->expects($this->at(1)) + $this->feedMapper->expects($this->exactly(2)) ->method('delete') - ->with($this->equalTo($feed1)); - $this->feedMapper->expects($this->at(2)) - ->method('delete') - ->with($this->equalTo($feed2)); + ->withConsecutive([$feed1], [$feed2]); $this->feedService->purgeDeleted($this->user, false); } @@ -1142,29 +1091,21 @@ class FeedServiceTest extends TestCase ] ); $feed2 = Feed::fromRow(['id' => 3]); - $this->feedMapper->expects($this->at(0)) + $this->feedMapper->expects($this->exactly(2)) ->method('find') ->with( $this->equalTo($this->user), $this->equalTo($feed->getId()) ) - ->will($this->returnValue($feed)); + ->willReturnOnConsecutiveCalls($this->returnValue($feed), $this->throwException(new DoesNotExistException(''))); $feed2->setFullTextEnabled(true); $feed2->setHttpEtag(''); $feed2->setHttpLastModified(0); - $this->feedMapper->expects($this->at(1)) + $this->feedMapper->expects($this->exactly(1)) ->method('update') ->with($this->equalTo($feed2)); - $this->feedMapper->expects($this->at(2)) - ->method('find') - ->with( - $this->equalTo($this->user), - $this->equalTo($feed->getId()) - ) - ->will($this->throwException(new DoesNotExistException(''))); - $this->expectException(ServiceNotFoundException::class); $this->feedService->patch(3, $this->user, ['fullTextEnabled' => true]); diff --git a/tests/Unit/Service/FolderServiceTest.php b/tests/Unit/Service/FolderServiceTest.php index deca27f08..b1469dc00 100644 --- a/tests/Unit/Service/FolderServiceTest.php +++ b/tests/Unit/Service/FolderServiceTest.php @@ -293,12 +293,9 @@ class FolderServiceTest extends TestCase ->method('getToDelete') ->with($this->equalTo($time), $this->equalTo($this->user)) ->will($this->returnValue($feeds)); - $this->folderMapper->expects($this->at(1)) + $this->folderMapper->expects($this->exactly(2)) ->method('delete') - ->with($this->equalTo($folder1)); - $this->folderMapper->expects($this->at(2)) - ->method('delete') - ->with($this->equalTo($folder2)); + ->withConsecutive([$folder1], [$folder2]); $this->folderService->purgeDeleted($this->user); } @@ -316,12 +313,9 @@ class FolderServiceTest extends TestCase ->method('getToDelete') ->with($this->equalTo(null), $this->equalTo($this->user)) ->will($this->returnValue($feeds)); - $this->folderMapper->expects($this->at(1)) + $this->folderMapper->expects($this->exactly(2)) ->method('delete') - ->with($this->equalTo($folder1)); - $this->folderMapper->expects($this->at(2)) - ->method('delete') - ->with($this->equalTo($folder2)); + ->withConsecutive([$folder1], [$folder2]); $this->folderService->purgeDeleted($this->user, false); } -- cgit v1.2.3