Skip to content

Commit

Permalink
Add base repository config (#1)
Browse files Browse the repository at this point in the history
* Add base repository config

* CS (#2)

* CS

* Fix scrut issues

* Update link

* Add test

* Improve test

* Fix readme

* Fix readme

* Fix readme

* Remove useless const

* Less strict message comparaison

* Add behat tests

* Fix

* Improve tests

* Improve listener and tests

* Fix

* Add doc about static attributes

* Fix doc
  • Loading branch information
yoanm committed Jan 14, 2017
1 parent 5d3065e commit 2305291
Show file tree
Hide file tree
Showing 25 changed files with 761 additions and 59 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
vendor
build
composer.lock
.gitkeep
78 changes: 78 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
checks:
php:
code_rating: true
duplication: true
build_failure_conditions:
- 'project.metric_change("scrutinizer.quality", < -0.10)'
- 'elements.rating(<= D).exists' # No classes/methods with a rating of D or worse
- 'issues.label("coding-style").exists' # No coding style issues allowed
- 'issues.severity(>= MAJOR).exists' # New major or higher severity issues
- 'project.metric("scrutinizer.quality", < 9)' # Code Quality Rating drops below 9
- 'project.metric("scrutinizer.test_coverage", < 0.90)' # Code Coverage drops below 90%
- 'project.metric_change("scrutinizer.test_coverage", < -0.02)'
- 'patches.label("Doc Comments").exists' # No doc comments patches allowed
- 'patches.label("Spacing").exists' # No spacing patches allowed
build:
cache:
directories:
- ~/.composer

dependencies:
before:
- composer global require hirak/prestissimo
override:
- composer build-ci

environment:
mysql: false
postgresql: false
elasticsearch: false
redis: false

tests:
override:
- composer cs
-
command: composer coverage-clover
idle_timeout: 1200
coverage:
file: 'build/coverage/clover.xml'
format: 'php-clover'

tools:
php_analyzer:
enabled: true
config:
checkstyle:
enabled: true
naming:
isser_method_name: ^.*$
utility_class_name: ^.*$
doc_comment_fixes:
enabled: false
reflection_fixes:
enabled: false
use_statement_fixes:
enabled: false
simplify_boolean_return:
enabled: true
external_code_coverage: false
php_code_coverage: false
php_changetracking: true
php_cpd: true
php_cs_fixer: false
php_mess_detector: true
php_pdepend: true
sensiolabs_security_checker: true

filter:
paths:
- src/*

coding_style:
php:
spaces:
before_parentheses:
closure_definition: true
around_operators:
concatenation: false
24 changes: 24 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
language: php

php:
- '5.5'
- '5.6'
- '7.0'

sudo: false

matrix:
fast_finish: true

before_install:
- composer global require hirak/prestissimo

install:
- composer build-ci

script:
- composer test

cache:
directories:
- $HOME/.composer
31 changes: 31 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Contributing

## Getting Started
* Fork, then clone the repo:
```bash
git clone git@github.com:your-username/PhpUnitExtended.git
````

* Make sure everything goes well:
```bash
composer ci
```

* Make your changes (Add/Update tests according to your changes).
* Make sure tests are still green:
```bash
composer ci
```

* To check code coverage, launch
```bash
composer coverage
```

* Push to your fork and [submit a pull request](https://github.com/yoanm/PhpUnitExtended/compare/).
* Wait for feedback or merge.

Some stuff that will increase your pull request's acceptance:
* Write tests.
* Follow PSR-2 coding style.
* Write good commit messages.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017 Yo
Copyright (c) 2017 Yoanm

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
35 changes: 29 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,47 @@
# PhpUnitExtended
[![Scrutinizer Build Status](https://img.shields.io/scrutinizer/build/g/yoanm/PhpUnitExtended.svg?label=Scrutinizer)](https://scrutinizer-ci.com/g/yoanm/PhpUnitExtended/?branch=master) [![Scrutinizer Code Quality](https://img.shields.io/scrutinizer/g/yoanm/PhpUnitExtended.svg?label=Code%20quality)](https://scrutinizer-ci.com/g/yoanm/PhpUnitExtended/?branch=master) [![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/yoanm/PhpUnitExtended.svg?label=Coverage)](https://scrutinizer-ci.com/g/yoanm/PhpUnitExtended/?branch=master)

Php library to extend PhpUnit
[![Travis Build Status](https://img.shields.io/travis/yoanm/PhpUnitExtended/master.svg?label=travis)](https://travis-ci.org/yoanm/PhpUnitExtended) [![PHP Versions](https://img.shields.io/badge/php-5.5%20%2F%205.6%20%2F%207.0-8892BF.svg)](https://php.net/)

[![Latest Stable Version](https://img.shields.io/packagist/v/yoanm/php-unit-extended.svg)](https://packagist.org/packages/yoanm/php-unit-extended)

Php library to extend PhpUnit
> :information_source: **[Yoanm Tests strategy](https://github.com/yoanm/Readme/blob/master/strategy/tests/README.md) compliant**
# Install
```bash
composer require --dev yoanm/php-unit-extended
```

* [Configuration reference](#configuration-reference)
* [Tests strategy rules validated by configuration reference](#rules-validated)
* [Mandatory](#rules-validated-mandatory)
* [Listeners](#rules-validated-mandatory-listeners)
* [TestStrategyListener](#rules-validated-mandatory-listeners-TestsStrategyListener)
* [**Strict mode - fails if - risky tests**](#rules-validated-mandatory-listeners-TestsStrategyListener-rule-1)
* [**Real coverage - risky tests does not count in coverage**](#rules-validated-mandatory-listeners-TestsStrategyListener-rule-1)

## Configuration reference
```xml
<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.8/phpunit.xsd"
<!-- To convert test that output something into failed test -->
beStrictAboutOutputDuringTests="true"

<!-- To convert test that manipulates globals into failed test -->
backupGlobals="true"<!-- For globals variables -->
backupStaticAttributes="true"<!-- For static attributes -->
beStrictAboutChangesToGlobalState="true"

<!-- To convert test that do not test anything into failed test -->
beStrictAboutTestsThatDoNotTestAnything="true"

<!-- To convert test that have not expected coverage into failed test -->
forceCoversAnnotation="true"
checkForUnintentionallyCoveredCode="true"
>
<listeners>
<listener class="Yoanm\PhpUnitExtended\Listener\TestsStrategyListener"/>
Expand All @@ -38,14 +58,17 @@ Php library to extend PhpUnit
#### Listeners
<a name="rules-validated-mandatory-listeners-TestsStrategyListener"></a>
* [TestsStrategyListener](./src/Yoanm/PhpUnitExtended/Listener/TestsStrategyListener.php)

Listener will validate following mandatory rule
<a name="rules-validated-mandatory-listeners-TestsStrategyListener-rule-1"></a>
* [Strict mode - fails if - risky tests](https://github.com/yoanm/Readme/blob/master/strategy/tests/README.md#rules-strict-mode-fails-if-risky-tests)
* Requires
* `beStrictAboutOutputDuringTests="true"`
* `beStrictAboutChangesToGlobalState="true"`
* `beStrictAboutTestsThatDoNotTestAnything="true"`
* `beStrictAboutOutputDuringTests="true"`
* `beStrictAboutChangesToGlobalState="true"` with `backupGlobals="true"`
* `beStrictAboutTestsThatDoNotTestAnything="true"`
<a name="rules-validated-mandatory-listeners-TestsStrategyListener-rule-1"></a>
* [Real coverage - risky tests does not count in coverage](https://github.com/yoanm/Readme/blob/master/strategy/tests/README.md#rules-real-coverage-risky-tests) for some specific kinds of risky test
* Requires `beStrictAboutOutputDuringTests="true"`

# Contributing
See [contributing note](./CONTRIBUTING.md)
5 changes: 5 additions & 0 deletions app_test/behat.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
default:
suites:
default:
contexts:
- Functional\Yoanm\PhpUnitExtended\BehatContext\FeatureContext: ~
33 changes: 33 additions & 0 deletions app_test/features/base/TestStrategyListener.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Feature: TestStrategyListener

Scenario: Risky output
Given I run "risky-output" phpunit test-suite
Then I should have 1 failure
Then I should have a failure containing following message "1\) RiskyOutputTest::test"
Then I should have a failure containing following message "Strict mode - No output during test"
And I should have a failure containing following message "TEST_OUTPUT_DURING_TEST"

Scenario: Risky coverage
Given I run "risky-coverage" phpunit test-suite with coverage
Then I should have 1 failure
Then I should have a failure containing following message "1\) RiskyCoverageTest::test"
Then I should have a failure containing following message "Strict mode - Executed code must be defined with @covers and @uses annotations"
And I should have a failure containing following message "src/DefaultClass.php:10"

Scenario: Risky test that do not test anything
Given I run "risky-nothing" phpunit test-suite
Then I should have 1 failure
Then I should have a failure containing following message "1\) RiskyNothingTest::test"
And I should have a failure containing following message "No test that do not test anything"

Scenario: Risky globals manipulation
Given I run "risky-globals" phpunit test-suite
Then I should have 1 failure
Then I should have a failure containing following message "1\) RiskyGlobalsTest::test"
Then I should have a failure containing following message "No global variable manipulation during test"

Scenario: Risky test that do not test anything
Given I run "risky-static" phpunit test-suite
Then I should have 1 failure
Then I should have a failure containing following message "1\) RiskyStaticTest::test"
And I should have a failure containing following message "No static attribute manipulation during test"
70 changes: 70 additions & 0 deletions app_test/features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
namespace Functional\Yoanm\PhpUnitExtended\BehatContext;

use Behat\Behat\Context\Context;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\ProcessBuilder;

/**
* Defines application features from the specific context.
*/
class FeatureContext implements Context
{

/** @var Process|null */
private $process = null;

/**
* @Given I run :testSuiteName phpunit test-suite
*/
public function iRunPhpunitTestSuite($testSuiteName, $withCoverage = false)
{
$argList = [
sprintf('%s/%s/vendor/bin/phpunit', __DIR__, '../../..'),
'--testsuite',
$testSuiteName,
'-c',
sprintf('%s/%s/phpunit.xml', __DIR__, '../..'),
];
if (true === $withCoverage) {
$argList[] = '--coverage-text';
}
$processBuilder = new ProcessBuilder($argList);
$this->process = $processBuilder->getProcess();

$this->process->run();
}

/**
* @Given I run :testSuiteName phpunit test-suite with coverage
*/
public function iRunPhpunitTestSuiteWithCoverage($testSuiteName)
{
$this->iRunPhpunitTestSuite($testSuiteName, true);
}

/**
* @Then /^I should have (?P<expectedFailureCount>\d+) failure(s)?$/
*/
public function iShouldHaveXFailures($expectedFailureCount)
{
$output = $this->process->getOutput();

$failureCount = preg_match_all(sprintf('#There was %s failure#', $expectedFailureCount), $output);
if ($failureCount != $expectedFailureCount) {
throw new \Exception(sprintf('Found %d failure, but %d expected', $failureCount, $expectedFailureCount));
}
}

/**
* @Then I should have a failure containing following message :expectedMessage
*/
public function iShouldHaveAFailureContainingFollowingMessage($expectedMessage)
{
$output = $this->process->getOutput();

if (!preg_match(sprintf('#%s#', $expectedMessage), $output)) {
throw new \Exception(sprintf('"%s" not found in : %s', $expectedMessage, $output));
}
}
}
41 changes: 41 additions & 0 deletions app_test/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.8/phpunit.xsd"
beStrictAboutOutputDuringTests="true"
backupGlobals="true"
beStrictAboutChangesToGlobalState="true"
beStrictAboutTestsThatDoNotTestAnything="true"
checkForUnintentionallyCoveredCode="true"
forceCoversAnnotation="true"
bootstrap="../vendor/autoload.php"
>
<listeners>
<listener class="Yoanm\PhpUnitExtended\Listener\TestsStrategyListener"/>
</listeners>
<directory>tests/*</directory>
<testsuites>
<testsuite name="risky-output">
<file>tests/RiskyOutputTest.php</file>
</testsuite>
<testsuite name="risky-coverage">
<file>tests/RiskyCoverageTest.php</file>
</testsuite>
<testsuite name="risky-nothing">
<file>tests/RiskyNothingTest.php</file>
</testsuite>
<testsuite name="risky-globals">
<file>tests/RiskyGlobalsTest.php</file>
</testsuite>
<testsuite name="risky-static">
<file>tests/RiskyStaticTest.php</file>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>src</directory>
</whitelist>
</filter>
</phpunit>
18 changes: 18 additions & 0 deletions app_test/src/DefaultClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace AppTest;

class DefaultClass
{
static $value = null;

public function justForTest($b = 2)
{
if ($b % 2) {
$a = $b/2;
} else {
$a = $b*2;
}

return $a === $b;
}
}

0 comments on commit 2305291

Please sign in to comment.