Skip to content

Commit

Permalink
Merge pull request #258 from lmc-eu/feature/reflections-replace
Browse files Browse the repository at this point in the history
Replace deprecated nette/reflection
  • Loading branch information
OndraM committed Nov 30, 2020
2 parents 1aabbce + ea97682 commit 0dae025
Show file tree
Hide file tree
Showing 22 changed files with 435 additions and 76 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -19,6 +19,7 @@
- Don't hardcode timezone to `Europe/Prague`. Timezone is now used based on your PHP settings ([date.timezone](https://php.net/manual/en/datetime.configuration.php#ini.date.timezone)).
- Server URL now must be provided including URL prefix (if it has one, like `/wd/hub`) - eg. `http://foo.bar:4444/wd/hub`. This means the `/wd/hub` part is now never auto-amended.
- Package `symfony/polyfill-mbstring` now provides mbstring functions even if PHP mbstring extension is not installed.
- Read annotations (like `@group`, `@noBrowser` etc.) using different and more-robust underlying library.

### Fixed
- Remote server running in W3C-protocol mode (eg. Selenium v3.5.3+) was erroneously detected as BrowserStack cloud service.
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -32,10 +32,11 @@
"doctrine/inflector": "^2.0.3",
"florianwolters/component-util-singleton": "0.3.2",
"graphp/algorithms": "^0.8.1",
"nette/reflection": "^2.4.2",
"ondram/ci-detector": "^3.1",
"php-webdriver/webdriver": "^1.8.1",
"phpdocumentor/reflection-docblock": "^5.2",
"phpunit/phpunit": "^7.5.20",
"roave/better-reflection": "^4.3",
"symfony/console": "^4.0",
"symfony/event-dispatcher": "^4.0",
"symfony/filesystem": "^4.0",
Expand Down
1 change: 1 addition & 0 deletions easy-coding-standard.yaml
Expand Up @@ -18,3 +18,4 @@ parameters:
exclude_files:
- 'src-tests/coverage/*'
- 'src-tests/FunctionalTests/logs/coverage/*'
- 'src-tests/Utils/Annotations/Fixtures/*'

This file was deleted.

2 changes: 0 additions & 2 deletions src-tests/Process/Fixtures/InvalidTests/NoClassTest.php

This file was deleted.

21 changes: 0 additions & 21 deletions src-tests/Process/ProcessSetCreatorTest.php
Expand Up @@ -120,16 +120,6 @@ public function testShouldCreateProcessSetFromGivenFiles(): void
$this->assertSame('/foo/bar/logs', $testEnv['LOGS_DIR']);
}

public function testShouldThrowExceptionIfAddingFileWithNoClass(): void
{
$files = $this->findDummyTests('NoClassTest.php', 'InvalidTests');

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessageRegExp('/No class found in file ".*NoClassTest.php"/');

$this->creator->createFromFiles($files, [], []);
}

public function testShouldThrowExceptionIfAddingClassWithNameMismatchingTheFileName(): void
{
$files = $this->findDummyTests('WrongClassTest.php', 'InvalidTests');
Expand All @@ -143,17 +133,6 @@ public function testShouldThrowExceptionIfAddingClassWithNameMismatchingTheFileN
$this->creator->createFromFiles($files, [], []);
}

public function testShouldThrowExceptionIfMultipleClassesAreDefinedInFile(): void
{
$files = $this->findDummyTests('MultipleClassesInFileTest.php', 'InvalidTests');

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessageRegExp(
'/File ".*MultipleClassesInFileTest.php" contains definition of 2 classes/'
);
$this->creator->createFromFiles($files, [], []);
}

