Completed
Push — feature/6.x ( db50a0...aa9894 )
by Schlaefer
03:28
created

TestCaseTrait::_clearFolder()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 10
rs 10
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * Saito - The Threaded Web Forum
6
 *
7
 * @copyright Copyright (c) the Saito Project Developers
8
 * @link https://github.com/Schlaefer/Saito
9
 * @license http://opensource.org/licenses/MIT
10
 */
11
12
namespace Saito\Test;
13
14
use Cake\Core\Configure;
15
use Cake\Event\EventManager;
16
use Cake\Filesystem\File;
17
use Cake\I18n\I18n;
18
use Cake\Mailer\Transport\DebugTransport;
19
use Cake\Mailer\TransportFactory;
20
use Cake\ORM\TableRegistry;
21
use Cake\Utility\Inflector;
22
use League\Flysystem\Adapter\Local;
23
use League\Flysystem\Filesystem;
24
use Saito\App\Registry;
25
use Saito\Cache\CacheSupport;
26
27
trait TestCaseTrait
28
{
29
    private $saitoSettings;
30
31
    protected $saitoPermissions;
32
33
    /**
34
     * set-up saito
35
     *
36
     * @return void
37
     */
38
    protected function setUpSaito()
39
    {
40
        Registry::initialize();
41
42
        $this->_storeSettings();
43
        $this->mockMailTransporter();
44
        $this->_clearCaches();
45
    }
46
47
    /**
48
     * tear down saito
49
     *
50
     * @return void
51
     */
52
    protected function tearDownSaito()
53
    {
54
        $this->_restoreSettings();
55
        $this->_clearCaches();
56
        $this->_clearTmpFolder();
57
    }
58
59
    /**
60
     * Clear test tmp folder
61
     * @return void
62
     */
63
    protected function _clearTmpFolder(): void
64
    {
65
        $this->_clearFolder();
66
    }
67
68
    /**
69
     * Clears a folder within the test folder
70
     * @param null|string $path  Path to folder (null clears root)
71
     * @return void
72
     */
73
    private function _clearFolder(?string $path = null): void
74
    {
75
        $fs = new Filesystem(new Local(TEST_TMP_DIR));
76
        $contents = $fs->listContents($path);
77
        foreach ($contents as $content) {
78
            $basename = $content['basename'];
79
            if (strpos($basename, '.') === 0) {
80
                continue;
81
            }
82
            $fs->delete($basename);
83
        }
84
    }
85
86
    /**
87
     * clear caches
88
     *
89
     * @return void
90
     */
91
    protected function _clearCaches()
92
    {
93
        $CacheSupport = new CacheSupport();
94
        $CacheSupport->clear();
95
        EventManager::instance()->off($CacheSupport);
96
        unset($CacheSupport);
97
    }
98
99
    /**
100
     * store global settings
101
     *
102
     * @return void
103
     */
104
    protected function _storeSettings()
105
    {
106
        $this->saitoSettings = Configure::read('Saito.Settings');
107
        $this->saitoPermissions = clone Configure::read('Saito.Permission.Resources');
108
        $this->setI18n('en');
109
        Configure::write('Saito.Settings.ParserPlugin', \Plugin\BbcodeParser\src\Lib\Markup::class);
110
        Configure::write('Saito.Settings.uploader', clone $this->saitoSettings['uploader']);
111
    }
112
113
    /**
114
     * restore global settings
115
     *
116
     * @return void
117
     */
118
    protected function _restoreSettings()
119
    {
120
        Configure::write('Saito.Settings', $this->saitoSettings);
121
        Configure::write('Saito.Permission.Resources', $this->saitoPermissions);
122
    }
123
124
    /**
125
     * Set the current translation language
126
     *
127
     * @param string $lang language code
128
     * @return void
129
     */
130
    public function setI18n(string $lang): void
131
    {
132
        Configure::write('Saito.language', $lang);
133
        I18n::setLocale($lang);
134
    }
135
136
    /**
137
     * Mock table
138
     *
139
     * @param string $table table
140
     * @param array $methods methods to mock
141
     * @return mixed
142
     */
143
    public function getMockForTable($table, array $methods = [])
144
    {
145
        $tableName = Inflector::underscore($table);
146
        $Mock = $this->getMockForModel(
147
            $table,
148
            $methods,
149
            ['table' => strtolower($tableName)]
150
        );
151
152
        return $Mock;
153
    }
154
155
    /**
156
     * Insert categories into permissions
157
     *
158
     * @return void
159
     */
160
    protected function insertCategoryPermissions(): void
161
    {
162
        Registry::get('Permissions')
163
            ->buildCategories(TableRegistry::getTableLocator()->get('Categories'));
164
    }
165
166
    /**
167
     * Mock mailtransporter
168
     *
169
     * @return mixed
170
     */
171
    protected function mockMailTransporter()
172
    {
173
        $mock = $this->createMock(DebugTransport::class);
174
        TransportFactory::drop('saito');
175
        TransportFactory::setConfig('saito', $mock);
176
177
        return $mock;
178
    }
179
180
    /**
181
     * Creates a mock image file in $file
182
     *
183
     * @param \Cake\Filesystem\File $file File with extension.
184
     * Mime type is taken from extension. Allowed extensions: png, jpeg, jpg
185
     *
186
     * @param int $size size of the mock image in kB
187
     * @return void
188
     */
189
    protected function mockMediaFile(File $file, int $size = 100): void
190
    {
191
        //// Create single pixel image
192
        $Image = imagecreatetruecolor(1, 1);
193
        imagesetpixel($Image, 0, 0, imagecolorallocate($Image, 0, 0, 0));
194
195
        switch ($file->ext()) {
196
            case 'jpeg':
197
            case 'jpg':
198
                imagejpeg($Image, $file->path);
199
                break;
200
            case 'png':
201
                imagepng($Image, $file->path);
202
                break;
203
            default:
204
                throw new \InvalidArgumentException();
205
        }
206
207
        // pad to saze with garbage data
208
        $file->append(str_repeat('0', $size * 1024));
209
    }
210
}
211