Completed
Push — master ( e6ef7b...361fa3 )
by Joschi
07:11
created

ImageObjectTest::deleteRecursive()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 4
eloc 9
c 2
b 0
f 1
nc 4
nop 1
dl 0
loc 15
rs 9.2
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\Infrastructure\Utilities\File;
41
    use Apparat\Object\Ports\Types\Object as ObjectTypes;
42
43
    /**
44
     * Image object tests
45
     *
46
     * @package Apparat\Object
47
     * @subpackage Apparat\Object\Test
48
     */
49
    class ImageObjectTest extends AbstractObjectTest
50
    {
51
        /**
52
         * Tears down the fixture
53
         */
54
        public function tearDown()
55
        {
56
            putenv('MOCK_COPY');
57
            parent::tearDown();
58
        }
59
60
        /**
61
         * Test the creation and persisting of an image object
62
         */
63
        public function testCreateAndPublishImageObject()
64
        {
65
            // Create a temporary repository
66
            $tempRepoDirectory = sys_get_temp_dir().DIRECTORY_SEPARATOR.'temp-repo';
67
            $fixtureDirectory = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'non-repo'
68
                .DIRECTORY_SEPARATOR;
69
            $repository = $this->createRepository($tempRepoDirectory);
70
            $payloadFileName1 = '1.'.File::hash($fixtureDirectory.'MuehlenbergDerwitz.jpg').'.jpg';
71
            $payloadFileName2 = '1.'.File::hash($fixtureDirectory.'Normalsegelapparat1895.jpg').'.jpg';
72
73
            // Create and persist an image object
74
            $image = $repository->createObject(ObjectTypes::IMAGE, $fixtureDirectory.'MuehlenbergDerwitz.jpg')
75
                ->persist();
76
            $this->assertInstanceOf(Image::class, $image);
77
            $this->assertEquals($payloadFileName1, $image->getPayload());
78
            $this->assertFileExists(
79
                $tempRepoDirectory.dirname(str_replace('/', DIRECTORY_SEPARATOR, $image->getRepositoryLocator())).
80
                DIRECTORY_SEPARATOR.$payloadFileName1
81
            );
82
83
            // Publish and persist the image
84
            $image->publish()->persist();
85
86
            // Add content relevant properties, publish & persist
87
            $image->setDomain('license', 'gemeinfrei')->publish()->persist();
88
89
            // Alter the payload
90
            $image->setPayload($fixtureDirectory.'Normalsegelapparat1895.jpg')->persist();
91
            $this->assertEquals('gemeinfrei', $image->getDomain('license'));
92
            $this->assertEquals($payloadFileName2, $image->getPayload());
93
            $this->assertFileExists(
94
                $tempRepoDirectory.dirname(str_replace('/', DIRECTORY_SEPARATOR, $image->getRepositoryLocator())).
95
                DIRECTORY_SEPARATOR.$payloadFileName2
96
            );
97
98
            // Delete temporary repository
99
            $this->deleteRecursive($tempRepoDirectory);
100
        }
101
102
        /**
103
         * Test empty binary payload
104
         *
105
         * @expectedException \Apparat\Object\Ports\Exceptions\InvalidArgumentException
106
         * @expectedExceptionCode 1464296678
107
         */
108
        public function testEmptyBinaryPayload()
109
        {
110
            // Create a temporary repository
111
            $tempRepoDirectory = $this->registerTemporaryDirectory(sys_get_temp_dir().DIRECTORY_SEPARATOR.'temp-repo');
112
            $repository = $this->createRepository($tempRepoDirectory);
113
114
            // Create an image object
115
            $repository->createObject(ObjectTypes::IMAGE);
116
        }
117
118
        /**
119
         * Test empty binary payload
120
         *
121
         * @expectedException \Apparat\Object\Ports\Exceptions\RuntimeException
122
         * @expectedExceptionCode 1464299856
123
         */
124
        public function testFailedRepositoryImport()
125
        {
126
            putenv('MOCK_COPY=1');
127
128
            // Create a temporary repository
129
            $tempRepoDirectory = $this->registerTemporaryDirectory(sys_get_temp_dir().DIRECTORY_SEPARATOR.'temp-repo');
130
            $fixtureDirectory = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'non-repo'
131
                .DIRECTORY_SEPARATOR;
132
            $repository = $this->createRepository($tempRepoDirectory);
133
134
            // Create an image object
135
            $repository->createObject(ObjectTypes::IMAGE, $fixtureDirectory.'MuehlenbergDerwitz.jpg')->persist();
136
        }
137
    }
138
}
139
140
namespace Apparat\Object\Infrastructure\Repository {
141
142
    /**
143
     * Mocked version of the native copy() function
144
     *
145
     * @param string $source Path to the source file
146
     * @param string $dest The destination path
147
     * @return bool true on success or false on failure.
148
     */
149
    function copy($source, $dest)
150
    {
151
        return (getenv('MOCK_COPY') != 1) ? \copy($source, $dest) : false;
152
    }
153
}
154