Completed
Push — master ( 82d0ec...c6502b )
by Joschi
03:15
created

ImageObjectTest::deleteRecursive()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 15
cc 4
eloc 9
nc 4
nop 1
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\Domain\Model\Object\Type;
40
use Apparat\Object\Domain\Repository\Repository;
41
use Apparat\Object\Infrastructure\Repository\FileAdapterStrategy;
42
use Apparat\Object\Ports\Repository as RepositoryFactory;
43
44
/**
45
 * Object tests
46
 *
47
 * @package Apparat\Object
48
 * @subpackage Apparat\Object\Test
49
 */
50
class ImageObjectTest extends AbstractRepositoryEnabledTest
51
{
52
    /**
53
     * Tears down the fixture
54
     */
55
    public function tearDown()
56
    {
57
//        putenv('MOCK_RENAME');
58
        parent::tearDown();
59
    }
60
61
    /**
62
     * Test the creation and persisting of an image object
63
     */
64
    public function testCreateAndPublishImageObject()
65
    {
66
        // Create a temporary repository
67
        $tempRepoDirectory = sys_get_temp_dir().DIRECTORY_SEPARATOR.'temp-repo';
68
        $repository = $this->createRepository($tempRepoDirectory);
69
        $source = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'non-repo'.DIRECTORY_SEPARATOR.
70
            'MuehlenbergDerwitz.jpg';
71
72
        $repository->createObject(Type::IMAGE, $source);
73
74
//echo $tempRepoDirectory;
75
//        $article = $this->createRepositoryAndArticleObject($tempRepoDirectory, $payload);
76
//        $this->assertInstanceOf(Article::class, $article);
77
//        $this->assertEquals($payload, $article->getPayload());
78
//        $this->assertFileExists($tempRepoDirectory.
79
//            str_replace('/', DIRECTORY_SEPARATOR, $article->getRepositoryPath()
80
//                ->withExtension(getenv('OBJECT_RESOURCE_EXTENSION'))));
81
82
        // Alter and persist the object
83
//        $article->setPayload('Revision 1 draft (updated)');
84
//        $article->persist();
85
//
86
//        // Publish and persist the first object revision
87
//        $article->setPayload('Revision 1');
88
//        $article->publish();
89
//        $article->persist();
90
91
        // Delete temporary repository
92
//        $this->deleteRecursive($tempRepoDirectory);
93
//        $article->persist();
94
    }
95
96
    /**
97
     * Create a temporary repository
98
     *
99
     * @param string $tempRepoDirectory Repository directory
100
     * @return Repository File repository
101
     */
102
    protected function createRepository($tempRepoDirectory)
103
    {
104
        $fileRepository = RepositoryFactory::create(
105
            getenv('REPOSITORY_URL'),
106
            [
107
                'type' => FileAdapterStrategy::TYPE,
108
                'root' => $tempRepoDirectory,
109
            ]
110
        );
111
        $this->assertInstanceOf(Repository::class, $fileRepository);
112
        $this->assertEquals($fileRepository->getAdapterStrategy()->getRepositorySize(), 0);
113
114
        return $fileRepository;
115
    }
116
117
    /**
118
     * Recursively register a directory and all nested files and directories for deletion on teardown
119
     *
120
     * @param string $directory Directory
121
     */
122
    protected function deleteRecursive($directory)
123
    {
124
        $this->tmpFiles[] = $directory;
125
        foreach (scandir($directory) as $item) {
126
            if (!preg_match('%^\.+$%', $item)) {
127
                $path = $directory.DIRECTORY_SEPARATOR.$item;
128
                if (is_dir($path)) {
129
                    $this->deleteRecursive($path);
130
                    continue;
131
                }
132
133
                $this->tmpFiles[] = $path;
134
            }
135
        }
136
    }
137
}
138