Completed
Push — master ( 7de777...9be641 )
by
unknown
11:28
created

FileProviderTest::testFileDownload()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 24
nc 1
nop 0
1
<?php
2
3
namespace MediaMonks\SonataMediaBundle\Tests\Functional;
4
5
use MediaMonks\SonataMediaBundle\Handler\SignatureParameterHandler;
6
use MediaMonks\SonataMediaBundle\Model\MediaInterface;
7
use MediaMonks\SonataMediaBundle\ParameterBag\DownloadParameterBag;
8
use Symfony\Component\DomCrawler\Crawler;
9
use Mockery as m;
10
11
class FileProviderTest extends AdminTestAbstract
12
{
13
    public function testFile()
14
    {
15
        $crawler = $this->uploadFile();
16
17
        $form = $crawler->selectButton('Update')->form();
18
19
        $this->assertSonataFormValues($form, ['title' => 'text']);
20
21
        $this->client->request('GET', self::BASE_PATH.'list');
22
        $this->assertContains('text', $this->client->getResponse()->getContent());
23
24
        $this->assertNumberOfFilesInPath(2, $this->getMediaPathPrivate());
25
    }
26
27
    public function testFileDownload()
28
    {
29
        $crawler = $this->uploadFile();
30
31
        $link = $crawler->selectLink('Download original')->link();
32
33
        ob_start();
34
        $this->client->click($link);
35
        $fileContents = $content = ob_get_contents();
0 ignored issues
show
Unused Code introduced by
$content is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
36
        ob_end_clean();
37
38
        $this->assertEquals('foobar', $fileContents);
39
        $this->assertTrue($this->client->getResponse()->isSuccessful(), 'response status is 2xx');
40
41
        $media = m::mock(MediaInterface::class);
42
        $media->shouldReceive('getId')->andReturn(1);
43
44
        $parameterBag = new DownloadParameterBag();
45
        $signature = new SignatureParameterHandler(self::$kernel->getContainer()->getParameter('secret'));
46
        $parameters = $signature->getRouteParameters($media, $parameterBag);
47
48
        ob_start();
49
        $this->client->request(
50
            'GET',
51
            sprintf(
52
                '/media/download/%d?s=%s',
53
                $media->getId(),
0 ignored issues
show
Bug introduced by
The method getId() does not seem to exist on object<Mockery\MockInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54
                $parameters['s']
55
            )
56
        );
57
        $fileContents = $content = ob_get_contents();
0 ignored issues
show
Unused Code introduced by
$content is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
58
        ob_end_clean();
59
60
        $this->assertEquals('foobar', $fileContents);
61
    }
62
63
    public function testInvalidFileUpload()
64
    {
65
        $this->uploadFile('text.exe');
66
67
        $this->assertContains('An error has occurred during the creation of item', $this->client->getResponse()->getContent());
68
        $this->assertContains('not allowed to upload a file with extension', $this->client->getResponse()->getContent());
69
    }
70
71 View Code Duplication
    public function testEmptyFileUpload()
0 ignored issues
show
Duplication introduced by
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...
72
    {
73
        $provider = 'file';
74
75
        $crawler = $this->client->request('GET', self::BASE_PATH.'create?provider='.$provider);
76
77
        $form = $crawler->selectButton('Create')->form();
78
79
        $this->assertSonataFormValues(
80
            $form,
81
            [
82
                'provider' => $provider,
83
            ]
84
        );
85
86
        $this->client->submit($form);
87
88
        $this->assertContains('This value should not be blank', $this->client->getResponse()->getContent());
89
        $this->assertContains('This value should not be blank', $this->client->getResponse()->getContent());
90
    }
91
92
    /**
93
     * @param string $fileName
94
     * @return Crawler
95
     */
96
    private function uploadFile($fileName = 'text.txt')
97
    {
98
        $provider = 'file';
99
100
        $crawler = $this->client->request('GET', self::BASE_PATH.'create?provider='.$provider);
101
102
        $form = $crawler->selectButton('Create')->form();
103
104
        $this->assertSonataFormValues(
105
            $form,
106
            [
107
                'provider' => $provider,
108
            ]
109
        );
110
111
        $this->setFormBinaryContent($form, $this->getFixturesPath().$fileName);
112
113
        $crawler = $this->client->submit($form);
114
115
        return $crawler;
116
    }
117
}
118