1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Webino (http://webino.sk/) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/webino/WebinoDev/ for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2012-2019 Webino, s. r. o. (http://webino.sk/) |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
use org\bovigo\vfs\vfsStream; |
11
|
|
|
use org\bovigo\vfs\vfsStreamWrapper; |
12
|
|
|
use WebinoBase\Filesystem\Path\BaseModulePath; |
13
|
|
|
use WebinoDev\Test\TestCase; |
14
|
|
|
use Zend\Mvc\Application; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Create a PHPUnit test case |
18
|
|
|
* |
19
|
|
|
* For mocking and legacy PHPUnit tests. |
20
|
|
|
* |
21
|
|
|
* @return TestCase|\PHPUnit_Framework_TestCase|object |
22
|
|
|
* @throws ReflectionException |
23
|
|
|
*/ |
24
|
|
|
function createTestCase() |
25
|
|
|
{ |
26
|
|
|
return setUpTestCase(new TestCase); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param TestCase|\PHPUnit_Framework_TestCase|object $testCase |
31
|
|
|
* @return TestCase|\PHPUnit_Framework_TestCase|object |
32
|
|
|
* @throws ReflectionException |
33
|
|
|
*/ |
34
|
|
View Code Duplication |
function setUpTestCase($testCase) |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$reflection = new ReflectionMethod(get_class($testCase), 'setUp'); |
37
|
|
|
$reflection->setAccessible(true); |
38
|
|
|
$reflection->invoke($testCase); |
39
|
|
|
return $testCase; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param TestCase|\PHPUnit_Framework|object_$testCase |
44
|
|
|
* @return TestCase|\PHPUnit_Framework_TestCase|object |
45
|
|
|
* @throws ReflectionException |
46
|
|
|
*/ |
47
|
|
View Code Duplication |
function tearDownTestCase($testCase) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$reflection = new ReflectionMethod(get_class($testCase), 'tearDown'); |
50
|
|
|
$reflection->setAccessible(true); |
51
|
|
|
$reflection->invoke($testCase); |
52
|
|
|
return $testCase; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Application factory |
57
|
|
|
*/ |
58
|
|
|
function createApp() { |
59
|
|
|
|
60
|
|
|
// Webino support |
61
|
|
|
if (class_exists(BaseModulePath::class)) { |
62
|
|
|
return require (new BaseModulePath) . '/inc/app.php'; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// Zend initialization |
66
|
|
|
return Application::init(require 'config/application.config.php'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Create a virtual filesystem |
71
|
|
|
* |
72
|
|
|
* @param array $structure |
73
|
|
|
* @throws \org\bovigo\vfs\vfsStreamException |
74
|
|
|
*/ |
75
|
|
|
function createVfs(array $structure) |
76
|
|
|
{ |
77
|
|
|
vfsStreamWrapper::register(); |
78
|
|
|
vfsStreamWrapper::setRoot(vfsStream::setup('root', null, $structure)); |
79
|
|
|
set_include_path(vfsStream::url('root')); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Create a temporary directory |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
function createTmpDir() |
88
|
|
|
{ |
89
|
|
|
$dir = __DIR__ . '/tmp/' . getmypid(); |
90
|
|
|
// @ - directory may already exist |
91
|
|
|
@mkdir(dirname($dir)); |
92
|
|
|
Tester\Helpers::purge($dir); |
93
|
|
|
return $dir; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Return output buffer contents |
98
|
|
|
* |
99
|
|
|
* @param callable $callback |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
function catchOutput(callable $callback) |
103
|
|
|
{ |
104
|
|
|
ob_start(); |
105
|
|
|
call_user_func($callback); |
106
|
|
|
return trim(ob_get_clean()); |
107
|
|
|
} |
108
|
|
|
|
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.