createMock(Security::class); $security->expects($this->any())->method('getUser')->willReturn(new User()); $security->expects($this->any())->method('isGranted')->willReturn(true); $translator = $this->createMock(TranslatorInterface::class); $dispatcher = new EventDispatcher(); $dispatcher->addSubscriber(new MetaFieldColumnSubscriber()); return new CsvRenderer(new SpreadsheetRenderer($dispatcher, $security), $translator); } public function testConfiguration(): void { $sut = $this->getAbstractRenderer(); self::assertEquals('csv', $sut->getId()); self::assertEquals('csv', $sut->getTitle()); } public static function getTestModel(): array { return [ ['400', '2437.12', '1947.99', 7, 6, 1, 2, 2] ]; } /** * @dataProvider getTestModel */ public function testRender(string $totalDuration, string $totalRate, string $expectedRate, int $expectedRows, int $expectedDescriptions, int $expectedUser1, int $expectedUser2, int $expectedUser3): void { $sut = $this->getAbstractRenderer(); /** @var BinaryFileResponse $response */ $response = $this->render($sut); $file = $response->getFile(); $prefix = date('Ymd'); self::assertEquals('text/csv', $response->headers->get('Content-Type')); self::assertEquals('attachment; filename=' . $prefix . '-Customer_Name-project_name.csv', $response->headers->get('Content-Disposition')); self::assertTrue(file_exists($file->getRealPath())); $content = file_get_contents($file->getRealPath()); self::assertIsString($content); self::assertStringContainsString($expectedRate, $content); self::assertEquals($expectedRows, substr_count($content, PHP_EOL)); self::assertEquals($expectedDescriptions, substr_count($content, '"activity description"')); self::assertEquals($expectedUser1, substr_count($content, ',kevin,')); self::assertEquals($expectedUser3, substr_count($content, ',hello-world,')); self::assertEquals($expectedUser2, substr_count($content, ',foo-bar,')); ob_start(); $response->sendContent(); $content2 = ob_get_clean(); self::assertIsString($content2); self::assertEquals($content, $content2); self::assertFalse(file_exists($file->getRealPath())); $all = []; $rows = str_getcsv($content2, PHP_EOL); foreach ($rows as $row) { self::assertIsString($row); $all[] = str_getcsv($row); } $expected = [ '2019-06-16', '12:00', '12:06', '0:06', //'0.11', 'EUR', '0', '0', '0', '84', 'Kevin', 'kevin', '', 'Customer Name', 'project name', 'activity description', '', '1', 'foo, bar', 'timesheet', 'work', 'A-0123456789', '', 'DE-9876543210', 'ORDER-123', 'meta-bar', 'meta-bar2', 'customer-bar', '', 'project-foo2', 'activity-bar', ]; $expected2 = [ '2019-06-16', '12:00', '12:06', '0:06', //'0.11', 'EUR', '0', '0', '0', '-100.92', 'niveK', 'nivek', '', 'Customer Name', 'project name', 'activity description', '', '1', '', 'timesheet', 'work', 'A-0123456789', '', 'DE-9876543210', 'ORDER-123', '', '', 'customer-bar', '', 'project-foo2', 'activity-bar', ]; self::assertEquals(7, \count($all)); self::assertEquals($expected, $all[5]); self::assertEquals($expected2, $all[6]); self::assertEquals(\count($expected), \count($all[0])); self::assertEquals('foo', $all[4][17]); } }