1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHPUnit plugin for HiDev. |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/hidev-phpunit |
6
|
|
|
* @package hidev-phpunit |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hidev\phpunit\console; |
12
|
|
|
|
13
|
|
|
use hidev\base\File; |
14
|
|
|
use hidev\handlers\BaseHandler; |
15
|
|
|
use Yii; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* PHPunit. |
19
|
|
|
*/ |
20
|
|
|
class PhpunitController extends \hidev\base\Controller |
21
|
|
|
{ |
22
|
|
|
protected $_before = ['phpunit.xml.dist']; |
23
|
|
|
|
24
|
|
|
public $force; |
25
|
|
|
|
26
|
|
|
public function getComponent() |
27
|
|
|
{ |
28
|
|
|
return $this->take('phpunit'); |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Generates `tests/_bootstrap.php` and runs tests. |
33
|
|
|
*/ |
34
|
|
|
public function actionIndex() |
35
|
|
|
{ |
36
|
|
|
$this->getComponent()->touchBootstrap(); |
37
|
|
|
|
38
|
|
|
return $this->doRun(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function doRun() |
42
|
|
|
{ |
43
|
|
|
$args = $this->getComponent()->prepareArgs(); |
44
|
|
|
|
45
|
|
|
return $this->passthru('phpunit', $args); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Generates skeleton class for fake. |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public function actionGenfake($file) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$path = $this->buildFakePath($file); |
54
|
|
|
if (!$this->force && file_exists($path)) { |
55
|
|
|
Yii::warning("already exists: $path"); |
56
|
|
|
return 1; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $this->genFake($file, $path); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function genFake($file, $path) |
63
|
|
|
{ |
64
|
|
|
$text = $this->getView()->render('phpunit/fake.twig', [ |
65
|
|
|
'class' => $this->buildClass($file), |
66
|
|
|
'namespace' => $this->buildTestNamespace(), |
67
|
|
|
'name' => $file, |
68
|
|
|
]); |
69
|
|
|
BaseHandler::write($path, $text); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Generates skeleton class for test. |
74
|
|
|
*/ |
75
|
|
View Code Duplication |
public function actionGentest($file) |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$path = $this->buildTestPath($file); |
78
|
|
|
if (!$this->force && file_exists($path)) { |
79
|
|
|
Yii::warning("already exists: $path"); |
80
|
|
|
return 1; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this->genSkel($file); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected static function prepareFile($file) |
87
|
|
|
{ |
88
|
|
|
return substr($file, -4) === '.php' ? substr($file, 0, -4) : $file; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected function genSkel($file) |
92
|
|
|
{ |
93
|
|
|
$destPath = $this->buildTestPath($file); |
94
|
|
|
$dir = dirname($destPath); |
95
|
|
|
if (!file_exists($dir)) { |
96
|
|
|
mkdir($dir, 0777, true); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this->passthru('phpunit-skelgen', [ |
|
|
|
|
100
|
|
|
'generate-test', '--bootstrap', 'tests/_bootstrap.php', '--', |
101
|
|
|
$this->buildClass($file), $this->buildPath($file), $this->buildTestClass($file), $destPath, |
102
|
|
|
]); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
protected function buildNamespace($dir = '') |
106
|
|
|
{ |
107
|
|
|
return $this->take('package')->namespace . ($dir ? '\\' . strtr($dir, '/', '\\') : ''); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
protected function buildTestNamespace($dir = 'tests\\unit') |
111
|
|
|
{ |
112
|
|
|
return $this->buildNamespace($dir); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
protected function buildClass($file, $dir = '', $postfix = '') |
116
|
|
|
{ |
117
|
|
|
return $this->buildNamespace($dir) . '\\' . strtr(static::prepareFile($file), '/', '\\') . $postfix; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
protected function buildTestClass($file, $dir = 'tests\\unit', $postfix = 'Test') |
121
|
|
|
{ |
122
|
|
|
return $this->buildClass($file, $dir, $postfix); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
protected function buildPath($file, $dir = 'src', $prefix = '', $postfix = '') |
126
|
|
|
{ |
127
|
|
|
return $dir . DIRECTORY_SEPARATOR . $prefix . static::prepareFile($file) . $postfix . '.php'; |
128
|
|
|
|
129
|
|
|
//## XXX getting absolute path, think if needed |
130
|
|
|
//return strncmp($path, '/', 1) === 0 ? $path : Yii::getAlias("@root/$path"); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
protected function buildTestPath($file, $dir = 'tests/unit', $prefix = '', $postfix = 'Test') |
134
|
|
|
{ |
135
|
|
|
return $this->buildPath($file, $dir, $prefix, $postfix); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
protected function buildFakePath($file, $dir = 'tests/unit', $prefix = 'Fake', $postfix = '') |
139
|
|
|
{ |
140
|
|
|
return $this->buildPath($file, $dir, $prefix, $postfix); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: