Completed
Pull Request — master (#433)
by Albin
10:58
created

LocalSpec   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 18
lcom 2
cbo 1
dl 0
loc 154
rs 10
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 6 1
A it_is_adapter() 0 4 1
A it_is_checksum_calculator() 0 4 1
A it_is_a_mime_type_provider() 0 4 1
A it_gets_the_file_mime_type() 0 4 1
A it_is_stream_factory() 0 4 1
A it_reads_file() 0 4 1
A it_writes_file() 0 4 1
A it_renames_file() 0 4 1
A it_checks_if_file_exists() 0 5 1
A it_fetches_keys() 0 6 1
A it_fetches_mtime() 0 5 1
A it_deletes_file() 0 5 1
A it_checks_if_given_key_is_directory() 0 5 1
A it_creates_local_stream() 0 4 1
A it_does_not_allow_to_read_path_above_main_file_directory() 0 11 1
A it_fails_when_directory_does_not_exists() 0 49 1
A it_creates_directory_when_does_not_exists() 0 6 1
1
<?php
2
3
namespace spec\Gaufrette\Adapter\Local;
4
5
use Gaufrette\Adapter\Local\LocalStream;
6
use org\bovigo\vfs\vfsStream;
7
use PhpSpec\ObjectBehavior;
8
9
class LocalSpec extends ObjectBehavior
10
{
11
    function let()
12
    {
13
        vfsStream::setup('test');
14
        vfsStream::copyFromFileSystem(__DIR__.'/MockFilesystem');
15
        $this->beConstructedWith(vfsStream::url('test'));
16
    }
17
18
    function it_is_adapter()
19
    {
20
        $this->shouldHaveType('Gaufrette\Adapter');
21
    }
22
23
    function it_is_checksum_calculator()
24
    {
25
        $this->shouldHaveType('Gaufrette\Adapter\ChecksumCalculator');
26
    }
27
28
    function it_is_a_mime_type_provider()
29
    {
30
        $this->shouldHaveType('Gaufrette\Adapter\MimeTypeProvider');
31
    }
32
33
    function it_gets_the_file_mime_type()
34
    {
35
        $this->mimeType('filename')->shouldReturn('text/plain');
36
    }
37
38
    function it_is_stream_factory()
39
    {
40
        $this->shouldHaveType('Gaufrette\Adapter\StreamFactory');
41
    }
42
43
    function it_reads_file()
44
    {
45
        $this->read('filename')->shouldReturn("content\n");
46
    }
47
48
    function it_writes_file()
49
    {
50
        $this->write('filename', 'some content')->shouldReturn(12);
51
    }
52
53
    function it_renames_file()
54
    {
55
        $this->rename('filename', 'aaa/filename2')->shouldReturn(true);
56
    }
57
58
    function it_checks_if_file_exists()
59
    {
60
        $this->exists('filename')->shouldReturn(true);
61
        $this->exists('filename1')->shouldReturn(false);
62
    }
63
64
    function it_fetches_keys()
65
    {
66
        $expectedKeys = array('filename', 'dir', 'dir/file');
67
        sort($expectedKeys);
68
        $this->keys()->shouldReturn($expectedKeys);
69
    }
70
71
    function it_fetches_mtime()
72
    {
73
        $mtime = filemtime(vfsStream::url('test/filename'));
74
        $this->mtime('filename')->shouldReturn($mtime);
75
    }
76
77
    function it_deletes_file()
78
    {
79
        $this->delete('filename')->shouldReturn(true);
80
        $this->delete('filename1')->shouldReturn(false);
81
    }
82
83
    function it_checks_if_given_key_is_directory()
84
    {
85
        $this->isDirectory('dir')->shouldReturn(true);
86
        $this->isDirectory('filename')->shouldReturn(false);
87
    }
88
89
    function it_creates_local_stream()
90
    {
91
        $this->createStream('filename')->shouldReturnAnInstanceOf(LocalStream::class);
92
    }
93
94
    function it_does_not_allow_to_read_path_above_main_file_directory()
95
    {
96
        $this
97
            ->shouldThrow(new \OutOfBoundsException(sprintf('The path "%s" is out of the filesystem.', vfsStream::url('filename'))))
98
            ->duringRead('../filename')
99
        ;
100
        $this
101
            ->shouldThrow(new \OutOfBoundsException(sprintf('The path "%s" is out of the filesystem.', vfsStream::url('filename'))))
102
            ->duringExists('../filename')
103
        ;
104
    }
105
106
    function it_fails_when_directory_does_not_exists()
107
    {
108
        $this->beConstructedWith(vfsStream::url('other'));
109
110
        $this
111
            ->shouldThrow(new \RuntimeException(sprintf('The directory "%s" does not exist.', vfsStream::url('other'))))
112
            ->duringRead('filename')
113
        ;
114
        $this
115
            ->shouldThrow(new \RuntimeException(sprintf('The directory "%s" does not exist.', vfsStream::url('other'))))
116
            ->duringWrite('filename', 'some content')
117
        ;
118
        $this
119
            ->shouldThrow(new \RuntimeException(sprintf('The directory "%s" does not exist.', vfsStream::url('other'))))
120
            ->duringRename('filename', 'otherFilename')
121
        ;
122
        $this
123
            ->shouldThrow(new \RuntimeException(sprintf('The directory "%s" does not exist.', vfsStream::url('other'))))
124
            ->duringExists('filename')
125
        ;
126
        $this
127
            ->shouldThrow(new \RuntimeException(sprintf('The directory "%s" does not exist.', vfsStream::url('other'))))
128
            ->duringKeys()
129
        ;
130
        $this
131
            ->shouldThrow(new \RuntimeException(sprintf('The directory "%s" does not exist.', vfsStream::url('other'))))
132
            ->duringMtime('filename')
133
        ;
134
        $this
135
            ->shouldThrow(new \RuntimeException(sprintf('The directory "%s" does not exist.', vfsStream::url('other'))))
136
            ->duringDelete('filename')
137
        ;
138
        $this
139
            ->shouldThrow(new \RuntimeException(sprintf('The directory "%s" does not exist.', vfsStream::url('other'))))
140
            ->duringIsDirectory('filename')
141
        ;
142
        $this
143
            ->shouldThrow(new \RuntimeException(sprintf('The directory "%s" does not exist.', vfsStream::url('other'))))
144
            ->duringCreateStream('filename')
145
        ;
146
        $this
147
            ->shouldThrow(new \RuntimeException(sprintf('The directory "%s" does not exist.', vfsStream::url('other'))))
148
            ->duringChecksum('filename')
149
        ;
150
        $this
151
            ->shouldThrow(new \RuntimeException(sprintf('The directory "%s" does not exist.', vfsStream::url('other'))))
152
            ->duringMimeType('filename')
153
        ;
154
    }
155
156
    function it_creates_directory_when_does_not_exists()
157
    {
158
        $this->beConstructedWith(vfsStream::url('test/other'), true);
159
160
        $this->exists('/')->shouldReturn(true);
161
    }
162
}
163