Completed
Push — master ( bd6d37...98df61 )
by Joschi
03:30
created

ImageObjectTest.php ➔ copy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
/**
4
 * apparat-object
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Object
8
 * @subpackage  Apparat\Object\Infrastructure
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\Object\Tests {
38
39
    use Apparat\Object\Application\Model\Object\Image;
40
    use Apparat\Object\Domain\Repository\Repository;
41
    use Apparat\Object\Infrastructure\Repository\FileAdapterStrategy;
42
    use Apparat\Object\Infrastructure\Utilities\File;
43
    use Apparat\Object\Ports\Object;
44
    use Apparat\Object\Ports\Repository as RepositoryFactory;
45
46
    /**
47
     * Object tests
48
     *
49
     * @package Apparat\Object
50
     * @subpackage Apparat\Object\Test
51
     */
52
    class ImageObjectTest extends AbstractRepositoryEnabledTest
53
    {
54
        /**
55
         * Tears down the fixture
56
         */
57
        public function tearDown()
58
        {
59
            putenv('MOCK_COPY');
60
            parent::tearDown();
61
        }
62
63
        /**
64
         * Test the creation and persisting of an image object
65
         */
66
        public function testCreateAndPublishImageObject()
67
        {
68
            // Create a temporary repository
69
            $tempRepoDirectory = sys_get_temp_dir().DIRECTORY_SEPARATOR.'temp-repo';
70
            $fixtureDirectory = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'non-repo'.DIRECTORY_SEPARATOR;
71
            $repository = $this->createRepository($tempRepoDirectory);
72
            $payloadFileName1 = '1.'.File::hash($fixtureDirectory.'MuehlenbergDerwitz.jpg').'.jpg';
73
            $payloadFileName2 = '1.'.File::hash($fixtureDirectory.'Normalsegelapparat1895.jpg').'.jpg';
74
75
            // Create and persist an image object
76
            $image = $repository->createObject(Object::IMAGE, $fixtureDirectory.'MuehlenbergDerwitz.jpg')->persist();
77
            $this->assertInstanceOf(Image::class, $image);
78
            $this->assertEquals($payloadFileName1, $image->getPayload());
79
            $this->assertFileExists($tempRepoDirectory.
80
                dirname(str_replace('/', DIRECTORY_SEPARATOR, $image->getRepositoryPath())).
81
                DIRECTORY_SEPARATOR.$payloadFileName1
82
            );
83
84
            // Publish and persist the image
85
            $image->publish()->persist();
86
87
            // Add content relevant properties, publish & persist
88
            $image->setDomainProperty('license', 'gemeinfrei')->publish()->persist();
89
90
            // Alter the payload
91
            $image->setPayload($fixtureDirectory.'Normalsegelapparat1895.jpg')->persist();
92
            $this->assertEquals('gemeinfrei', $image->getDomainProperty('license'));
93
            $this->assertEquals($payloadFileName2, $image->getPayload());
94
            $this->assertFileExists($tempRepoDirectory.
95
                dirname(str_replace('/', DIRECTORY_SEPARATOR, $image->getRepositoryPath())).
96
                DIRECTORY_SEPARATOR.$payloadFileName2
97
            );
98
99
            // Delete temporary repository
100
            $this->deleteRecursive($tempRepoDirectory);
101
        }
102
103
        /**
104
         * Create a temporary repository
105
         *
106
         * @param string $tempRepoDirectory Repository directory
107
         * @return Repository File repository
108
         */
109
        protected function createRepository($tempRepoDirectory)
110
        {
111
            $fileRepository = RepositoryFactory::create(
112
                getenv('REPOSITORY_URL'),
113
                [
114
                    'type' => FileAdapterStrategy::TYPE,
115
                    'root' => $tempRepoDirectory,
116
                ]
117
            );
118
            $this->assertInstanceOf(Repository::class, $fileRepository);
119
            $this->assertEquals($fileRepository->getAdapterStrategy()->getRepositorySize(), 0);
120
121
            return $fileRepository;
122
        }
123
124
        /**
125
         * Recursively register a directory and all nested files and directories for deletion on teardown
126
         *
127
         * @param string $directory Directory
128
         */
129
        protected function deleteRecursive($directory)
130
        {
131
            $this->tmpFiles[] = $directory;
132
            foreach (scandir($directory) as $item) {
133
                if (!preg_match('%^\.+$%', $item)) {
134
                    $path = $directory.DIRECTORY_SEPARATOR.$item;
135
                    if (is_dir($path)) {
136
                        $this->deleteRecursive($path);
137
                        continue;
138
                    }
139
140
                    $this->tmpFiles[] = $path;
141
                }
142
            }
143
        }
144
145
        /**
146
         * Test empty binary payload
147
         *
148
         * @expectedException \Apparat\Object\Ports\InvalidArgumentException
149
         * @expectedExceptionCode 1464296678
150
         */
151
        public function testEmptyBinaryPayload()
152
        {
153
            // Create a temporary repository
154
            $tempRepoDirectory = $this->registerTemporaryDirectory(sys_get_temp_dir().DIRECTORY_SEPARATOR.'temp-repo');
155
            $repository = $this->createRepository($tempRepoDirectory);
156
157
            // Create an image object
158
            $repository->createObject(Object::IMAGE);
159
        }
160
161
        /**
162
         * Test empty binary payload
163
         *
164
         * @expectedException \Apparat\Object\Ports\RuntimeException
165
         * @expectedExceptionCode 1464299856
166
         */
167
        public function testFailedRepositoryImport()
168
        {
169
            putenv('MOCK_COPY=1');
170
171
            // Create a temporary repository
172
            $tempRepoDirectory = $this->registerTemporaryDirectory(sys_get_temp_dir().DIRECTORY_SEPARATOR.'temp-repo');
173
            $fixtureDirectory = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'non-repo'.DIRECTORY_SEPARATOR;
174
            $repository = $this->createRepository($tempRepoDirectory);
175
176
            // Create an image object
177
            $repository->createObject(Object::IMAGE, $fixtureDirectory.'MuehlenbergDerwitz.jpg')->persist();
178
        }
179
    }
180
}
181
182
namespace Apparat\Object\Infrastructure\Repository {
183
184
    /**
185
     * Mocked version of the native copy() function
186
     *
187
     * @param string $source Path to the source file
188
     * @param string $dest The destination path
189
     * @return bool true on success or false on failure.
190
     */
191
    function copy($source, $dest)
192
    {
193
        return (getenv('MOCK_COPY') != 1) ? \copy($source, $dest) : false;
194
    }
195
}