1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\AdminListBundle\Tests\Service; |
4
|
|
|
|
5
|
|
|
use Box\Spout\Common\Type; |
6
|
|
|
use Kunstmaan\AdminListBundle\AdminList\ExportableInterface; |
7
|
|
|
use Kunstmaan\AdminListBundle\Service\ExportService; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
10
|
|
|
use Symfony\Component\Translation\Translator; |
11
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
12
|
|
|
use Twig\Environment; |
13
|
|
|
|
14
|
|
|
class ExportServiceTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var ExportService |
18
|
|
|
*/ |
19
|
|
|
protected $object; |
20
|
|
|
|
21
|
|
|
protected function setUp() |
22
|
|
|
{ |
23
|
|
|
$this->object = new ExportService($this->createMock(Environment::class), new Translator('nl')); |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @group legacy |
28
|
|
|
* @expectedDeprecation Not passing the Twig service as the first argument of "Kunstmaan\AdminListBundle\Service\ExportService::__construct" is deprecated since KunstmaanAdminListBundle 5.4 and will be required in KunstmaanAdminListBundle 6.0. Injected the required services in the constructor instead. |
29
|
|
|
*/ |
30
|
|
|
public function testWithoutConstructorInjection() |
31
|
|
|
{ |
32
|
|
|
new ExportService(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @group legacy |
37
|
|
|
* @expectedDeprecation Not passing the Twig service as the first argument of "Kunstmaan\AdminListBundle\Service\ExportService::__construct" is deprecated since KunstmaanAdminListBundle 5.4 and will be required in KunstmaanAdminListBundle 6.0. Injected the required services in the constructor instead. |
38
|
|
|
* @expectedDeprecation Injecting the template renderer with "Kunstmaan\AdminListBundle\Service\ExportService::setRenderer" is deprecated since KunstmaanAdminListBundle 5.4 and will be removed in KunstmaanAdminListBundle 6.0. Inject Twig with constructor injection instead. |
39
|
|
|
* @expectedDeprecation Injecting the translator with "Kunstmaan\AdminListBundle\Service\ExportService::setTranslator" is deprecated since KunstmaanAdminListBundle 5.4 and will be removed in KunstmaanAdminListBundle 6.0. Inject the Translator with constructor injection instead. |
40
|
|
|
*/ |
41
|
|
|
public function testDeprecatedSetters() |
42
|
|
|
{ |
43
|
|
|
$obj = new ExportService(); |
44
|
|
|
$obj->setRenderer($this->createMock(EngineInterface::class)); |
|
|
|
|
45
|
|
|
$obj->setTranslator($this->createMock(TranslatorInterface::class)); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testConstructorInvalidTranslatorlass() |
49
|
|
|
{ |
50
|
|
|
$this->expectException(\InvalidArgumentException::class); |
51
|
|
|
$this->expectExceptionMessage('Argument 2 passed to "Kunstmaan\AdminListBundle\Service\ExportService::__construct" must be of the type "Symfony\Component\Translation\TranslatorInterface" or "Symfony\Contracts\Translation\TranslatorInterface", "stdClass" given'); |
52
|
|
|
|
53
|
|
|
new ExportService($this->createMock(Environment::class), new \stdClass()); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testGetSupportedExtensions() |
57
|
|
|
{ |
58
|
|
|
$extensions = ExportService::getSupportedExtensions(); |
59
|
|
|
$this->assertEquals(['Csv' => 'csv', 'Ods' => 'ods', 'Excel' => 'xlsx'], $extensions); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
View Code Duplication |
public function testGetDownloadableResponseReturnsStreamedResponseWithExcel() |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
/** @var ExportableInterface $adminList */ |
65
|
|
|
$adminList = $this->createMock('Kunstmaan\AdminListBundle\AdminList\ExportableInterface'); |
66
|
|
|
|
67
|
|
|
$response = $this->object->getDownloadableResponse($adminList, Type::XLSX); |
68
|
|
|
|
69
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $response); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
View Code Duplication |
public function testGetDownloadableResponseReturnsStreamedResponseWithOds() |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
/** @var ExportableInterface $adminList */ |
75
|
|
|
$adminList = $this->createMock('Kunstmaan\AdminListBundle\AdminList\ExportableInterface'); |
76
|
|
|
|
77
|
|
|
$response = $this->object->getDownloadableResponse($adminList, Type::ODS); |
78
|
|
|
|
79
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $response); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
View Code Duplication |
public function testGetDownloadableResponseReturnsStreamedResponseWithCsv() |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
/** @var ExportableInterface $adminList */ |
85
|
|
|
$adminList = $this->createMock('Kunstmaan\AdminListBundle\AdminList\ExportableInterface'); |
86
|
|
|
|
87
|
|
|
$response = $this->object->getDownloadableResponse($adminList, Type::CSV); |
88
|
|
|
|
89
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\StreamedResponse', $response); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: