1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CakeCMS Core |
4
|
|
|
* |
5
|
|
|
* This file is part of the of the simple cms based on CakePHP 3. |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
* |
9
|
|
|
* @package Core |
10
|
|
|
* @license MIT |
11
|
|
|
* @copyright MIT License http://www.opensource.org/licenses/mit-license.php |
12
|
|
|
* @link https://github.com/CakeCMS/Core". |
13
|
|
|
* @author Sergey Kalistratov <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Core\TestSuite; |
17
|
|
|
|
18
|
|
|
use Core\Plugin; |
19
|
|
|
use Cake\Cache\Cache; |
20
|
|
|
use Cake\Utility\Hash; |
21
|
|
|
use Cake\ORM\TableRegistry; |
22
|
|
|
use Cake\TestSuite\IntegrationTestCase as CakeIntegrationTestCase; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class IntegrationTestCase |
26
|
|
|
* |
27
|
|
|
* @package Core\TestSuite |
28
|
|
|
*/ |
29
|
|
|
class IntegrationTestCase extends CakeIntegrationTestCase |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Default plugin. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $_plugin = 'Core'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Core plugin. |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $_corePlugin = 'Core'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Default table name. |
48
|
|
|
* |
49
|
|
|
* @var null|string |
50
|
|
|
*/ |
51
|
|
|
protected $_defaultTable; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Default url. |
55
|
|
|
* |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
protected $_url = [ |
59
|
|
|
'prefix' => null, |
60
|
|
|
'plugin' => 'Core', |
61
|
|
|
'action' => '', |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Setup the test case, backup the static object values so they can be restored. |
66
|
|
|
* Specifically backs up the contents of Configure and paths in App if they have |
67
|
|
|
* not already been backed up. |
68
|
|
|
* |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
View Code Duplication |
public function setUp() |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
parent::setUp(); |
74
|
|
|
|
75
|
|
|
if (!Plugin::loaded($this->_corePlugin)) { |
76
|
|
|
$loadParams = [ |
77
|
|
|
'bootstrap' => true, |
78
|
|
|
'routes' => true, |
79
|
|
|
'path' => ROOT . DS, |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
Plugin::load($this->_corePlugin, $loadParams); |
83
|
|
|
Plugin::routes($this->_corePlugin); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($this->_plugin !== $this->_corePlugin) { |
87
|
|
|
$options = [ |
88
|
|
|
'bootstrap' => true, |
89
|
|
|
'routes' => true, |
90
|
|
|
'autoload' => true, |
91
|
|
|
]; |
92
|
|
|
|
93
|
|
|
Plugin::load($this->_plugin, $options); |
94
|
|
|
Plugin::routes($this->_plugin); |
95
|
|
|
|
96
|
|
|
$this->_url['plugin'] = $this->_plugin; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Clears the state used for requests. |
102
|
|
|
* |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
|
View Code Duplication |
public function tearDown() |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
parent::tearDown(); |
108
|
|
|
|
109
|
|
|
Plugin::unload($this->_corePlugin); |
110
|
|
|
if ($this->_plugin !== $this->_corePlugin) { |
111
|
|
|
Plugin::unload($this->_plugin); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
Cache::drop('test_cached'); |
115
|
|
|
unset($this->_url); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Prepare url. |
120
|
|
|
* |
121
|
|
|
* @param array $url |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
|
|
protected function _getUrl(array $url = []) |
125
|
|
|
{ |
126
|
|
|
return Hash::merge($this->_url, $url); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get table object. |
131
|
|
|
* |
132
|
|
|
* @param null|string $name |
133
|
|
|
* @return \Cake\ORM\Table |
134
|
|
|
*/ |
135
|
|
|
protected function _getTable($name = null) |
136
|
|
|
{ |
137
|
|
|
$tableName = ($name === null) ? $this->_defaultTable : $name; |
138
|
|
|
return TableRegistry::get($this->_corePlugin . '.' . $tableName); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.