Completed
Push — develop ( 841446...f3e111 )
by Schlaefer
04:07
created

TestCaseTrait   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 47
dl 0
loc 155
rs 10
c 2
b 0
f 0
wmc 13

10 Methods

Rating   Name   Duplication   Size   Complexity  
A _clearCaches() 0 6 1
A insertCategoryPermissions() 0 4 1
A setUpSaito() 0 7 1
A _storeSettings() 0 7 1
A setI18n() 0 4 1
A _restoreSettings() 0 4 1
A mockMediaFile() 0 20 4
A mockMailTransporter() 0 7 1
A getMockForTable() 0 10 1
A tearDownSaito() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace Saito\Test;
14
15
use Cake\Core\Configure;
16
use Cake\Event\EventManager;
17
use Cake\Filesystem\File;
18
use Cake\I18n\I18n;
19
use Cake\Mailer\TransportFactory;
20
use Cake\ORM\TableRegistry;
21
use Cake\Utility\Inflector;
22
use Saito\App\Registry;
23
use Saito\Cache\CacheSupport;
24
25
trait TestCaseTrait
26
{
27
    private $saitoSettings;
28
29
    protected $saitoPermissions;
30
31
    /**
32
     * set-up saito
33
     *
34
     * @return void
35
     */
36
    protected function setUpSaito()
37
    {
38
        Registry::initialize();
39
40
        $this->_storeSettings();
41
        $this->mockMailTransporter();
42
        $this->_clearCaches();
43
    }
44
45
    /**
46
     * tear down saito
47
     *
48
     * @return void
49
     */
50
    protected function tearDownSaito()
51
    {
52
        $this->_restoreSettings();
53
        $this->_clearCaches();
54
    }
55
56
    /**
57
     * clear caches
58
     *
59
     * @return void
60
     */
61
    protected function _clearCaches()
62
    {
63
        $CacheSupport = new CacheSupport();
64
        $CacheSupport->clear();
65
        EventManager::instance()->off($CacheSupport);
66
        unset($CacheSupport);
67
    }
68
69
    /**
70
     * store global settings
71
     *
72
     * @return void
73
     */
74
    protected function _storeSettings()
75
    {
76
        $this->saitoSettings = Configure::read('Saito.Settings');
77
        $this->saitoPermissions = clone(Configure::read('Saito.Permission.Resources'));
78
        $this->setI18n('en');
79
        Configure::write('Saito.Settings.ParserPlugin', \Plugin\BbcodeParser\src\Lib\Markup::class);
80
        Configure::write('Saito.Settings.uploader', clone($this->saitoSettings['uploader']));
81
    }
82
83
    /**
84
     * restore global settings
85
     *
86
     * @return void
87
     */
88
    protected function _restoreSettings()
89
    {
90
        Configure::write('Saito.Settings', $this->saitoSettings);
91
        Configure::write('Saito.Permission.Resources', $this->saitoPermissions);
92
    }
93
94
    /**
95
     * Set the current translation language
96
     *
97
     * @param string $lang language code
98
     * @return void
99
     */
100
    public function setI18n(string $lang): void
101
    {
102
        Configure::write('Saito.language', $lang);
103
        I18n::setLocale($lang);
104
    }
105
106
    /**
107
     * Mock table
108
     *
109
     * @param string $table table
110
     * @param array $methods methods to mock
111
     * @return mixed
112
     */
113
    public function getMockForTable($table, array $methods = [])
114
    {
115
        $tableName = Inflector::underscore($table);
116
        $Mock = $this->getMockForModel(
0 ignored issues
show
Bug introduced by
The method getMockForModel() does not exist on Saito\Test\TestCaseTrait. Did you maybe mean getMockForTable()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

116
        /** @scrutinizer ignore-call */ 
117
        $Mock = $this->getMockForModel(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
117
            $table,
118
            $methods,
119
            ['table' => strtolower($tableName)]
120
        );
121
122
        return $Mock;
123
    }
124
125
    /**
126
     * Insert categories into permissions
127
     *
128
     * @return void
129
     */
130
    protected function insertCategoryPermissions(): void
131
    {
132
        Registry::get('Permissions')
133
            ->buildCategories(TableRegistry::getTableLocator()->get('Categories'));
134
    }
135
136
    /**
137
     * Mock mailtransporter
138
     *
139
     * @return mixed
140
     */
141
    protected function mockMailTransporter()
142
    {
143
        $mock = $this->createMock('Cake\Mailer\Transport\DebugTransport');
0 ignored issues
show
Bug introduced by
It seems like createMock() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

143
        /** @scrutinizer ignore-call */ 
144
        $mock = $this->createMock('Cake\Mailer\Transport\DebugTransport');
Loading history...
144
        TransportFactory::drop('saito');
145
        TransportFactory::setConfig('saito', $mock);
146
147
        return $mock;
148
    }
149
150
    /**
151
     * Creates a mock image file in $file
152
     *
153
     * @param File $file File with extension.
154
     *
155
     * Mime type is taken from extension. Allowed extensions: png, jpeg, jpg
156
     *
157
     * @param int $size size of the mock image in kB
158
     * @return void
159
     */
160
    protected function mockMediaFile(File $file, int $size = 100): void
161
    {
162
        //// Create single pixel image
163
        $Image = imagecreatetruecolor(1, 1);
164
        imagesetpixel($Image, 0, 0, imagecolorallocate($Image, 0, 0, 0));
0 ignored issues
show
Bug introduced by
It seems like $Image can also be of type false; however, parameter $image of imagesetpixel() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

164
        imagesetpixel(/** @scrutinizer ignore-type */ $Image, 0, 0, imagecolorallocate($Image, 0, 0, 0));
Loading history...
Bug introduced by
It seems like $Image can also be of type false; however, parameter $image of imagecolorallocate() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

164
        imagesetpixel($Image, 0, 0, imagecolorallocate(/** @scrutinizer ignore-type */ $Image, 0, 0, 0));
Loading history...
165
166
        switch ($file->ext()) {
167
            case 'jpeg':
168
            case 'jpg':
169
                imagejpeg($Image, $file->path);
0 ignored issues
show
Bug introduced by
It seems like $Image can also be of type false; however, parameter $image of imagejpeg() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

169
                imagejpeg(/** @scrutinizer ignore-type */ $Image, $file->path);
Loading history...
170
                break;
171
            case 'png':
172
                imagepng($Image, $file->path);
0 ignored issues
show
Bug introduced by
It seems like $Image can also be of type false; however, parameter $image of imagepng() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

172
                imagepng(/** @scrutinizer ignore-type */ $Image, $file->path);
Loading history...
173
                break;
174
            default:
175
                throw new \InvalidArgumentException();
176
        }
177
178
        // pad to saze with garbage data
179
        $file->append(str_repeat('0', $size * 1024));
180
    }
181
}
182