c87c86b652525b5cb6c59f91256f5283cbafa047
[yaffs-website] / vendor / mikey179 / vfsStream / src / test / php / org / bovigo / vfs / vfsStreamWrapperQuotaTestCase.php
1 <?php
2 /**
3  * This file is part of vfsStream.
4  *
5  * For the full copyright and license information, please view the LICENSE
6  * file that was distributed with this source code.
7  *
8  * @package  org\bovigo\vfs
9  */
10 namespace org\bovigo\vfs;
11 /**
12  * Test for quota related functionality of org\bovigo\vfs\vfsStreamWrapper.
13  *
14  * @group  issue_35
15  */
16 class vfsStreamWrapperQuotaTestCase extends \PHPUnit_Framework_TestCase
17 {
18     /**
19      * access to root
20      *
21      * @type  vfsStreamDirectory
22      */
23     private $root;
24
25     /**
26      * set up test environment
27      */
28     public function setUp()
29     {
30         $this->root = vfsStream::setup();
31         vfsStream::setQuota(10);
32     }
33
34     /**
35      * @test
36      */
37     public function writeLessThanQuotaWritesEverything()
38     {
39         $this->assertEquals(9, file_put_contents(vfsStream::url('root/file.txt'), '123456789'));
40         $this->assertEquals('123456789', $this->root->getChild('file.txt')->getContent());
41     }
42
43     /**
44      * @test
45      */
46     public function writeUpToQotaWritesEverything()
47     {
48         $this->assertEquals(10, file_put_contents(vfsStream::url('root/file.txt'), '1234567890'));
49         $this->assertEquals('1234567890', $this->root->getChild('file.txt')->getContent());
50     }
51
52     /**
53      * @test
54      */
55     public function writeMoreThanQotaWritesOnlyUpToQuota()
56     {
57         try {
58             file_put_contents(vfsStream::url('root/file.txt'), '12345678901');
59         } catch (\PHPUnit_Framework_Error $e) {
60             $this->assertEquals('file_put_contents(): Only 10 of 11 bytes written, possibly out of free disk space',
61                                 $e->getMessage()
62             );
63         }
64
65         $this->assertEquals('1234567890', $this->root->getChild('file.txt')->getContent());
66     }
67
68     /**
69      * @test
70      */
71     public function considersAllFilesForQuota()
72     {
73         vfsStream::newFile('foo.txt')
74                  ->withContent('foo')
75                  ->at(vfsStream::newDirectory('bar')
76                                ->at($this->root)
77                    );
78         try {
79             file_put_contents(vfsStream::url('root/file.txt'), '12345678901');
80         } catch (\PHPUnit_Framework_Error $e) {
81             $this->assertEquals('file_put_contents(): Only 7 of 11 bytes written, possibly out of free disk space',
82                                 $e->getMessage()
83             );
84         }
85
86         $this->assertEquals('1234567', $this->root->getChild('file.txt')->getContent());
87     }
88
89     /**
90      * @test
91      * @group  issue_33
92      */
93     public function truncateToLessThanQuotaWritesEverything()
94     {
95         if (version_compare(PHP_VERSION, '5.4.0', '<')) {
96             $this->markTestSkipped('Requires PHP 5.4');
97         }
98
99         if (strstr(PHP_VERSION, 'hiphop') !== false) {
100             $this->markTestSkipped('Not supported on hhvm');
101         }
102
103         $fp = fopen(vfsStream::url('root/file.txt'), 'w+');
104         $this->assertTrue(ftruncate($fp, 9));
105         fclose($fp);
106         $this->assertEquals(9,
107                             $this->root->getChild('file.txt')->size()
108         );
109         $this->assertEquals("\0\0\0\0\0\0\0\0\0",
110                             $this->root->getChild('file.txt')->getContent()
111         );
112     }
113
114     /**
115      * @test
116      * @group  issue_33
117      */
118     public function truncateUpToQotaWritesEverything()
119     {
120         if (version_compare(PHP_VERSION, '5.4.0', '<')) {
121             $this->markTestSkipped('Requires PHP 5.4');
122         }
123
124         if (strstr(PHP_VERSION, 'hiphop') !== false) {
125             $this->markTestSkipped('Not supported on hhvm');
126         }
127
128         $fp = fopen(vfsStream::url('root/file.txt'), 'w+');
129         $this->assertTrue(ftruncate($fp, 10));
130         fclose($fp);
131         $this->assertEquals(10,
132                             $this->root->getChild('file.txt')->size()
133         );
134         $this->assertEquals("\0\0\0\0\0\0\0\0\0\0",
135                             $this->root->getChild('file.txt')->getContent()
136         );
137     }
138
139     /**
140      * @test
141      * @group  issue_33
142      */
143     public function truncateToMoreThanQotaWritesOnlyUpToQuota()
144     {
145         if (version_compare(PHP_VERSION, '5.4.0', '<')) {
146             $this->markTestSkipped('Requires PHP 5.4');
147         }
148
149         if (strstr(PHP_VERSION, 'hiphop') !== false) {
150             $this->markTestSkipped('Not supported on hhvm');
151         }
152
153         $fp = fopen(vfsStream::url('root/file.txt'), 'w+');
154         $this->assertTrue(ftruncate($fp, 11));
155         fclose($fp);
156         $this->assertEquals(10,
157                             $this->root->getChild('file.txt')->size()
158         );
159         $this->assertEquals("\0\0\0\0\0\0\0\0\0\0",
160                             $this->root->getChild('file.txt')->getContent()
161         );
162     }
163
164     /**
165      * @test
166      * @group  issue_33
167      */
168     public function truncateConsidersAllFilesForQuota()
169     {
170         if (version_compare(PHP_VERSION, '5.4.0', '<')) {
171             $this->markTestSkipped('Requires PHP 5.4');
172         }
173
174         if (strstr(PHP_VERSION, 'hiphop') !== false) {
175             $this->markTestSkipped('Not supported on hhvm');
176         }
177
178         vfsStream::newFile('bar.txt')
179                  ->withContent('bar')
180                  ->at(vfsStream::newDirectory('bar')
181                                ->at($this->root)
182                    );
183         $fp = fopen(vfsStream::url('root/file.txt'), 'w+');
184         $this->assertTrue(ftruncate($fp, 11));
185         fclose($fp);
186         $this->assertEquals(7,
187                             $this->root->getChild('file.txt')->size()
188         );
189         $this->assertEquals("\0\0\0\0\0\0\0",
190                             $this->root->getChild('file.txt')->getContent()
191         );
192     }
193
194     /**
195      * @test
196      * @group  issue_33
197      */
198     public function canNotTruncateToGreaterLengthWhenDiscQuotaReached()
199     {
200         if (version_compare(PHP_VERSION, '5.4.0', '<')) {
201             $this->markTestSkipped('Requires PHP 5.4');
202         }
203
204         if (strstr(PHP_VERSION, 'hiphop') !== false) {
205             $this->markTestSkipped('Not supported on hhvm');
206         }
207
208         vfsStream::newFile('bar.txt')
209                  ->withContent('1234567890')
210                  ->at(vfsStream::newDirectory('bar')
211                                ->at($this->root)
212                    );
213         $fp = fopen(vfsStream::url('root/file.txt'), 'w+');
214         $this->assertFalse(ftruncate($fp, 11));
215         fclose($fp);
216         $this->assertEquals(0,
217                             $this->root->getChild('file.txt')->size()
218         );
219         $this->assertEquals('',
220                             $this->root->getChild('file.txt')->getContent()
221         );
222     }
223 }