Skip to content
This repository has been archived by the owner on Oct 27, 2019. It is now read-only.

Commit

Permalink
Add tests with Peridot
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Apr 30, 2017
1 parent 479a563 commit 342506b
Show file tree
Hide file tree
Showing 16 changed files with 129 additions and 3 deletions.
2 changes: 0 additions & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# .gitattributes
tests/ export-ignore
.coveralls.yml export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.travis.yml export-ignore
phpunit.xml.dist export-ignore

# Auto detect text files and perform LF normalization
* text=auto
3 changes: 2 additions & 1 deletion .sniff.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"paths": [
"sniff"
"sniff",
"tests/sniff.spec.php"
],
"rules": {
"@PSR2": true
Expand Down
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ before_script:

script:
- ./sniff validate
- vendor/bin/peridot tests
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ Below is an example that enables PSR-2 + Symfony's coding standard, along with a
}
```

## Contributing

To run the tests:

```
$ composer tests
```

## Credits

This project is only a small wrapper above [PHP-CS-Fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer), a huge thanks to the contributors of that tool.
Expand Down
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,12 @@
"require": {
"php": ">=5.6",
"friendsofphp/php-cs-fixer": "^2.3"
},
"require-dev": {
"peridot-php/peridot": "^1.19",
"webmozart/assert": "^1.2"
},
"scripts": {
"tests": "peridot tests"
}
}
8 changes: 8 additions & 0 deletions tests/invalid-path-1/.sniff.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"paths": [
".."
],
"rules": {
"@PSR2": true
}
}
8 changes: 8 additions & 0 deletions tests/invalid-path-2/.sniff.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"paths": [
"/tmp"
],
"rules": {
"@PSR2": true
}
}
8 changes: 8 additions & 0 deletions tests/invalid-path-3/.sniff.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"paths": [
"./.."
],
"rules": {
"@PSR2": true
}
}
8 changes: 8 additions & 0 deletions tests/invalid/.sniff.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"paths": [
"."
],
"rules": {
"@PSR2": true
}
}
3 changes: 3 additions & 0 deletions tests/invalid/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

class foo {}
9 changes: 9 additions & 0 deletions tests/multiple-paths/.sniff.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"paths": [
"test1.php",
"test2.php"
],
"rules": {
"@PSR2": true
}
}
3 changes: 3 additions & 0 deletions tests/multiple-paths/test1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

class foo {}
3 changes: 3 additions & 0 deletions tests/multiple-paths/test2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

class foo {}
50 changes: 50 additions & 0 deletions tests/sniff.spec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

use Webmozart\Assert\Assert;

const CODE_INVALID_SYNTAX = 8;

describe('the validate command', function () {
it('should exit with the exit code 0 when there are no errors', function () {
list($code) = sniff('valid');
Assert::eq($code, 0);
});
it('should exit with an exit code greater than 0 when there are errors', function () {
list($code, $output) = sniff('invalid');
Assert::eq($code, CODE_INVALID_SYNTAX);
Assert::contains($output, 'test.php');
});
});

describe('the configuration', function () {
it('should allow to analyze multiple paths', function () {
list($code, $output) = sniff('multiple-paths');
Assert::eq($code, CODE_INVALID_SYNTAX, $output);
Assert::contains($output, 'test1.php');
Assert::contains($output, 'test2.php');
});
it('should not accept paths outside the project directory', function () {
$testCases = [
'invalid-path-1',
'invalid-path-2',
'invalid-path-3',
];
foreach ($testCases as $testCase) {
list($code, $output) = sniff($testCase);
Assert::greaterThan($code, 0);
Assert::contains($output, 'The .sniff.json configuration file contains an invalid path.');
}
});
});

it('should ignore php-cs-fixer config files', function () {
// TODO
});

function sniff($directory, $command = 'validate')
{
chdir(__DIR__ . '/' . $directory);
exec("../../sniff $command 2>&1", $output, $code);
$output = implode(PHP_EOL, (array) $output);
return [$code, $output];
}
8 changes: 8 additions & 0 deletions tests/valid/.sniff.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"paths": [
"."
],
"rules": {
"@PSR2": true
}
}
3 changes: 3 additions & 0 deletions tests/valid/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

echo 'test';

0 comments on commit 342506b

Please sign in to comment.