1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\MediaBundle\Tests\Helper\File; |
4
|
|
|
|
5
|
|
|
use Kunstmaan\MediaBundle\Entity\Media; |
6
|
|
|
use Kunstmaan\MediaBundle\Helper\File\PdfHandler; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
9
|
|
|
use Symfony\Component\HttpFoundation\File\File; |
10
|
|
|
|
11
|
|
|
class PdfHandlerTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
/** @var PdfHandler */ |
14
|
|
|
protected $object; |
15
|
|
|
|
16
|
|
|
protected $pdfTransformer; |
17
|
|
|
|
18
|
|
|
/** @var string */ |
19
|
|
|
protected $filesDir; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Sets up the fixture, for example, opens a network connection. |
23
|
|
|
* This method is called before a test is executed. |
24
|
|
|
*/ |
25
|
|
|
protected function setUp() |
26
|
|
|
{ |
27
|
|
|
$this->pdfTransformer = $this->createMock('Kunstmaan\MediaBundle\Helper\Transformer\PreviewTransformerInterface'); |
28
|
|
|
$mockMimeTypeGuesserfactory = $this->createMock('Kunstmaan\MediaBundle\Helper\MimeTypeGuesserFactoryInterface'); |
29
|
|
|
$mockExtensionGuesserfactory = $this->createMock('Kunstmaan\MediaBundle\Helper\ExtensionGuesserFactoryInterface'); |
30
|
|
|
$this->filesDir = realpath(__DIR__ . '/../../Files'); |
31
|
|
|
|
32
|
|
|
$this->object = new PdfHandler(1, $mockMimeTypeGuesserfactory, $mockExtensionGuesserfactory); |
|
|
|
|
33
|
|
|
$this->object->setPdfTransformer($this->pdfTransformer); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testGetType() |
37
|
|
|
{ |
38
|
|
|
$this->assertEquals(PdfHandler::TYPE, $this->object->getType()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testCanHandlePdfFiles() |
42
|
|
|
{ |
43
|
|
|
$media = new Media(); |
44
|
|
|
$media->setContent(new File($this->filesDir . '/sample.pdf')); |
45
|
|
|
$media->setContentType('application/pdf'); |
46
|
|
|
|
47
|
|
|
$this->assertTrue($this->object->canHandle($media)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testCannotHandleNonPdfFiles() |
51
|
|
|
{ |
52
|
|
|
$media = new Media(); |
53
|
|
|
$media->setContentType('image/jpg'); |
54
|
|
|
|
55
|
|
|
$this->assertFalse($this->object->canHandle($media)); |
56
|
|
|
$this->assertFalse($this->object->canHandle(new \stdClass())); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testGetImageUrl() |
60
|
|
|
{ |
61
|
|
|
$this->pdfTransformer |
62
|
|
|
->expects($this->any()) |
63
|
|
|
->method('getPreviewFilename') |
64
|
|
|
->willReturn('/media.pdf.jpg'); |
65
|
|
|
|
66
|
|
|
$media = new Media(); |
67
|
|
|
$media->setUrl('/path/to/media.pdf'); |
68
|
|
|
$this->assertNull($this->object->getImageUrl($media, '/basepath')); |
69
|
|
|
|
70
|
|
|
$previewFilename = sys_get_temp_dir() . '/media.pdf.jpg'; |
71
|
|
|
$fileSystem = new Filesystem(); |
72
|
|
|
$fileSystem->touch($previewFilename); |
73
|
|
|
$media->setUrl('/media.pdf'); |
74
|
|
|
$this->object->setWebPath(sys_get_temp_dir()); |
75
|
|
|
$this->assertEquals('/media.pdf.jpg', $this->object->getImageUrl($media, '')); |
76
|
|
|
$fileSystem->remove($previewFilename); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
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: