From 78dce7ffe18665bf083ff69631db8ae128a2b99f Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Mon, 1 Mar 2021 21:10:46 +0100 Subject: DB: Updates should use set() Issue GH-1211 Signed-off-by: Sean Molenaar --- tests/Unit/Db/FeedMapperTest.php | 161 +++++++++++++++++++++++++++++++++------ 1 file changed, 139 insertions(+), 22 deletions(-) (limited to 'tests/Unit/Db/FeedMapperTest.php') diff --git a/tests/Unit/Db/FeedMapperTest.php b/tests/Unit/Db/FeedMapperTest.php index 780bfbc38..ad2417b53 100644 --- a/tests/Unit/Db/FeedMapperTest.php +++ b/tests/Unit/Db/FeedMapperTest.php @@ -13,12 +13,15 @@ namespace OCA\News\Tests\Unit\Db; +use OC\DB\QueryBuilder\Parameter; +use OC\DB\ResultAdapter; use OCA\News\Db\Feed; use OCA\News\Db\FeedMapperV2; use OCA\News\Utility\Time; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Db\MultipleObjectsReturnedException; use OCP\DB\QueryBuilder\IFunctionBuilder; +use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\DB\QueryBuilder\IQueryFunction; class FeedMapperTest extends MapperTestUtility @@ -457,39 +460,96 @@ class FeedMapperTest extends MapperTestUtility */ public function testRead() { - $this->db->expects($this->once()) + $selectbuilder = $this->getMockBuilder(IQueryBuilder::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->db->expects($this->exactly(2)) ->method('getQueryBuilder') - ->willReturn($this->builder); + ->willReturnOnConsecutiveCalls($selectbuilder, $this->builder); - $this->builder->expects($this->once()) - ->method('update') + $selectbuilder->expects($this->once()) + ->method('select') + ->with('items.id') + ->will($this->returnSelf()); + + $selectbuilder->expects($this->once()) + ->method('from') ->with('news_items', 'items') ->will($this->returnSelf()); - $this->builder->expects($this->once()) + $selectbuilder->expects($this->once()) ->method('innerJoin') ->with('items', 'news_feeds', 'feeds', 'items.feed_id = feeds.id') ->will($this->returnSelf()); + $selectbuilder->expects($this->exactly(2)) + ->method('andWhere') + ->withConsecutive(['feeds.user_id = :userId'], ['feeds.id = :feedId']) + ->will($this->returnSelf()); + + $selectbuilder->expects($this->exactly(2)) + ->method('setParameter') + ->withConsecutive(['userId', 'admin'], ['feedId', 1]) + ->will($this->returnSelf()); + + $selectbuilder->expects($this->exactly(1)) + ->method('getSQL') + ->will($this->returnValue('SQL QUERY')); + + $selectbuilder->expects($this->exactly(1)) + ->method('getParameters') + ->will($this->returnValue([])); + + $result = $this->getMockBuilder(ResultAdapter::class) + ->disableOriginalConstructor() + ->getMock(); + + $result->expects($this->once()) + ->method('fetchAll') + ->willReturn([['id' => 1], ['id' => 2]]); + + $this->db->expects($this->exactly(1)) + ->method('executeQuery') + ->with('SQL QUERY') + ->willReturn($result); + + $this->builder->expects($this->once()) + ->method('update') + ->with('news_items') + ->will($this->returnSelf()); + + $this->builder->expects($this->once()) + ->method('createParameter') + ->will($this->returnArgument(0)); + $this->builder->expects($this->once()) - ->method('setValue') - ->with('unread', 0) + ->method('set') + ->with('unread', 'unread') ->will($this->returnSelf()); $this->builder->expects($this->exactly(2)) ->method('andWhere') - ->withConsecutive(['feeds.user_id = :userId'], ['feeds.id = :feedId']) + ->withConsecutive(['id IN (:idList)'], ['unread != :unread']) ->will($this->returnSelf()); $this->builder->expects($this->exactly(2)) ->method('setParameter') - ->withConsecutive(['userId', 'admin'], ['feedId', 1]) + ->withConsecutive(['unread', false], ['idList', [1, 2]]) ->will($this->returnSelf()); $this->builder->expects($this->exactly(1)) ->method('getSQL') ->will($this->returnValue('QUERY')); + $this->builder->expects($this->exactly(1)) + ->method('getParameters') + ->will($this->returnValue([])); + + $this->builder->expects($this->exactly(1)) + ->method('getParameterTypes') + ->will($this->returnValue([])); + $this->db->expects($this->exactly(1)) ->method('executeUpdate') ->with('QUERY'); @@ -500,45 +560,102 @@ class FeedMapperTest extends MapperTestUtility /** * @covers \OCA\News\Db\FeedMapperV2::read */ - public function testReadWithMaxId() + public function testReadWithMaxID() { - $this->db->expects($this->once()) + $selectbuilder = $this->getMockBuilder(IQueryBuilder::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->db->expects($this->exactly(2)) ->method('getQueryBuilder') - ->willReturn($this->builder); + ->willReturnOnConsecutiveCalls($selectbuilder, $this->builder); - $this->builder->expects($this->once()) - ->method('update') + $selectbuilder->expects($this->once()) + ->method('select') + ->with('items.id') + ->will($this->returnSelf()); + + $selectbuilder->expects($this->once()) + ->method('from') ->with('news_items', 'items') ->will($this->returnSelf()); - $this->builder->expects($this->once()) + $selectbuilder->expects($this->once()) ->method('innerJoin') ->with('items', 'news_feeds', 'feeds', 'items.feed_id = feeds.id') ->will($this->returnSelf()); + $selectbuilder->expects($this->exactly(3)) + ->method('andWhere') + ->withConsecutive(['feeds.user_id = :userId'], ['feeds.id = :feedId'], ['items.id <= :maxItemId']) + ->will($this->returnSelf()); + + $selectbuilder->expects($this->exactly(3)) + ->method('setParameter') + ->withConsecutive(['userId', 'admin'], ['feedId', 1], ['maxItemId', 4]) + ->will($this->returnSelf()); + + $selectbuilder->expects($this->exactly(1)) + ->method('getSQL') + ->will($this->returnValue('SQL QUERY')); + + $selectbuilder->expects($this->exactly(1)) + ->method('getParameters') + ->will($this->returnValue([])); + + $result = $this->getMockBuilder(ResultAdapter::class) + ->disableOriginalConstructor() + ->getMock(); + + $result->expects($this->once()) + ->method('fetchAll') + ->willReturn([['id' => 1], ['id' => 2]]); + + $this->db->expects($this->exactly(1)) + ->method('executeQuery') + ->with('SQL QUERY') + ->willReturn($result); + + $this->builder->expects($this->once()) + ->method('createParameter') + ->will($this->returnArgument(0)); + + $this->builder->expects($this->once()) + ->method('update') + ->with('news_items') + ->will($this->returnSelf()); + $this->builder->expects($this->once()) - ->method('setValue') - ->with('unread', 0) + ->method('set') + ->with('unread', 'unread') ->will($this->returnSelf()); - $this->builder->expects($this->exactly(3)) + $this->builder->expects($this->exactly(2)) ->method('andWhere') - ->withConsecutive(['feeds.user_id = :userId'], ['feeds.id = :feedId'], ['items.id =< :maxItemId']) + ->withConsecutive(['id IN (:idList)'], ['unread != :unread']) ->will($this->returnSelf()); - $this->builder->expects($this->exactly(3)) + $this->builder->expects($this->exactly(2)) ->method('setParameter') - ->withConsecutive(['userId', 'admin'], ['feedId', 1], ['maxItemId', 4]) + ->withConsecutive(['unread', false], ['idList', [1, 2]]) ->will($this->returnSelf()); $this->builder->expects($this->exactly(1)) ->method('getSQL') ->will($this->returnValue('QUERY')); + $this->builder->expects($this->exactly(1)) + ->method('getParameters') + ->will($this->returnValue([])); + + $this->builder->expects($this->exactly(1)) + ->method('getParameterTypes') + ->will($this->returnValue([])); + $this->db->expects($this->exactly(1)) ->method('executeUpdate') ->with('QUERY'); $this->class->read('admin', 1, 4); } -} \ No newline at end of file +} -- cgit v1.2.3