Completed
Pull Request — 5.0 (#2066)
by Jeroen
31:48 queued 20:01
created

Tests/Helper/Transformer/PdfTransformerTest.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\MediaBundle\Tests\Helper\Transformer;
4
5
use Kunstmaan\MediaBundle\Helper\Transformer\PdfTransformer;
6
use Symfony\Component\Filesystem\Filesystem;
7
8
/**
9
 * Generated by PHPUnit_SkeletonGenerator on 2014-05-16 at 11:21:53.
10
 */
11
class PdfTransformerTest extends \PHPUnit_Framework_TestCase
12
{
13
    /** @var PdfTransformer */
14
    protected $object;
15
16
    /** @var Filesystem  */
17
    protected $filesystem;
18
19
    /** @var string */
20
    protected $filesDir;
21
22
    /** @var string */
23
    protected $tempDir;
24
25
    /**
26
     * @covers Kunstmaan\MediaBundle\Helper\Transformer\PdfTransformer::__construct
27
     */
28
    protected function setUp()
29
    {
30
        if (!class_exists('Imagick')) {
31
            $this->markTestSkipped('Imagick is not available.');
32
        }
33
        $this->filesDir = realpath(__DIR__ . '/../../Files');
34
        $this->tempDir = str_replace('/', DIRECTORY_SEPARATOR, sys_get_temp_dir().'/kunstmaan_media_test');
35
36
        $this->filesystem = new Filesystem();
37
        $this->removeTempDir();
38
        $this->filesystem->mkdir($this->tempDir);
39
        $this->object = new PdfTransformer(new \Imagick());
40
    }
41
42
    protected function tearDown()
43
    {
44
        if (!$this->filesystem) {
45
            return;
46
        }
47
48
        $this->removeTempDir();
49
    }
50
51
    /**
52
     * @covers Kunstmaan\MediaBundle\Helper\Transformer\PdfTransformer::apply
53
     */
54 View Code Duplication
    public function testApplyWritesJpg()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
    {
56
        $pdfFilename = $this->tempDir . '/sample.pdf';
57
        $jpgFilename = $pdfFilename.'.jpg';
58
59
        $pdf = $this->filesDir . '/sample.pdf';
60
        $this->filesystem->copy($pdf, $pdfFilename);
61
        $this->assertTrue(file_exists($pdfFilename));
62
63
        $transformer = new PdfTransformer(new \Imagick());
64
        $absolutePath = $transformer->apply($pdfFilename);
65
66
        $this->assertEquals($jpgFilename, $absolutePath);
67
        $this->assertTrue(file_exists($jpgFilename));
68
        $this->assertNotEmpty(file_get_contents($jpgFilename));
69
    }
70
71
    /**
72
     * @covers Kunstmaan\MediaBundle\Helper\Transformer\PdfTransformer::apply
73
     */
74 View Code Duplication
    public function testApplyDoesNotOverwriteExisting()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
    {
76
        $pdfFilename = $this->tempDir . '/sample.pdf';
77
        $jpgFilename = $pdfFilename . '.jpg';
78
79
        $pdf = $this->filesDir . '/sample.pdf';
80
        $this->filesystem->copy($pdf, $pdfFilename);
81
        $this->assertTrue(file_exists($pdfFilename));
82
83
        $this->filesystem->touch($jpgFilename);
84
85
        $transformer = new PdfTransformer(new \Imagick());
86
        $absolutePath = $transformer->apply($pdfFilename);
87
88
        $this->assertEquals($jpgFilename, $absolutePath);
89
        $this->assertEmpty(file_get_contents($jpgFilename));
90
    }
91
92
    /**
93
     * @covers Kunstmaan\MediaBundle\Helper\Transformer\PdfTransformer::getPreviewFilename
94
     */
95
    public function testGetPreviewFilename()
96
    {
97
        $pdfFilename = $this->tempDir . '/sample.pdf';
98
        $jpgFilename = $pdfFilename . '.jpg';
99
100
        $this->assertEquals($jpgFilename, $this->object->getPreviewFilename($pdfFilename));
101
    }
102
103
    private function removeTempDir()
104
    {
105
        if ($this->filesystem->exists($this->tempDir)) {
106
            $this->filesystem->remove($this->tempDir);
107
        }
108
    }
109
}
110