eventManager = $this->createMock(EventManager::class); $this->sharedStimulusExtractor = $this->createMock(SharedStimulusMediaExtractor::class); $this->subject = new MediaSavedEventDispatcher(); $this->subject->setServiceLocator($this->getServiceLocatorMock([ EventManager::SERVICE_ID => $this->eventManager, SharedStimulusMediaExtractor::class => $this->sharedStimulusExtractor, ])); } public function testDispatchFromContentWithNonSharedStimulus(): void { $id = 'fixture-id'; $mimeType = 'fixture-mimetype'; $content = 'fixture-content'; $expectedIds = []; $this->mockTriggerMediaSavedEvent($id, $expectedIds); $this->subject->dispatchFromContent($id, $mimeType, $content); } public function testDispatchFromContent(): void { $id = 'fixture-id'; $mimeType = MediaService::SHARED_STIMULUS_MIME_TYPE; $content = 'fixture-content'; $fixtureIds = ['media-1', 'media-2']; $this->sharedStimulusExtractor ->expects($this->once()) ->method('extractMediaIdentifiers') ->with($content) ->willReturn($fixtureIds); $this->mockTriggerMediaSavedEvent($id, $fixtureIds); $this->subject->dispatchFromContent($id, $mimeType, $content); } public function testDispatchFromFileWithMimeType(): void { $id = 'fixture-id'; $fileSource = 'fixture-path'; $mimeType = 'fixture-mimetype'; $expectedIds = []; $this->mockTriggerMediaSavedEvent($id, $expectedIds); $this->subject->dispatchFromFile($id, $fileSource, $mimeType); } public function testDispatchFromFileWithFile(): void { $id = 'fixture-id'; $mimeType = 'fixture-mimetype'; $fileSource = $this->createMock(File::class); $fileSource->expects($this->once()) ->method('getMimeType') ->willReturn($mimeType); $expectedIds = []; $this->mockTriggerMediaSavedEvent($id, $expectedIds); $this->subject->dispatchFromFile($id, $fileSource, ''); } public function testDispatchFromFileWithSharedStimulusFile(): void { $id = 'fixture-id'; $fixtureIds = ['media-1', 'media-2']; $fileSource = $this->createMock(File::class); $fileSource->expects($this->once()) ->method('getMimeType') ->willReturn(MediaService::SHARED_STIMULUS_MIME_TYPE); $fileSource->expects($this->once()) ->method('read') ->willReturn('content'); $this->sharedStimulusExtractor ->expects($this->once()) ->method('extractMediaIdentifiers') ->with('content') ->willReturn($fixtureIds); $this->mockTriggerMediaSavedEvent($id, $fixtureIds); $this->subject->dispatchFromFile($id, $fileSource, ''); } private function mockTriggerMediaSavedEvent(string $id, array $referenceIds): void { $this->eventManager ->expects($this->once()) ->method('trigger') ->with( $this->callback( function($event) use ($id, $referenceIds) { return ($event instanceof MediaSavedEvent) && ($event->getMediaId() == $id) && ($event->getReferencedMediaIds() == $referenceIds); } ) ); } }