Completed
Push — master ( 9c9dd6...7e9b13 )
by Joschi
03:42
created

testGenerateRepositoryFailingTouch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
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\Infrastructure\Mutator\ArticleObjectMutator;
40
    use Apparat\Dev\Ports\Repository;
41
    use Apparat\Kernel\Ports\Kernel;
42
    use Apparat\Object\Ports\Types\Object;
43
44
    /**
45
     * Repository test
46
     *
47
     * @package Apparat\Dev
48
     * @subpackage Apparat\Dev\Tests
49
     */
50
    class RepositoryTest extends AbstractRepositoryTest
51
    {
52
        /**
53
         * Tears down the fixture
54
         */
55
        public function tearDown()
56
        {
57
            putenv('MOCK_MKDIR');
58
            putenv('MOCK_TOUCH');
59
            parent::tearDown();
60
        }
61
62
        /**
63
         * Test the generation of a test repository
64
         *
65
         * @expectedException \Apparat\Dev\Ports\InvalidArgumentsException
66
         * @expectedExceptionCode 1464974472
67
         */
68
        public function testGenerateRepositoryInvalidRoot()
69
        {
70
            Repository::generate(null, []);
71
        }
72
73
        /**
74
         * Test the generation of a test repository with an empty object configuration
75
         *
76
         * @expectedException \Apparat\Dev\Ports\InvalidArgumentsException
77
         * @expectedExceptionCode 1464974779
78
         */
79
        public function testGenerateRepositoryInvalidObjectConfig()
80
        {
81
            Repository::generate($this->createTemporaryFileName(), [], Repository::FLAG_CREATE_ROOT_DIRECTORY);
82
        }
83
84
        /**
85
         * Test the generation of a test repository with zero object count
86
         *
87
         * @expectedException \Apparat\Dev\Ports\InvalidArgumentsException
88
         * @expectedExceptionCode 1464975382
89
         */
90
        public function testGenerateRepositoryEmptyObjectCount()
91
        {
92
            $objectConfig = [
93
                Object::ARTICLE => []
94
            ];
95
            Repository::generate(
96
                $this->createTemporaryFileName(),
97
                $objectConfig,
98
                Repository::FLAG_CREATE_ROOT_DIRECTORY
99
            );
100
        }
101
102
        /**
103
         * Test the generation of a test repository
104
         */
105
        public function testGenerateRepositoryEmptyFiles()
106
        {
107
            $objectConfig = [
108
                Object::ARTICLE => [
109
                    Repository::COUNT => 100,
110
                    Repository::HIDDEN => .2,
111
                    Repository::DRAFTS => .2,
112
                ],
113
                Object::EVENT => [
114
                    Repository::COUNT => 100,
115
                ],
116
                Object::CONTACT => [
117
                    Repository::COUNT => 100,
118
                    Repository::REVISIONS => -3,
119
                    Repository::DRAFTS => .2,
120
                ]
121
            ];
122
            $this->generateAndTestRepository($objectConfig, 200, true);
123
        }
124
125
        /**
126
         * Test the repository generation with failing mkdir()
127
         *
128
         * @expectedException \Apparat\Dev\Ports\RuntimeException
129
         * @expectedExceptionCode 1464997310
130
         */
131
        public function testGenerateRepositoryFailingMkdir()
132
        {
133
            putenv('MOCK_MKDIR=1');
134
            $this->testGenerateRepositoryEmptyFiles();
135
        }
136
137
        /**
138
         * Test the repository generation with failing touch()
139
         *
140
         * @expectedException \Apparat\Dev\Ports\RuntimeException
141
         * @expectedExceptionCode 1464997761
142
         */
143
        public function testGenerateRepositoryFailingTouch()
144
        {
145
            putenv('MOCK_TOUCH=1');
146
            $this->testGenerateRepositoryEmptyFiles();
147
        }
148
149
        /**
150
         * Test mutator with invalid method
151
         *
152
         * @expectedException \Apparat\Dev\Ports\RuntimeException
153
         * @expectedExceptionCode 1465078211
154
         */
155
        public function testMutatorInvalidMethod()
156
        {
157
            /** @var ArticleObjectMutator $articleMutator */
158
            $articleMutator = Kernel::create(ArticleObjectMutator::class);
159
            $this->assertInstanceOf(ArticleObjectMutator::class, $articleMutator);
160
            /** @noinspection PhpUndefinedMethodInspection */
161
            $articleMutator->invalidMethod(0, []);
0 ignored issues
show
Documentation Bug introduced by
The method invalidMethod does not exist on object<Apparat\Dev\Infra...r\ArticleObjectMutator>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
162
        }
163
164
        /**
165
         * Test mutator with invalid mutation subject
166
         *
167
         * @expectedException \Apparat\Dev\Ports\RuntimeException
168
         * @expectedExceptionCode 1465078374
169
         */
170
        public function testMutatorInvalidSubject()
171
        {
172
            /** @var ArticleObjectMutator $articleMutator */
173
            $articleMutator = Kernel::create(ArticleObjectMutator::class);
174
            $this->assertInstanceOf(ArticleObjectMutator::class, $articleMutator);
175
            /** @noinspection PhpUndefinedMethodInspection */
176
            $articleMutator->setRandomInvalid(0, []);
0 ignored issues
show
Documentation Bug introduced by
The method setRandomInvalid does not exist on object<Apparat\Dev\Infra...r\ArticleObjectMutator>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
177
        }
178
    }
179
}
180
181
namespace Apparat\Dev\Infrastructure\Factory {
182
183
    /**
184
     * Create a directory
185
     *
186
     * @param string $pathname Path name
187
     * @param int $mode File mode
188
     * @param bool $recursive Create parent directories
189
     * @return bool Success
190
     */
191
    function mkdir($pathname, $mode = 0777, $recursive = false)
192
    {
193
        return (getenv('MOCK_MKDIR') != 1) ? \mkdir($pathname, $mode, $recursive) : false;
194
    }
195
196
    /**
197
     * Sets access and modification time of file
198
     *
199
     * @param string $filename File name
200
     * @param null $time Modification time
201
     * @param null $atime Access time
202
     * @return bool Success
203
     */
204
    function touch($filename, $time = null, $atime = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
205
    {
206
        return (getenv('MOCK_TOUCH') != 1) ? \touch($filename, $time, $atime) : false;
207
    }
208
}
209