createMock(ExportTemplateRepository::class); $templates = []; $logger = $this->createMock(LoggerInterface::class); if ($withTemplates) { $template1 = $this->createMock(ExportTemplate::class); $template1->method('getId')->willReturn(1); $template1->method('getTitle')->willReturn('CSV Test'); $template1->method('getLanguage')->willReturn('de'); $template1->method('getRenderer')->willReturn('csv'); $template1->method('getColumns')->willReturn(['date', 'customer.name', 'duration', 'rate']); $template2 = $this->createMock(ExportTemplate::class); $template2->method('getId')->willReturn(2); $template2->method('getTitle')->willReturn('XLSX Test'); $template2->method('getLanguage')->willReturn('it'); $template2->method('getRenderer')->willReturn('xlsx'); $template2->method('getColumns')->willReturn(['date', 'begin', 'duration', 'rate', 'user.name']); $template3 = $this->createMock(ExportTemplate::class); $template3->method('getTitle')->willReturn('XLSX Test'); $template3->method('getLanguage')->willReturn('it'); $template3->method('getRenderer')->willReturn('foo'); // invalid renderer will be ignored $template3->method('getColumns')->willReturn(['date', 'begin', 'duration', 'rate', 'user.name']); $logger->expects($this->exactly($failureCount))->method('error')->with('Unknown export template type: ' . $template3->getRenderer()); $templates = [$template1, $template2, $template3]; } $repository->method('findAll')->willReturn($templates); return new ServiceExport( $this->createMock(EventDispatcherInterface::class), (new HtmlRendererFactoryMock($this))->create(), (new PdfRendererFactoryMock($this))->create(), (new CsvRendererFactoryMock($this))->create(), (new XlsxRendererFactoryMock($this))->create(), $repository, $logger, ); } public function testEmptyObject(): void { $sut = $this->createSut(); self::assertEmpty($sut->getRenderer()); self::assertNull($sut->getRendererById('default')); self::assertEmpty($sut->getTimesheetExporter()); self::assertNull($sut->getTimesheetExporterById('default')); } public function testAddRenderer(): void { $sut = $this->createSut(); $renderer = new HtmlRenderer( $this->createMock(Environment::class), new EventDispatcher(), $this->createMock(ProjectStatisticService::class), $this->createMock(ActivityStatisticService::class) ); $sut->addRenderer($renderer); self::assertEquals(1, \count($sut->getRenderer())); self::assertSame($renderer, $sut->getRendererById('html')); } public function testAddTimesheetExporter(): void { $sut = $this->createSut(); $exporter = new HtmlExporter($this->createMock(Environment::class), new EventDispatcher(), $this->createMock(ProjectStatisticService::class), $this->createMock(ActivityStatisticService::class)); $sut->addTimesheetExporter($exporter); self::assertEquals(1, \count($sut->getTimesheetExporter())); self::assertSame($exporter, $sut->getTimesheetExporterById('print')); } public function testAddExportRepository(): void { $sut = $this->createSut(); $repository = $this->createMock(ExportRepositoryInterface::class); $repository->expects($this->once())->method('getExportItemsForQuery')->willReturn([]); $sut->addExportRepository($repository); $query = new ExportQuery(); $items = $sut->getExportItems($query); self::assertEquals([], $items); } public function testWithTemplates(): void { $sut = $this->createSut(true, 5); $renderer = $sut->getRenderer(); self::assertCount(2, $renderer); self::assertInstanceOf(CsvRenderer::class, $renderer[0]); self::assertInstanceOf(XlsxRenderer::class, $renderer[1]); self::assertInstanceOf(CsvRenderer::class, $sut->getRendererById('1')); self::assertNull($sut->getRendererById('default')); self::assertNull($sut->getRendererById('csv')); self::assertNull($sut->getRendererById('xlsx')); } }