Skip to content

Commit 22e9ed1

Browse files
authored
Merge pull request #32 from humanmade/phpcs-tests
Add tests for our phpcs sniffs
2 parents ccc228f + b9a2905 commit 22e9ed1

37 files changed

+730
-27
lines changed

.travis.yml

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,10 @@
11
language: php
22
php:
3-
- '5.6'
4-
- '7.0'
53
- '7.1'
6-
- hhvm
74

85
install:
96
- composer install
107
- npm install
118

12-
before_script:
13-
- if [[ ! -d /tmp/phpcs ]]; then git clone -b 2.8.1 https://github.com/squizlabs/PHP_CodeSniffer.git /tmp/phpcs; fi
14-
- cd /tmp/phpcs && composer install
15-
- /tmp/phpcs/scripts/phpcs --config-set installed_paths $TRAVIS_BUILD_DIR
16-
179
script:
18-
- /tmp/phpcs/vendor/bin/phpunit --filter HM
19-
20-
cache:
21-
directories:
22-
- $HOME/.composer/cache/files
23-
- $HOME/.npm
24-
- /tmp/phpcs
10+
- phpunit
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace HM\Coding\Standards\Tests;
4+
5+
class Bar {
6+
public function test() {
7+
echo 'bar';
8+
}
9+
}
10+
11+
class Foo {
12+
public function test() {
13+
echo 'foo';
14+
}
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace HM\Coding\Standards\Tests;
4+
5+
function foo() {
6+
echo 'foo';
7+
}
8+
9+
class Bar {
10+
public function test() {
11+
echo 'bar';
12+
}
13+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace HM\Tests\Classes;
4+
5+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
6+
7+
class OnlyClassInFileUnitTest extends AbstractSniffUnitTest {
8+
9+
/**
10+
* Returns the lines where errors should occur.
11+
*
12+
* @return array <int line number> => <int number of errors>
13+
*/
14+
public function getErrorList() {
15+
return [];
16+
}
17+
18+
/**
19+
* Returns the lines where warnings should occur.
20+
*
21+
* @return array <int line number> => <int number of warnings>
22+
*/
23+
public function getWarningList() {
24+
$file = func_get_arg( 0 );
25+
list( $_, $type, $variant ) = explode( '.', $file, 3 );
26+
if ( $type !== 'fail' ) {
27+
return [];
28+
}
29+
30+
return [
31+
1 => 1,
32+
];
33+
}
34+
35+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace HM\Coding\Standards\Tests;
4+
5+
const FOO = 'foo';
6+
7+
class Bar {
8+
public function test() {
9+
echo 'bar';
10+
}
11+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace HM\Tests\Files;
4+
5+
use DirectoryIterator;
6+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
7+
8+
class ClassFileNameUnitTest extends AbstractSniffUnitTest {
9+
/**
10+
* Get files to test against.
11+
*
12+
* Overridden from base to use the directory instead.
13+
*/
14+
protected function getTestFiles( $test_base_dir ) {
15+
$test_base_dir = rtrim( $test_base_dir, '.' );
16+
$test_files = [];
17+
18+
$di = new DirectoryIterator( $test_base_dir );
19+
20+
foreach ( $di as $file ) {
21+
if ( $file->isDot() ) {
22+
continue;
23+
}
24+
25+
$test_files[] = $file->getPathname();
26+
}
27+
28+
// Put them in order.
29+
sort( $test_files );
30+
31+
return $test_files;
32+
}
33+
34+
/**
35+
* Returns the lines where errors should occur.
36+
*
37+
* @return array <int line number> => <int number of errors>
38+
*/
39+
public function getErrorList() {
40+
$file = func_get_arg( 0 );
41+
$pass = [
42+
'class-test.php',
43+
'class-two-parts.php',
44+
];
45+
if ( in_array( $file, $pass, true ) ) {
46+
return [];
47+
}
48+
return [
49+
5 => 1,
50+
];
51+
}
52+
53+
/**
54+
* Returns the lines where warnings should occur.
55+
*
56+
* @return array <int line number> => <int number of warnings>
57+
*/
58+
public function getWarningList() {
59+
return [];
60+
}
61+
62+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace HM\Coding\Standards\Tests;
4+
5+
class Foo {}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace HM\Coding\Standards\Tests;
4+
5+
class Test {}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace HM\Coding\Standards\Tests;
4+
5+
class Test {}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace HM\Coding\Standards\Tests;
4+
5+
class Two_Parts {}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace HM\Coding\Standards\Tests;
4+
5+
class Two_Parts {}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace HM\Coding\Standards\Tests;
4+
5+
class Two_Parts {}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace HM\Coding\Standards\Tests;
4+
5+
class Test {}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace HM\Tests\Files;
4+
5+
use DirectoryIterator;
6+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
7+
8+
class FunctionFileNameUnitTest extends AbstractSniffUnitTest {
9+
/**
10+
* Get files to test against.
11+
*
12+
* Overridden from base to use the directory instead.
13+
*/
14+
protected function getTestFiles( $test_base_dir ) {
15+
$test_base_dir = rtrim( $test_base_dir, '.' );
16+
$test_files = [];
17+
18+
$di = new DirectoryIterator( $test_base_dir );
19+
20+
foreach ( $di as $file ) {
21+
if ( $file->isDot() ) {
22+
continue;
23+
}
24+
25+
$test_files[] = $file->getPathname();
26+
}
27+
28+
// Put them in order.
29+
sort( $test_files );
30+
31+
return $test_files;
32+
}
33+
34+
/**
35+
* Returns the lines where errors should occur.
36+
*
37+
* @return array <int line number> => <int number of errors>
38+
*/
39+
public function getErrorList() {
40+
$file = func_get_arg( 0 );
41+
$pass = [
42+
'namespace.php',
43+
];
44+
if ( in_array( $file, $pass, true ) ) {
45+
return [];
46+
}
47+
return [
48+
5 => 1,
49+
];
50+
}
51+
52+
/**
53+
* Returns the lines where warnings should occur.
54+
*
55+
* @return array <int line number> => <int number of warnings>
56+
*/
57+
public function getWarningList() {
58+
return [];
59+
}
60+
61+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace HM\Coding\Standards\Tests;
4+
5+
function foo() {}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace HM\Coding\Standards\Tests;
4+
5+
function foo() {}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace HM\Tests\Files;
4+
5+
use RecursiveDirectoryIterator;
6+
use RecursiveIteratorIterator;
7+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
8+
9+
class NamespaceDirectoryNameUnitTest extends AbstractSniffUnitTest {
10+
/**
11+
* Get files to test against.
12+
*
13+
* Overridden from base to use the directory instead.
14+
*/
15+
protected function getTestFiles( $test_base_dir ) {
16+
$test_base_dir = rtrim( $test_base_dir, '.' );
17+
$test_files = [];
18+
19+
$di = new RecursiveIteratorIterator(
20+
new RecursiveDirectoryIterator( $test_base_dir )
21+
);
22+
23+
foreach ( $di as $file ) {
24+
if ( ! $file->isFile() ) {
25+
continue;
26+
}
27+
28+
$test_files[] = $file->getPathname();
29+
}
30+
31+
// Put them in order.
32+
sort( $test_files );
33+
34+
return $test_files;
35+
}
36+
37+
/**
38+
* Returns the lines where errors should occur.
39+
*
40+
* @return array <int line number> => <int number of errors>
41+
*/
42+
public function getErrorList() {
43+
$file = func_get_arg( 0 );
44+
switch ( $file ) {
45+
case 'pass.php':
46+
return [];
47+
48+
default:
49+
return [
50+
3 => 1,
51+
];
52+
}
53+
}
54+
55+
/**
56+
* Returns the lines where warnings should occur.
57+
*
58+
* @return array <int line number> => <int number of warnings>
59+
*/
60+
public function getWarningList() {
61+
return [];
62+
}
63+
64+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
namespace HM\Coding\Standards\Tests;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
namespace HM\Coding\Standards\Tests;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
namespace HM\Coding\Standards\Tests;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
namespace HM\Coding\Standards\Tests;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
namespace HM\Coding\Standards\Tests;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
namespace HM\Coding\Standards\Tests;

HM/Tests/Layout/OrderUnitTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<?php
22

3-
class HM_Tests_Layout_OrderUnitTest extends AbstractSniffUnitTest {
3+
namespace HM\Tests\Layout;
4+
5+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
6+
7+
class OrderUnitTest extends AbstractSniffUnitTest {
48

59
/**
610
* Returns the lines where errors should occur.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
namespace HM\Coding\Standards\Tests;
4+
5+
use \HM\Other;
6+
use \WP_Post;

0 commit comments

Comments
 (0)