prophesize(JwtAuth::class); $this->jwtAuth = $prophecy->reveal(); $prophecy = $this->prophesize(LoggerInterface::class); $this->logger = $prophecy->reveal(); $prophecy = $this->prophesize(MediaSourceService::class); $this->mediaSource = $prophecy->reveal(); $prophecy = $this->prophesize(GeminiClient::class); $this->geminiClient = $prophecy->reveal(); $prophecy = $this->prophesize(Client::class); $this->guzzle = $prophecy->reveal(); // Mock up an entity to use (node in this case). $prophecy = $this->prophesize(EntityInterface::class); $prophecy->id()->willReturn(1); $prophecy->getEntityTypeId()->willReturn('node'); $prophecy->uuid()->willReturn('abc123'); $this->entity = $prophecy->reveal(); // Mock up a media to use. $prophecy = $this->prophesize(MediaInterface::class); $prophecy->id()->willReturn(1); $prophecy->getEntityTypeId()->willReturn('media'); $prophecy->uuid()->willReturn('abc123'); $this->media = $prophecy->reveal(); } /** * Mocks up a gemini client that fails its lookup. */ private function mockGeminiClientForFail() { $prophecy = $this->prophesize(GeminiClient::class); $prophecy->getUrls(Argument::any(), Argument::any()) ->willReturn([]); $this->geminiClient = $prophecy->reveal(); } /** * Mocks up a gemini client that finds a fedora url. */ private function mockGeminiClientForSuccess() { $prophecy = $this->prophesize(GeminiClient::class); $prophecy->getUrls(Argument::any(), Argument::any()) ->willReturn([ 'drupal' => '', 'fedora' => 'http://localhost:8080/fcrepo/rest/abc123', ]); $this->geminiClient = $prophecy->reveal(); } /** * Mocks up a media source service that finds the source file for a media. */ private function mockMediaSourceForSuccess() { $prophecy = $this->prophesize(FileInterface::class); $prophecy->uuid()->willReturn('abc123'); $file = $prophecy->reveal(); $prophecy = $this->prophesize(MediaSourceService::class); $prophecy->getSourceFile(Argument::any()) ->willReturn($file); $this->mediaSource = $prophecy->reveal(); } /** * Make the gemini lookup out of class variables. */ private function createGeminiLookup() { return new GeminiLookup( $this->geminiClient, $this->jwtAuth, $this->mediaSource, $this->guzzle, $this->logger ); } /** * @covers ::lookup * @covers ::__construct */ public function testEntityNotSaved() { // Mock an entity that returns a null id. // That means it's not saved in the db yet. $prophecy = $this->prophesize(EntityInterface::class); $prophecy->id()->willReturn(NULL); $this->entity = $prophecy->reveal(); $gemini_lookup = $this->createGeminiLookup(); $this->assertEquals( NULL, $gemini_lookup->lookup($this->entity) ); } /** * @covers ::lookup * @covers ::__construct */ public function testEntityNotFound() { $this->mockGeminiClientForFail(); $gemini_lookup = $this->createGeminiLookup(); $this->assertEquals( NULL, $gemini_lookup->lookup($this->entity) ); } /** * @covers ::lookup * @covers ::__construct */ public function testEntityFound() { $this->mockGeminiClientForSuccess(); $gemini_lookup = $this->createGeminiLookup(); $this->assertEquals( 'http://localhost:8080/fcrepo/rest/abc123', $gemini_lookup->lookup($this->entity) ); } /** * @covers ::lookup * @covers ::__construct */ public function testMediaHasNoSourceFile() { // Mock a media source service that fails to find // the source file for a media. $prophecy = $this->prophesize(MediaSourceService::class); $prophecy->getSourceFile(Argument::any()) ->willThrow(new NotFoundHttpException("Media has no source")); $this->mediaSource = $prophecy->reveal(); $gemini_lookup = $this->createGeminiLookup(); $this->assertEquals( NULL, $gemini_lookup->lookup($this->media) ); } /** * @covers ::lookup * @covers ::__construct */ public function testMediaNotFound() { $this->mockMediaSourceForSuccess(); $this->mockGeminiClientForFail(); $gemini_lookup = $this->createGeminiLookup(); $this->assertEquals( NULL, $gemini_lookup->lookup($this->media) ); } /** * @covers ::lookup * @covers ::__construct */ public function testFileFoundButNoDescribedby() { $this->mockMediaSourceForSuccess(); $this->mockGeminiClientForSuccess(); // Mock up a guzzle client that does not return // the describedby header. $prophecy = $this->prophesize(Client::class); $prophecy->head(Argument::any(), Argument::any()) ->willReturn(new Response(200, [])); $this->guzzle = $prophecy->reveal(); $gemini_lookup = $this->createGeminiLookup(); $this->assertEquals( NULL, $gemini_lookup->lookup($this->media) ); } /** * @covers ::lookup * @covers ::__construct */ public function testMediaFound() { $this->mockMediaSourceForSuccess(); $this->mockGeminiClientForSuccess(); // Mock up a guzzle client that returns // the describedby header. $prophecy = $this->prophesize(Client::class); $prophecy->head(Argument::any(), Argument::any()) ->willReturn(new Response(200, ['Link' => '; rel="describedby"'])); $this->guzzle = $prophecy->reveal(); $gemini_lookup = $this->createGeminiLookup(); $this->assertEquals( 'http://localhost:8080/fcrepo/rest/abc123/fcr:metadata', $gemini_lookup->lookup($this->media) ); } }