Skip to content

Commit

Permalink
Load all library files automatically in tests (#204)
Browse files Browse the repository at this point in the history
Because it shouldn't be this easy to forget to load a new file. Some files in the `libs` dir were more of a test subject so they were moved.
  • Loading branch information
spaze authored Jul 1, 2023
2 parents 18ee549 + 66e6ccb commit 906abf8
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 105 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
},
"scripts": {
"lint": "vendor/bin/parallel-lint --colors src/ tests/",
"lint-7.x": "vendor/bin/parallel-lint --colors src/ tests/ --exclude tests/libs/TypesEverywhere.php --exclude tests/src/disallowed/functionCallsNamedParams.php --exclude tests/src/disallowed-allow/functionCallsNamedParams.php --exclude tests/src/disallowed/attributeUsages.php --exclude tests/src/disallowed-allow/attributeUsages.php",
"lint-8.0": "vendor/bin/parallel-lint --colors src/ tests/ --exclude tests/libs/TypesEverywhere.php",
"lint-7.x": "vendor/bin/parallel-lint --colors src/ tests/ --exclude tests/src/TypesEverywhere.php --exclude tests/src/disallowed/functionCallsNamedParams.php --exclude tests/src/disallowed-allow/functionCallsNamedParams.php --exclude tests/src/disallowed/attributeUsages.php --exclude tests/src/disallowed-allow/attributeUsages.php",
"lint-8.0": "vendor/bin/parallel-lint --colors src/ tests/ --exclude tests/src/TypesEverywhere.php",
"lint-neon": "vendor/bin/neon-lint .",
"phpcs": "vendor/bin/phpcs src/ tests/",
"cs-fix": "vendor/bin/phpcbf src/ tests/",
Expand Down
16 changes: 8 additions & 8 deletions tests/Calls/FunctionCallsInMultipleNamespacesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,36 +52,36 @@ protected function getRule(): Rule
public function testRule(): void
{
// Based on the configuration above, in this file:
$this->analyse([__DIR__ . '/../libs/FunctionInMultipleNamespaces.php'], [
$this->analyse([__DIR__ . '/../src/FunctionInMultipleNamespaces.php'], [
[
// expect this error message:
'Calling __() (as alias()) is forbidden, use MyNamespace\__ instead',
// on this line:
18,
20,
],
[
'Calling MyNamespace\__() (as __()) is forbidden, ha ha ha nope',
23,
26,
],
[
'Calling printf() is forbidden, because reasons',
26,
30,
],
[
'Calling printf() is forbidden, because reasons',
27,
31,
],
[
'Calling MyNamespace\__() (as alias()) is forbidden, ha ha ha nope',
35,
39,
],
[
'Calling printf() is forbidden, because reasons',
36,
40,
],
[
'Calling printf() is forbidden, because reasons',
37,
41,
],
]);
}
Expand Down
14 changes: 7 additions & 7 deletions tests/Usages/AttributeUsagesAllowParamsMultipleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected function getRule(): Rule
AttributeEntity::class,
],
'allowIn' => [
'../libs/ClassWithAttributesAllow.php',
'../src/disallowed-allow/ClassWithAttributesAllow.php',
],
'allowParamsAnywhereAnyValue' => [
[
Expand All @@ -57,26 +57,26 @@ protected function getRule(): Rule
public function testRule(): void
{
// Based on the configuration above, in this file:
$this->analyse([__DIR__ . '/../libs/ClassWithAttributes.php'], [
$this->analyse([__DIR__ . '/../src/disallowed/ClassWithAttributes.php'], [
[
// expect this error message:
'Attribute Attributes\AttributeEntity is forbidden, because reasons',
// on this line:
10,
8,
],
]);
$this->analyse([__DIR__ . '/../libs/ClassWithAttributesAllow.php'], [
$this->analyse([__DIR__ . '/../src/disallowed-allow/ClassWithAttributesAllow.php'], [
[
'Attribute Attributes\AttributeEntity is forbidden, because reasons',
10,
8,
],
[
'Attribute Attributes\AttributeEntity is forbidden, because reasons',
15,
12,
],
[
'Attribute Attributes\AttributeEntity is forbidden, because reasons',
22,
18,
],
]);
}
Expand Down
13 changes: 7 additions & 6 deletions tests/Usages/AttributeUsagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@ class AttributeUsagesTest extends RuleTestCase

protected function getRule(): Rule
{
$allowed = new Allowed(new Formatter(new Normalizer()), new Normalizer(), new AllowedPath(new FilePath(new FileHelper(__DIR__))));
$normalizer = new Normalizer();
$allowed = new Allowed(new Formatter($normalizer), $normalizer, new AllowedPath(new FilePath(new FileHelper(__DIR__))));
return new AttributeUsages(
new DisallowedAttributeRuleErrors($allowed, new Identifier()),
new DisallowedAttributeFactory($allowed, new Normalizer()),
new DisallowedAttributeFactory($allowed, $normalizer),
[
[
'attribute' => [
AttributeEntity::class,
],
'allowIn' => [
'../libs/ClassWithAttributesAllow.php',
'../src/disallowed-allow/ClassWithAttributesAllow.php',
],
'allowParamsAnywhereAnyValue' => [
[
Expand All @@ -48,15 +49,15 @@ protected function getRule(): Rule
public function testRule(): void
{
// Based on the configuration above, in this file:
$this->analyse([__DIR__ . '/../libs/ClassWithAttributes.php'], [
$this->analyse([__DIR__ . '/../src/disallowed/ClassWithAttributes.php'], [
[
// expect this error message:
'Attribute Attributes\AttributeEntity is forbidden, because reasons',
// on this line:
10,
8,
],
]);
$this->analyse([__DIR__ . '/../libs/ClassWithAttributesAllow.php'], []);
$this->analyse([__DIR__ . '/../src/disallowed-allow/ClassWithAttributesAllow.php'], []);
}

}
2 changes: 1 addition & 1 deletion tests/Usages/NamespaceUsagesTypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected function getRule(): Rule
public function testRule(): void
{
// Based on the configuration above, in this file:
$this->analyse([__DIR__ . '/../libs/TypesEverywhere.php'], [
$this->analyse([__DIR__ . '/../src/TypesEverywhere.php'], [
[
// expect this error message:
'Namespace Waldo\Quux\Blade is forbidden, do androids dream of electric sheep?',
Expand Down
17 changes: 7 additions & 10 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@
declare(strict_types = 1);

require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/libs/Bar.php';
require_once __DIR__ . '/libs/Blade.php';
require_once __DIR__ . '/libs/Constructor.php';
require_once __DIR__ . '/libs/Functions.php';
require_once __DIR__ . '/libs/Inheritance.php';
require_once __DIR__ . '/libs/Royale.php';
require_once __DIR__ . '/libs/SomeInterface.php';
require_once __DIR__ . '/libs/Option.php';
require_once __DIR__ . '/libs/TestTrait.php';
require_once __DIR__ . '/libs/Traits.php';

$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__ . '/libs', FilesystemIterator::SKIP_DOTS));
foreach ($iterator as $fileInfo) {
if ($fileInfo->getExtension() === 'php') {
require_once $fileInfo->getPathname();
}
}
34 changes: 0 additions & 34 deletions tests/libs/ClassWithAttributes.php

This file was deleted.

34 changes: 0 additions & 34 deletions tests/libs/ClassWithAttributesAllow.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
declare(strict_types=1);
declare(strict_types = 1);

namespace {

Expand All @@ -11,18 +11,22 @@ function __(): string

namespace MyNamespace {

// phpcs:ignore SlevomatCodingStandard.Namespaces.UseSpacing.IncorrectLinesCountAfterLastUse
use function __ as alias;


function __(): string
{
return alias();
}


function someOtherFn(): string
{
return __(); // The __ used here is MyNamespace\__
return __(); // The __ used here is MyNamespace\__
}


printf('oo');
\printf('oo');

Expand All @@ -37,4 +41,3 @@ function someOtherFn(): string
\printf('oo');

}

File renamed without changes.
29 changes: 29 additions & 0 deletions tests/src/disallowed-allow/ClassWithAttributesAllow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
declare(strict_types = 1);

namespace Attributes;

use Waldo\Quux\Blade;

#[\Attributes\AttributeEntity] // allowed by path in AttributeUsagesTest, disallowed in AttributeUsagesAllowParamsMultipleTest because no $repositoryClass parameter specified
class ClassWithAttributesAllow
{

#[\Attributes\AttributeEntity(repositoryClass: \Attributes\UserRepository::class, readOnly: false)] // allowed by path in AttributeUsagesTest, disallowed in AttributeUsagesAllowParamsMultipleTest because $repositoryClass has other value
public function hasAvocado(): bool
{
}


#[\Attributes\AttributeEntity(\Attributes\UserRepository::class)] // allowed by path in AttributeUsagesTest, disallowed in AttributeUsagesAllowParamsMultipleTest because $repositoryClass has other value
public function hasTuna(): bool
{
}


#[\Attributes\AttributeEntity(Blade::class)] // allowed in all tests, $repositoryClass present with allowed value
public function hasKetchup(): bool
{
}

}
29 changes: 29 additions & 0 deletions tests/src/disallowed/ClassWithAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
declare(strict_types = 1);

namespace Attributes;

use Waldo\Quux\Blade;

#[AttributeEntity] // disallowed, no $repositoryClass parameter specified
class ClassWithAttributes
{

#[AttributeEntity(repositoryClass: UserRepository::class, readOnly: false)] // disallowed, $repositoryClass present with any value
public function hasAvocado(): bool
{
}


#[AttributeEntity(UserRepository::class)] // allowed, $repositoryClass present with any value
public function hasTuna(): bool
{
}


#[AttributeEntity(Blade::class)] // allowed, $repositoryClass present with any value
public function hasKetchup(): bool
{
}

}

0 comments on commit 906abf8

Please sign in to comment.