public function testShouldOnlyAddTestsOfGivenGroups(): void
{
$processSet = $this->creator->createFromFiles($this->findDummyTests(), ['bar', 'foo'], []);
Expand Down
136 changes: 136 additions & 0 deletions src-tests/Utils/Annotations/ClassAnnotationsTest.php
@@ -0,0 +1,136 @@
<?php declare(strict_types=1);

namespace Lmc\Steward\Utils\Annotations;

use Lmc\Steward\Utils\Annotations\Fixtures\ClassEmptyDockBlock;
use Lmc\Steward\Utils\Annotations\Fixtures\ClassKeyAnnotations;
use Lmc\Steward\Utils\Annotations\Fixtures\ClassKeyValueAnnotations;
use Lmc\Steward\Utils\Annotations\Fixtures\ClassMixedAnnotations;
use Lmc\Steward\Utils\Annotations\Fixtures\ClassNoDocBlock;
use Lmc\Steward\Utils\Annotations\Fixtures\ClassWithMethods;
use PHPUnit\Framework\TestCase;

/**
* @covers \Lmc\Steward\Utils\Annotations\ClassAnnotations
*/
class ClassAnnotationsTest extends TestCase
{
/**
* @dataProvider provideClass
*/
public function testShouldGetAnnotationsForClass(string $className, array $expectedAnnotations): void
{
$annotations = ClassAnnotations::getAnnotationsForClass($className);

$this->assertSame($expectedAnnotations, $annotations);
}

/**
* @return array[]
*/
public function provideClass(): array
{
return [
'Class with empty doc block' => [
ClassEmptyDockBlock::class,
[],
],
'Class having no doc block at all' => [
ClassNoDocBlock::class,
[],
],
'Class with key-only annotations, without values' => [
ClassKeyAnnotations::class,
[
'first' => ['', ''],
'second' => [''],
'third' => [''],
],
],
'Class with key-value annotations' => [
ClassKeyValueAnnotations::class,
[
'first' => ['First value', 'Second value of first'],
'second' => ['Second value'],
'third' => ['Third "special" value'],
],
],
'Class with mixed key-only and key-value annotations' => [
ClassMixedAnnotations::class,
[
'first' => ['', 'First with some value'],
'second' => [''],
'third' => [''],
'fourth' => ['Fourth value'],
],
],
];
}

public function testShouldGetAnnotationsForInstances(): void
{
$annotations = ClassAnnotations::getAnnotationsForInstance(new ClassKeyAnnotations());

$this->assertSame(
[
'first' => ['', ''],
'second' => [''],
'third' => [''],
],
$annotations
);
}

/**
* @dataProvider provideMethod
*/
public function testShouldGetAnnotationsForMethodInClass(string $methodName, array $expectedAnnotations): void
{
$classInstance = new ClassWithMethods();

$annotations = ClassAnnotations::getAnnotationsForMethodOfInstance($classInstance, $methodName);

$this->assertSame($expectedAnnotations, $annotations);
}

/**
* @return array[]
*/
public function provideMethod(): array
{
return [
'Method with empty doc block' => [
'methodWithEmptyAnnotations',
[],
],
'Method having no doc block at all' => [
'methodWithout',
[],
],
'Method with key-only annotations, without values' => [
'methodWithKeys',
[
'first' => ['', ''],
'second' => [''],
'third' => [''],
],
],
'Method with key-value annotations' => [
'methodWithKeyValues',
[
'first' => ['First value', 'Second value of first'],
'second' => ['Second value'],
'third' => ['Third "special" @value!'],
],
],
'Method with mixed key-only and key-value annotations' => [
'methodWithMixedKeyValues',
[
'first' => ['', 'First with some value'],
'second' => ['Second with value', ''],
'third' => [''],
],
],
];
}
}
45 changes: 45 additions & 0 deletions src-tests/Utils/Annotations/ClassParserTest.php
@@ -0,0 +1,45 @@
<?php declare(strict_types=1);

namespace Lmc\Utils\Annotations;

use Lmc\Steward\Utils\Annotations\ClassParser;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Finder\SplFileInfo;

/**
* @covers \Lmc\Steward\Utils\Annotations\ClassParser
*/
class ClassParserTest extends TestCase
{
public function testShouldGetClassNameFromFile(): void
{
$file = $this->createFileInfo('ClassNoDocBlock.php');

$className = ClassParser::readClassNameFromFile($file);

$this->assertSame('Lmc\Steward\Utils\Annotations\Fixtures\ClassNoDocBlock', $className);
}

public function testShouldThrowExceptionForMultipleClassesInOneFile(): void
{
$file = $this->createFileInfo('MultipleClassesInFile.php');

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessageRegExp('/^File ".+MultipleClassesInFile.php" contains definition of 2 classes\./');
ClassParser::readClassNameFromFile($file);
}

public function testShouldThrowExceptionForNoClassInOneFile(): void
{
$file = $this->createFileInfo('NoClassInFile.php');

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessageRegExp('/^No class found in file ".+NoClassInFile.php"/');
ClassParser::readClassNameFromFile($file);
}

private function createFileInfo(string $fileName): SplFileInfo
{
return new SplFileInfo(__DIR__ . '/Fixtures/' . $fileName, 'Fixtures/', 'Fixtures/' . $fileName);
}
}
10 changes: 10 additions & 0 deletions src-tests/Utils/Annotations/Fixtures/ClassEmptyDockBlock.php
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace Lmc\Steward\Utils\Annotations\Fixtures;

/**
*
*/
class ClassEmptyDockBlock
{
}
15 changes: 15 additions & 0 deletions src-tests/Utils/Annotations/Fixtures/ClassKeyAnnotations.php
@@ -0,0 +1,15 @@
<?php declare(strict_types=1);

namespace Lmc\Steward\Utils\Annotations\Fixtures;

/**
* Some text with @tag, which will not be treated as annotation.
*
* @first
* @second
* @third
* @first
*/
class ClassKeyAnnotations
{
}
13 changes: 13 additions & 0 deletions src-tests/Utils/Annotations/Fixtures/ClassKeyValueAnnotations.php
@@ -0,0 +1,13 @@
<?php declare(strict_types=1);

namespace Lmc\Steward\Utils\Annotations\Fixtures;

/**
* @first First value
* @second Second value
* @third Third "special" value
* @first Second value of first
*/
class ClassKeyValueAnnotations
{
}
14 changes: 14 additions & 0 deletions src-tests/Utils/Annotations/Fixtures/ClassMixedAnnotations.php
@@ -0,0 +1,14 @@
<?php declare(strict_types=1);

namespace Lmc\Steward\Utils\Annotations\Fixtures;

/**
* @first
* @second
* @first First with some value
* @third
* @fourth Fourth value
*/
class ClassMixedAnnotations
{
}
7 changes: 7 additions & 0 deletions src-tests/Utils/Annotations/Fixtures/ClassNoDocBlock.php
@@ -0,0 +1,7 @@
<?php declare(strict_types=1);

namespace Lmc\Steward\Utils\Annotations\Fixtures;

class ClassNoDocBlock
{
}
56 changes: 56 additions & 0 deletions src-tests/Utils/Annotations/Fixtures/ClassWithMethods.php
@@ -0,0 +1,56 @@
<?php declare(strict_types=1);

namespace Lmc\Steward\Utils\Annotations\Fixtures;

/**
* This class has few methods with various annotations.
*
*/
class ClassWithMethods
{
public function methodWithout(): void
{
// method without annotations
}

/**
*
*/
public function methodWithEmptyAnnotations(): void
{
// method with empty docBlock
}

/**
* Some text not treated as @annotation
*
* @first
* @second
* @first
* @third
*/
public function methodWithKeys(): void
{
}

/**
* @first First value
* @second Second value
* @first Second value of first
* @third Third "special" @value!
*/
public function methodWithKeyValues(): void
{
}

/**
* @first
* @second Second with value
* @first First with some value
* @second
* @third
*/
public function methodWithMixedKeyValues(): void
{
}
}
11 changes: 11 additions & 0 deletions src-tests/Utils/Annotations/Fixtures/MultipleClassesInFile.php
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

namespace Lmc\Steward\Utils\Annotations\Fixtures;

class ClassFirst
{
}

class ClassSecond
{
}

0 comments on commit 0dae025

Please sign in to comment.