permissionHelperMock = $this->createMock(PermissionHelper::class); $this->searchQueryMock = $this->createMock(SearchQuery::class); $this->resultSetMock = $this->createMock(ResultSet::class); $this->modelMock = $this->createMock(Ontology::class); $this->resourceMock = $this->createMock(core_kernel_classes_Resource::class); $this->resultSetFilter = $this->createMock(ResultSetFilter::class); $this->resultAccessChecker = $this->createMock(ResultAccessChecker::class); $this->resourceMock ->method('getUri') ->willReturn('uri1'); $this->resourceMock ->method('getLabel') ->willReturn('label'); $this->modelMock ->method('getResource') ->willReturn($this->resourceMock); $this->resultSetMock ->method('getArrayCopy') ->willReturn( [ ['id' => 'uri1'], ['id' => 'uri2'], ] ); $this->permissionHelperMock ->expects($this->once()) ->method('filterByPermission') ->willReturn( [ 'uri1', ] ); $this->subject = new ResultSetResponseNormalizer(); $this->subject->setServiceLocator( $this->getServiceLocatorMock( [ PermissionHelper::class => $this->permissionHelperMock, ResultSetFilter::class => $this->resultSetFilter, ResultAccessChecker::class => $this->resultAccessChecker ] ) ); $this->subject->setModel($this->modelMock); } public function testNormalize() { $this->resultSetMock ->expects($this->exactly(2)) ->method('getTotalCount') ->willReturn(100); $this->searchQueryMock ->method('getRows') ->willReturn(2); $this->searchQueryMock ->method('getPage') ->willReturn(1); $this->resultSetFilter ->method('filter') ->willReturn(['id' => 'uri']); $this->resultAccessChecker ->method('hasReadAccess') ->willReturn(true); $result = $this->subject->normalize($this->searchQueryMock, $this->resultSetMock, 'results'); $this->assertResult($result); } public function testNormalizeWithEmptyRows() { $this->resultSetMock ->expects($this->once()) ->method('getTotalCount') ->willReturn(100); $this->searchQueryMock ->method('getRows') ->willReturn(0); $this->permissionHelperMock ->expects($this->once()) ->method('filterByPermission') ->willReturn( [ 'uri1', ] ); $this->resultSetFilter ->method('filter') ->willReturn(['id' => 'uri']); $this->searchQueryMock ->method('getPage') ->willReturn(1); $this->resultAccessChecker ->method('hasReadAccess') ->willReturn(true); $result = $this->subject->normalize($this->searchQueryMock, $this->resultSetMock, 'result'); $this->assertResult($result); } private function assertResult(array $result): void { $this->assertIsArray($result); $this->assertArrayHasKey('data', $result); $this->assertArrayHasKey('readonly', $result); $this->assertArrayHasKey('success', $result); $this->assertArrayHasKey('page', $result); $this->assertArrayHasKey('total', $result); $this->assertArrayHasKey('totalCount', $result); $this->assertArrayHasKey('records', $result); $this->assertEquals(1, $result['page']); $this->assertEquals(100, $result['totalCount']); $this->assertEquals(2, $result['records']); $this->assertCount(2, $result['data']); $this->assertCount(2, $result['readonly']); $this->assertEquals( [ [ 'id' => 'uri', ], [ 'id' => 'uri', ], ], $result['data'] ); $this->assertFalse($result['readonly']['uri1']); } }