Skip to content
This repository was archived by the owner on Feb 11, 2020. It is now read-only.

Adding parsing for @testWith + travis config #1

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
language: php

sudo: false

addons:
apt:
packages:
- libxml2-utils

php:
- 7.2
- master

matrix:
allow_failures:
- php: master
fast_finish: true

env:
matrix:
- DEPENDENCIES="high"
- DEPENDENCIES="low"
global:
- DEFAULT_COMPOSER_FLAGS="--no-interaction --no-ansi --no-progress --no-suggest"

before_install:
- composer clear-cache

install:
- if [[ "$DEPENDENCIES" = 'high' ]]; then travis_retry composer update $DEFAULT_COMPOSER_FLAGS; fi
- if [[ "$DEPENDENCIES" = 'low' ]]; then travis_retry composer update $DEFAULT_COMPOSER_FLAGS --prefer-lowest; fi

before_script:
- echo 'zend.assertions=1' >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
- echo 'assert.exception=On' >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini

script:
- ./vendor/bin/phpunit --coverage-clover=coverage.xml

after_success:
- bash <(curl -s https://codecov.io/bash)

notifications:
email: false

cache:
directories:
- $HOME/.composer/cache/

jobs:
include:
- stage: Static Code Analysis
php: 7.2
name: PHP Cs Fixer
before_install:
- phpenv config-rm xdebug.ini || true
- mkdir ../tools && composer init --name=phpu/tools --working-dir=../tools
- composer require friendsofphp/php-cs-fixer:^2.12 --working-dir=../tools
install: true
script:
- ../tools/vendor/bin/php-cs-fixer fix --dry-run -v --show-progress=dots --diff-format=udiff
- stage: Static Code Analysis
php: 7.2
name: PHPstan
before_install:
- phpenv config-rm xdebug.ini || true
- mkdir ../tools && composer init --name=phpu/tools --working-dir=../tools
- composer require phpstan/phpstan:^0.10 --working-dir=../tools
install: composer update
script:
- ../tools/vendor/bin/phpstan analyse --level max src tests
2 changes: 1 addition & 1 deletion src/Finder/FilesystemCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(string $directory)
public function get(string $sourceFile): TestCollection
{
return \unserialize(
\file_get_contents($this->cacheFile($sourceFile)),
\file_get_contents($this->cacheFile($sourceFile)) ?: '',
[
TestCollection::class,
TestMethod::class
Expand Down
20 changes: 19 additions & 1 deletion src/Finder/TestFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,19 +169,37 @@ private function isTestMethod(ReflectionMethod $method): bool
private function annotations(string $docBlock): AnnotationCollection
{
$annotations = new AnnotationCollection;
$docBlock = (string) \substr($docBlock, 3, -2);
$docBlock = \substr($docBlock, 3, -2);

if (!$docBlock) {
return $annotations;
}

if (\preg_match_all('/@(?P<name>[A-Za-z_-]+)(?:[ \t]+(?P<value>.*?))?[ \t]*\r?$/m', $docBlock, $matches)) {
$numMatches = \count($matches[0]);

for ($i = 0; $i < $numMatches; ++$i) {
if ($matches['name'][$i] === 'testWith') {
continue;
}
$annotations->add(
new Annotation(
(string) $matches['name'][$i],
(string) $matches['value'][$i]
)
);
}

if (\preg_match_all('/@(?P<name>testWith)[\t ](?P<value>(\[.*?\]|[\n\r]+[\t ]*\*[\t ]+)+)/', $docBlock, $matches)) {
foreach ($matches['value'] as $value) {
$annotations->add(
new Annotation(
'testWith',
'[' . \implode(',', \preg_split('/[ \t]*[\r\n][ \t]*\*[ \t]*/', $value) ?: []) . ']'
)
);
}
}
}

return $annotations;
Expand Down
23 changes: 23 additions & 0 deletions tests/_fixture/TestWithTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\NewRunner\TestFixture;

final class TestWithTest extends MyTestCase
{
/**
* @testWith [1]
* [8]
* [12]
*/
public function testSeven($value): void
{
$this->assertInternalType('int', $value);
}
}
34 changes: 27 additions & 7 deletions tests/unit/Finder/TestFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\NewRunner\TestFixture\BarTest;
use PHPUnit\NewRunner\TestFixture\FooTest;
use PHPUnit\NewRunner\TestFixture\MyTestCase;
use PHPUnit\NewRunner\TestFixture\TestWithTest;

/**
* @covers PHPUnit\NewRunner\TestFinder
Expand Down Expand Up @@ -46,7 +47,7 @@ protected function setUp(): void

$this->finder = new TestFinder($cache);

$this->fixtureDirectory = \realpath(__DIR__ . '/../../_fixture');
$this->fixtureDirectory = (string) \realpath(__DIR__ . '/../../_fixture');
}

public function testFindsTestMethods(): void
Expand All @@ -55,7 +56,7 @@ public function testFindsTestMethods(): void

$this->assertContains(
new TestMethod(
$this->fixtureDirectory . '/' . 'FooTest.php',
$this->fixtureDirectory . \DIRECTORY_SEPARATOR . 'FooTest.php',
FooTest::class,
'testOne',
AnnotationCollection::fromArray(
Expand All @@ -74,7 +75,7 @@ public function testFindsTestMethods(): void

$this->assertContains(
new TestMethod(
$this->fixtureDirectory . '/' . 'FooTest.php',
$this->fixtureDirectory . \DIRECTORY_SEPARATOR . 'FooTest.php',
FooTest::class,
'testTwo',
AnnotationCollection::fromArray(
Expand All @@ -98,7 +99,7 @@ public function testFindsTestMethods(): void

$this->assertContains(
new TestMethod(
$this->fixtureDirectory . '/' . 'FooTest.php',
$this->fixtureDirectory . \DIRECTORY_SEPARATOR . 'FooTest.php',
FooTest::class,
'testThree',
AnnotationCollection::fromArray(
Expand All @@ -122,7 +123,7 @@ public function testFindsTestMethods(): void

$this->assertNotContains(
new TestMethod(
$this->fixtureDirectory . '/' . 'MyTestCase.php',
$this->fixtureDirectory . \DIRECTORY_SEPARATOR . 'MyTestCase.php',
MyTestCase::class,
'testOne',
AnnotationCollection::fromArray(
Expand All @@ -141,7 +142,7 @@ public function testFindsTestMethods(): void

$this->assertContains(
new TestMethod(
$this->fixtureDirectory . '/' . 'BarTest.php',
$this->fixtureDirectory . \DIRECTORY_SEPARATOR . 'BarTest.php',
BarTest::class,
'testOne',
AnnotationCollection::fromArray(
Expand All @@ -160,7 +161,7 @@ public function testFindsTestMethods(): void

$this->assertContains(
new TestMethod(
$this->fixtureDirectory . '/' . 'BarTest.php',
$this->fixtureDirectory . \DIRECTORY_SEPARATOR . 'BarTest.php',
BarTest::class,
'testTwo',
AnnotationCollection::fromArray(
Expand All @@ -176,5 +177,24 @@ public function testFindsTestMethods(): void
false,
false
);

$this->assertContains(
new TestMethod(
$this->fixtureDirectory . \DIRECTORY_SEPARATOR . 'TestWithTest.php',
TestWithTest::class,
'testSeven',
AnnotationCollection::fromArray(),
AnnotationCollection::fromArray(
new Annotation(
'testWith',
'[[1],[8],[12]]'
)
)
),
$tests,
'',
false,
false
);
}
}