Completed
Push — master ( 373d93...1d945d )
by Joschi
02:53
created

RepositoryTest::testGenerateRepositoryEmptyFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 19
rs 9.4285
1
<?php
2
3
/**
4
 * apparat-dev
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Server
8
 * @subpackage  Apparat\Dev\Tests
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Dev\Tests {
38
39
    use Apparat\Dev\Ports\Repository;
40
    use Apparat\Object\Ports\Object;
41
42
    /**
43
     * Repository test
44
     *
45
     * @package Apparat\Dev
46
     * @subpackage Apparat\Dev\Tests
47
     */
48
    class RepositoryTest extends AbstractTest
49
    {
50
        /**
51
         * Tears down the fixture
52
         */
53
        public function tearDown()
54
        {
55
            putenv('MOCK_MKDIR');
56
            putenv('MOCK_TOUCH');
57
            parent::tearDown();
58
        }
59
60
        /**
61
         * Test the generation of a test repository
62
         *
63
         * @expectedException \Apparat\Dev\Ports\InvalidArgumentsException
64
         * @expectedExceptionCode 1464974472
65
         */
66
        public function testGenerateRepositoryInvalidRoot()
67
        {
68
            Repository::generate(null, []);
69
        }
70
71
        /**
72
         * Test the generation of a test repository with an empty object configuration
73
         *
74
         * @expectedException \Apparat\Dev\Ports\InvalidArgumentsException
75
         * @expectedExceptionCode 1464974779
76
         */
77
        public function testGenerateRepositoryInvalidObjectConfig()
78
        {
79
            Repository::generate($this->createTemporaryFileName(), [], Repository::FLAG_CREATE_ROOT_DIRECTORY);
80
        }
81
82
        /**
83
         * Test the generation of a test repository with zero object count
84
         *
85
         * @expectedException \Apparat\Dev\Ports\InvalidArgumentsException
86
         * @expectedExceptionCode 1464975382
87
         */
88
        public function testGenerateRepositoryEmptyObjectCount()
89
        {
90
            $objectConfig = [
91
                Object::ARTICLE => []
92
            ];
93
            Repository::generate(
94
                $this->createTemporaryFileName(),
95
                $objectConfig,
96
                Repository::FLAG_CREATE_ROOT_DIRECTORY
97
            );
98
        }
99
100
        /**
101
         * Test the generation of a test repository
102
         */
103
        public function testGenerateRepositoryEmptyFiles()
104
        {
105
            $objectConfig = [
106
                Object::ARTICLE => [
107
                    Repository::COUNT => 100,
108
                    Repository::HIDDEN => .2,
109
                    Repository::DRAFTS => .2,
110
                ],
111
                Object::EVENT => [
112
                    Repository::COUNT => 100,
113
                ],
114
                Object::CONTACT => [
115
                    Repository::COUNT => 100,
116
                    Repository::REVISIONS => -3,
117
                    Repository::DRAFTS => .2,
118
                ]
119
            ];
120
            $this->generateAndTestRepository($objectConfig, 200, true);
121
        }
122
123
        /**
124
         * Test the repository generation with failing mkdir()
125
         *
126
         * @expectedException \Apparat\Dev\Ports\RuntimeException
127
         * @expectedExceptionCode 1464997310
128
         */
129
        public function testGenerateRepositoryFailingMkdir()
130
        {
131
            putenv('MOCK_MKDIR=1');
132
            $this->testGenerateRepositoryEmptyFiles();
133
        }
134
135
        /**
136
         * Test the repository generation with failing touch()
137
         *
138
         * @expectedException \Apparat\Dev\Ports\RuntimeException
139
         * @expectedExceptionCode 1464997761
140
         */
141
        public function testGenerateRepositoryFailingTouch()
142
        {
143
            putenv('MOCK_TOUCH=1');
144
            $this->testGenerateRepositoryEmptyFiles();
145
        }
146
147
        /**
148
         * Test the generation of a test repository
149
         */
150
//        public function testGenerateRepository()
151
//        {
152
//            $objectConfig = [
153
//                Object::ARTICLE => [
154
//                    Repository::COUNT => 10,
155
//                    Repository::HIDDEN => .5,
156
//                    Repository::DRAFTS => .2,
157
//                ],
158
//            ];
159
//            $this->generateAndTestRepository($objectConfig, 10, false);
160
//        }
161
162
        /**
163
         * Generate and test a repository
164
         *
165
         * @param array $objectConfig Object configuration
166
         * @param int $expectedCount Expected number of objects
167
         * @param bool $emptyFiles Create empty files
168
         */
169
        protected function generateAndTestRepository(array $objectConfig, $expectedCount, $emptyFiles = false)
170
        {
171
            $tmpDirectory = sys_get_temp_dir().DIRECTORY_SEPARATOR.'apparat_test';
172
            Repository::generate(
173
                $this->registerTemporaryDirectory($tmpDirectory),
174
                $objectConfig,
175
                Repository::FLAG_CREATE_ROOT_DIRECTORY | ($emptyFiles ? Repository::FLAG_EMPTY_FILES : 0)
176
            );
177
            $datePath = str_repeat(DIRECTORY_SEPARATOR.'*', getenv('OBJECT_DATE_PRECISION'));
178
            $glob = $tmpDirectory.$datePath.DIRECTORY_SEPARATOR.'{.,}[!.,!..]*';
179
            $this->assertEquals($expectedCount, count(glob($glob, GLOB_ONLYDIR | GLOB_BRACE)));
180
        }
181
    }
182
}
183
184
namespace Apparat\Dev\Infrastructure\Factory {
185
186
    /**
187
     * Create a directory
188
     *
189
     * @param string $pathname Path name
190
     * @param int $mode File mode
191
     * @param bool $recursive Create parent directories
192
     * @return bool Success
193
     */
194
    function mkdir($pathname, $mode = 0777, $recursive = false)
195
    {
196
        return (getenv('MOCK_MKDIR') != 1) ? \mkdir($pathname, $mode, $recursive) : false;
197
    }
198
199
    /**
200
     * Sets access and modification time of file
201
     *
202
     * @param string $filename File name
203
     * @param null $time Modification time
204
     * @param null $atime Access time
205
     * @return bool Success
206
     */
207
    function touch($filename, $time = null, $atime = null)
208
    {
209
        return (getenv('MOCK_TOUCH') != 1) ? \touch($filename, $time, $atime) : false;
210
    }
211
}
212