Completed
Push — feature/EVO-5985 ( 3c8987...358956 )
by
unknown
61:14
created

FileManagerTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 172
Duplicated Lines 19.19 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
c 1
b 0
f 1
lcom 1
cbo 5
dl 33
loc 172
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 9 1
A testHas() 11 11 1
A testRead() 11 11 1
A testDelete() 11 11 1
B testSaveFiles() 0 92 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Test suite for the FileManager
4
 */
5
6
namespace Graviton\FileBundle\Tests;
7
8
use Graviton\FileBundle\FileManager;
9
use Graviton\TestBundle\Test\WebTestCase;
10
use Symfony\Component\HttpFoundation\File\UploadedFile;
11
use Symfony\Component\HttpFoundation\Response;
12
13
/**
14
 * Basic functional test for /file
15
 *
16
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
17
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
18
 * @link     http://swisscom.ch
19
 */
20
class FileManagerTest extends WebTestCase
21
{
22
    /** @var \Gaufrette\Filesystem $fileSystem */
23
    private $fileSystem;
24
25
    /** @var \Graviton\FileBundle\FileDocumentFactory $fileDocumentFactory */
26
    private $fileDocumentFactory;
27
28
    /**
29
     * Initiates mandatory properties
30
     *
31
     * @return void
32
     */
33
    public function setUp()
34
    {
35
        $this->fileSystem = $this->getMockBuilder('\Gaufrette\Filesystem')
36
            ->disableOriginalConstructor()
37
            ->getMock();
38
39
        $this->fileDocumentFactory = $this->getMockBuilder('\Graviton\FileBundle\FileDocumentFactory')
40
            ->getMock();
41
    }
42
43
    /**
44
     * Verifies the correct behavior of has method
45
     *
46
     * @return void
47
     */
48 View Code Duplication
    public function testHas()
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...
49
    {
50
        $this->fileSystem
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Gaufrette\Filesystem>.

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...
51
            ->expects($this->once())
52
            ->method('has')
53
            ->willReturn(true);
54
55
        $manager = new FileManager($this->fileSystem, $this->fileDocumentFactory);
56
57
        $this->assertTrue($manager->has('myKey'));
58
    }
59
60
    /**
61
     * Verifies the correct behavior of read method
62
     *
63
     * @return void
64
     */
65 View Code Duplication
    public function testRead()
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...
66
    {
67
        $this->fileSystem
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Gaufrette\Filesystem>.

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...
68
            ->expects($this->once())
69
            ->method('read')
70
            ->willReturn('myData');
71
72
        $manager = new FileManager($this->fileSystem, $this->fileDocumentFactory);
73
74
        $this->assertEquals('myData', $manager->read('myKey'));
75
    }
76
77
    /**
78
     * Verifies the correct behavior of read method
79
     *
80
     * @return void
81
     */
82 View Code Duplication
    public function testDelete()
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...
83
    {
84
        $this->fileSystem
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Gaufrette\Filesystem>.

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...
85
            ->expects($this->once())
86
            ->method('delete')
87
            ->willReturn(true);
88
89
        $manager = new FileManager($this->fileSystem, $this->fileDocumentFactory);
90
91
        $this->assertTrue($manager->delete('myKey'));
92
    }
93
94
    /**
95
     * Verifies the correct behavior of the FileManager
96
     *
97
     * @return void
98
     */
99
    public function testSaveFiles()
100
    {
101
        $jsonData = '{
102
          "links": [
103
            {
104
              "$ref": "http://localhost/testcase/readonly/101",
105
              "type": "owner"
106
            },
107
            {
108
              "$ref": "http://localhost/testcase/readonly/102",
109
              "type": "module"
110
            }
111
          ],
112
          "metadata": {
113
            "action":[{"command":"print"},{"command":"archive"}]
114
          }
115
        }';
116
117
        copy(__DIR__ . '/Fixtures/test.txt', sys_get_temp_dir() . '/test.txt');
118
        $file = sys_get_temp_dir() . '/test.txt';
119
        $uploadedFile = new UploadedFile($file, 'test.txt', 'text/plain', 15);
120
        $client = $this->createClient();
121
        $client->request(
122
            'POST',
123
            '/file',
124
            [
125
                'metadata' => $jsonData,
126
            ],
127
            [
128
                'upload' => $uploadedFile,
129
            ]
130
        );
131
        $response = $client->getResponse();
132
        $location = $response->headers->get('location');
133
134
        $this->assertEquals(Response::HTTP_CREATED, $response->getStatusCode());
135
        $this->assertContains('/file/', $location);
136
137
        // receive generated file information
138
        $client = $this->createClient();
139
        $client->request(
140
            'GET',
141
            $location,
142
            [],
143
            [],
144
            [
145
                'HTTP_ACCEPT' => 'application/json'
146
            ]
147
        );
148
149
        $response = $client->getResponse();
150
        $contentArray = json_decode($response->getContent(), true);
151
152
        $this->assertEquals([["command" => "print"], ["command" => "archive"]], $contentArray['metadata']['action']);
153
        $this->assertJsonStringEqualsJsonString(
154
            '[
155
              {
156
                "$ref": "http://localhost/testcase/readonly/101",
157
                "type": "owner"
158
              },
159
              {
160
                "$ref": "http://localhost/testcase/readonly/102",
161
                "type": "module"
162
              }
163
            ]',
164
            json_encode($contentArray['links'])
165
        );
166
167
        // Test Metadata, and Remove date.
168
        unset($contentArray['metadata']['modificationDate']);
169
        $this->assertJsonStringEqualsJsonString(
170
            '{
171
                "size":16,
172
                "action":[
173
                    {"command":"print"},
174
                    {"command":"archive"}
175
                ],
176
                "mime":"text\/plain",
177
                "filename":"test.txt",
178
                "hash":"4f3cbec0e58903d8bdcbd03d283cf43ed49a95d8d8b341ee38c0ba085204e2d5",
179
                "additionalProperties":[]
180
             }',
181
            json_encode($contentArray['metadata'])
182
        );
183
184
        // clean up
185
        $client = $this->createClient();
186
        $client->request(
187
            'DELETE',
188
            $location
189
        );
190
    }
191
}
192