Skip to content

Commit f150c52

Browse files
author
Gianluca Arbezzano
committed
Merge pull request #51 from tomphp/issue/31
Add tests for issue #31
2 parents 8ee904a + b1ff2b2 commit f150c52

File tree

4 files changed

+295
-3
lines changed

4 files changed

+295
-3
lines changed

PHPCtags.class.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,10 @@ private function render()
256256
foreach ($this->mStructs as $struct) {
257257
$file = $struct['file'];
258258

259+
if (!in_array($struct['kind'], $this->mOptions['kinds'])) {
260+
continue;
261+
}
262+
259263
if (!isset($files[$file]))
260264
$files[$file] = file($file);
261265

bootstrap.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
'exclude:',
2525
'excmd::',
2626
'fields::',
27+
'kinds::',
2728
'format::',
2829
'help',
2930
'recurse::',
@@ -57,7 +58,9 @@
5758
--excmd=number|pattern|mix
5859
Uses the specified type of EX command to locate tags [mix].
5960
--fields=[+|-]flags
60-
Include selected extension fields (flags: "afmikKlnsStz") [fks].
61+
Include selected extension fields (flags: "afmikKlnsStz") [fks].
62+
--kinds=[+|-]flags
63+
Enable/disable tag kinds [cmfpvditn]
6164
--format=level
6265
Force output of specified tag file format [2].
6366
--help
@@ -141,12 +144,20 @@
141144
$options['format'] = 2;
142145
if (!isset($options['memory']))
143146
$options['memory'] = '128M';
147+
144148
if (!isset($options['fields'])) {
145149
$options['fields'] = array('n', 'k', 's', 'a','i');
146150
} else {
147151
$options['fields'] = str_split($options['fields']);
148152
}
149153

154+
if (!isset($options['kinds'])) {
155+
$options['kinds'] = array('c', 'm', 'f', 'p', 'd', 'v', 'i', 't', 'n');
156+
} else {
157+
$options['kinds'] = str_split($options['kinds']);
158+
}
159+
160+
150161
// handle -u or --sort options
151162
if (isset($options['sort'])) {
152163
// --sort or --sort=[Y,y,YES,Yes,yes]
@@ -218,6 +229,8 @@
218229
}
219230

220231
$mode = ($options['sort'] == 'yes' ? 1 : ($options['sort'] == 'foldcase' ? 2 : 0));
232+
233+
if (!isset($options['a'])) {
221234
$tagline = <<<EOF
222235
!_TAG_FILE_FORMAT\t2\t/extended format; --format=1 will not append ;" to lines/
223236
!_TAG_FILE_SORTED\t{$mode}\t/0=unsorted, 1=sorted, 2=foldcase/
@@ -226,6 +239,7 @@
226239
!_TAG_PROGRAM_URL\thttps://github.com/techlivezheng/phpctags\t/official site/
227240
!_TAG_PROGRAM_VERSION\t${version}\t//\n
228241
EOF;
242+
}
229243

230244
fwrite($tagfile, $tagline.$result);
231245
fclose($tagfile);

tests/Acceptance/AcceptanceTestCase.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,22 @@ protected function givenMode($file, $mode)
7474
/**
7575
* @return void
7676
*/
77-
protected function runPHPCtags()
77+
protected function runPHPCtags(array $params = array())
7878
{
7979
$entryPoint = realpath(__DIR__ . '/../../bootstrap.php');
8080

81-
exec("php \"$entryPoint\" --recurse=yes -f - {$this->testDir}", $output);
81+
$params = implode(' ', $params);
82+
83+
exec("php \"$entryPoint\" --recurse=yes $params -f - {$this->testDir}", $output);
8284

8385
$this->tagsFileContent = $output;
8486
}
8587

88+
protected function runPHPCtagsWithKinds($kindString)
89+
{
90+
$this->runPHPCtags(array("--kinds=$kindString"));
91+
}
92+
8693
/**
8794
* @param string $patterns
8895
*

tests/Acceptance/KindsTest.php

Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
<?php
2+
3+
namespace tests\PHPCTags\Acceptance;
4+
5+
final class KindsTest extends AcceptanceTestCase
6+
{
7+
protected function setUp()
8+
{
9+
parent::setUp();
10+
11+
$sourceCode =<<<'EOS'
12+
<?php
13+
namespace KindsExampleNamespace;
14+
class KindsExampleClass
15+
{
16+
public $publicProperty;
17+
protected $protectedProperty;
18+
private $privateProperty;
19+
20+
public function publicMethod()
21+
{
22+
}
23+
24+
protected function protectedMethod()
25+
{
26+
}
27+
28+
private function privateMethod()
29+
{
30+
}
31+
}
32+
33+
function kindsExampleFunction()
34+
{
35+
}
36+
37+
define('CONSTANT_1', 1);
38+
const CONSTANT_2 = 2;
39+
40+
$var = 'test value';
41+
42+
interface KindsExampleInterface
43+
{
44+
}
45+
EOS;
46+
47+
if (version_compare('5.4.0', PHP_VERSION, '<=')) {
48+
$sourceCode .= <<<EOS
49+
trait KindsExampleTrait
50+
{
51+
}
52+
EOS;
53+
}
54+
55+
$this->givenSourceFile('KindsExample.php', $sourceCode);
56+
}
57+
58+
/**
59+
* @test
60+
*/
61+
public function itSupportsClassKindParameter()
62+
{
63+
$this->runPHPCtagsWithKinds('c');
64+
65+
$this->assertTagsFileHeaderIsCorrect();
66+
$this->assertNumberOfTagsInTagsFileIs(1);
67+
68+
$this->assertTagsFileContainsTag(
69+
'KindsExample.php',
70+
'KindsExampleClass',
71+
self::KIND_CLASS,
72+
3,
73+
'namespace:KindsExampleNamespace'
74+
);
75+
}
76+
77+
/**
78+
* @test
79+
*/
80+
public function itSupportsPropertyKindParameter()
81+
{
82+
$this->runPHPCtagsWithKinds('p');
83+
84+
$this->assertTagsFileHeaderIsCorrect();
85+
$this->assertNumberOfTagsInTagsFileIs(3);
86+
87+
$this->assertTagsFileContainsTag(
88+
'KindsExample.php',
89+
'publicProperty',
90+
self::KIND_PROPERTY,
91+
5,
92+
'class:KindsExampleNamespace\KindsExampleClass',
93+
'public'
94+
);
95+
$this->assertTagsFileContainsTag(
96+
'KindsExample.php',
97+
'protectedProperty',
98+
self::KIND_PROPERTY,
99+
6,
100+
'class:KindsExampleNamespace\KindsExampleClass',
101+
'protected'
102+
);
103+
$this->assertTagsFileContainsTag(
104+
'KindsExample.php',
105+
'privateProperty',
106+
self::KIND_PROPERTY,
107+
7,
108+
'class:KindsExampleNamespace\KindsExampleClass',
109+
'private'
110+
);
111+
}
112+
113+
/**
114+
* @test
115+
*/
116+
public function itSupportsMethodKindParameter()
117+
{
118+
$this->runPHPCtagsWithKinds('m');
119+
120+
$this->assertTagsFileHeaderIsCorrect();
121+
$this->assertNumberOfTagsInTagsFileIs(3);
122+
123+
$this->assertTagsFileContainsTag(
124+
'KindsExample.php',
125+
'publicMethod',
126+
self::KIND_METHOD,
127+
9,
128+
'class:KindsExampleNamespace\KindsExampleClass',
129+
'public'
130+
);
131+
$this->assertTagsFileContainsTag(
132+
'KindsExample.php',
133+
'protectedMethod',
134+
self::KIND_METHOD,
135+
13,
136+
'class:KindsExampleNamespace\KindsExampleClass',
137+
'protected'
138+
);
139+
$this->assertTagsFileContainsTag(
140+
'KindsExample.php',
141+
'privateMethod',
142+
self::KIND_METHOD,
143+
17,
144+
'class:KindsExampleNamespace\KindsExampleClass',
145+
'private'
146+
);
147+
}
148+
149+
/**
150+
* @test
151+
*/
152+
public function itSupportsFunctionKindParameter()
153+
{
154+
$this->runPHPCtagsWithKinds('f');
155+
156+
$this->assertTagsFileHeaderIsCorrect();
157+
$this->assertNumberOfTagsInTagsFileIs(1);
158+
159+
$this->assertTagsFileContainsTag(
160+
'KindsExample.php',
161+
'kindsExampleFunction',
162+
self::KIND_FUNCTION,
163+
22,
164+
'namespace:KindsExampleNamespace'
165+
);
166+
}
167+
168+
/**
169+
* @test
170+
*/
171+
public function itSupportsConstantKindsParameter()
172+
{
173+
$this->runPHPCtagsWithKinds('d');
174+
175+
$this->assertTagsFileHeaderIsCorrect();
176+
$this->assertNumberOfTagsInTagsFileIs(2);
177+
$this->assertTagsFileContainsTag(
178+
'KindsExample.php',
179+
'CONSTANT_1',
180+
self::KIND_CONSTANT,
181+
26,
182+
'namespace:KindsExampleNamespace'
183+
);
184+
$this->assertTagsFileContainsTag(
185+
'KindsExample.php',
186+
'CONSTANT_2',
187+
self::KIND_CONSTANT,
188+
27,
189+
'namespace:KindsExampleNamespace'
190+
);
191+
}
192+
193+
/**
194+
* @test
195+
*/
196+
public function itSupportsVariableKindsParameter()
197+
{
198+
$this->runPHPCtagsWithKinds('v');
199+
200+
$this->assertTagsFileHeaderIsCorrect();
201+
$this->assertNumberOfTagsInTagsFileIs(1);
202+
$this->assertTagsFileContainsTag(
203+
'KindsExample.php',
204+
'var',
205+
self::KIND_VARIABLE,
206+
29,
207+
'namespace:KindsExampleNamespace'
208+
);
209+
}
210+
211+
/**
212+
* @test
213+
*/
214+
public function itSupportsNamespaceKindsParameter()
215+
{
216+
$this->runPHPCtagsWithKinds('n');
217+
218+
$this->assertTagsFileHeaderIsCorrect();
219+
$this->assertNumberOfTagsInTagsFileIs(1);
220+
$this->assertTagsFileContainsTag(
221+
'KindsExample.php',
222+
'KindsExampleNamespace',
223+
self::KIND_NAMESPACE,
224+
2
225+
);
226+
}
227+
228+
/**
229+
* @test
230+
*/
231+
public function itSupportsInterfaceKindsParameter()
232+
{
233+
$this->runPHPCtagsWithKinds('i');
234+
235+
$this->assertTagsFileHeaderIsCorrect();
236+
$this->assertNumberOfTagsInTagsFileIs(1);
237+
$this->assertTagsFileContainsTag(
238+
'KindsExample.php',
239+
'KindsExampleInterface',
240+
self::KIND_INTERFACE,
241+
31,
242+
'namespace:KindsExampleNamespace'
243+
);
244+
}
245+
246+
/**
247+
* @test
248+
*/
249+
public function itSupportsTraitKindsParameter()
250+
{
251+
if (version_compare('5.4.0', PHP_VERSION, '>')) {
252+
$this->markTestSkipped('Traits were not introduced until 5.4');
253+
}
254+
255+
$this->runPHPCtagsWithKinds('t');
256+
257+
$this->assertTagsFileHeaderIsCorrect();
258+
$this->assertNumberOfTagsInTagsFileIs(1);
259+
$this->assertTagsFileContainsTag(
260+
'KindsExample.php',
261+
'KindsExampleTrait',
262+
self::KIND_TRAIT,
263+
33,
264+
'namespace:KindsExampleNamespace'
265+
);
266+
}
267+
}

0 commit comments

Comments
 (0)