Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TASK] Run unit tests #3

Merged
merged 4 commits into from
Nov 22, 2023
Merged
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
6 changes: 4 additions & 2 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ jobs:
dependency-versions: "locked"
- name: "Lint PHP"
run: find . -type f -name '*.php' ! -path "./.Build/*" -print0 | xargs -0 -n1 -P4 php -l -n | (! grep -v "No syntax errors detected" )
- name: "CGL"
run: "make code-style"
- name: "Run unit tests"
run: make test

quality:
name: Quality
Expand All @@ -50,3 +50,5 @@ jobs:
dependency-versions: "locked"
- name: "Check composer.json"
run: "composer normalize --dry-run"
- name: "CGL"
run: "make code-style"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/**/temp/
/Documentation-GENERATED-temp/
/.php-cs-fixer.cache
/.phpunit.result.cache
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
.PHONY: qa
qa: fix-code-style test

.PHONY: code-style
code-style: ## Executes php-cs-fixer with "check" option
vendor/bin/php-cs-fixer check

.PHONY: fix-code-style
fix-code-style: ## Executes php-cs-fixer with "fix" option
vendor/bin/php-cs-fixer fix

.PHONY: test
test: ## Runs unit tests with phpunit
vendor/bin/phpunit
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"require-dev": {
"ergebnis/composer-normalize": "^2.39",
"friendsofphp/php-cs-fixer": "^3.39",
"phpdocumentor/guides-cli": "dev-main"
"phpdocumentor/guides-cli": "dev-main",
"phpunit/phpunit": "^10.4"
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand All @@ -20,6 +21,11 @@
"T3Docs\\GuidesPhpDomain\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"T3Docs\\GuidesPhpDomain\\Tests\\": "tests/"
}
},
"config": {
"allow-plugins": {
"ergebnis/composer-normalize": true
Expand Down
27 changes: 27 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
beStrictAboutOutputDuringTests="true"
beStrictAboutChangesToGlobalState="true"
bootstrap="vendor/autoload.php"
colors="true"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnTestsThatTriggerNotices="true"
displayDetailsOnTestsThatTriggerWarnings="true"
displayDetailsOnTestsThatTriggerErrors="true"
failOnNotice="true"
failOnWarning="true"
>
<testsuites>
<testsuite name="unit">
<directory>tests/unit/</directory>
</testsuite>
</testsuites>
<coverage/>
<source>
<include>
<directory>src</directory>
</include>
</source>
</phpunit>
15 changes: 12 additions & 3 deletions tests/unit/PhpDomain/FullyQualifiedNameServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,34 @@

declare(strict_types=1);

namespace T3Docs\PhpDomain\PhpDomain;
namespace T3Docs\GuidesPhpDomain\Tests\PhpDomain;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use T3Docs\PhpDomain\Nodes\PhpNamespaceNode;
use T3Docs\GuidesPhpDomain\Nodes\PhpNamespaceNode;
use T3Docs\GuidesPhpDomain\PhpDomain\FullyQualifiedNameService;
use T3Docs\GuidesPhpDomain\PhpDomain\NamespaceRepository;

class FullyQualifiedNameServiceTest extends TestCase
final class FullyQualifiedNameServiceTest extends TestCase
{
private NamespaceRepository&MockObject $namespaceRepository;
private FullyQualifiedNameService $fullyQualifiedNameService;

protected function setUp(): void
{
$this->namespaceRepository = $this->createMock(NamespaceRepository::class);
$this->fullyQualifiedNameService = new FullyQualifiedNameService($this->namespaceRepository);
}

#[DataProvider('validFqnProvider')]
public function testValidClassNames(string $expectedName, string|null $expectedNamespace, string $fqn): void
{
$result = $this->fullyQualifiedNameService->getFullyQualifiedName($fqn);
self::assertEquals($expectedName, $result->getName());
self::assertEquals($expectedNamespace, $result->getNamespaceNode()?->getName());
}

/**
* @return array<int, mixed>
*/
Expand All @@ -38,6 +43,7 @@ public static function validFqnProvider(): array
['AnotherClassName', null, 'AnotherClassName'],
];
}

#[DataProvider('validFqnProviderWithCurrentNamespace')]
public function testValidClassNameWithCurrentNamespace(string $expectedName, string|null $expectedNamespace, string $fqn, string $currentNamespace): void
{
Expand All @@ -46,6 +52,7 @@ public function testValidClassNameWithCurrentNamespace(string $expectedName, str
self::assertEquals($expectedName, $result->getName());
self::assertEquals($expectedNamespace, $result->getNamespaceNode()?->getName());
}

/**
* @return array<int, mixed>
*/
Expand All @@ -59,12 +66,14 @@ public static function validFqnProviderWithCurrentNamespace(): array
['AnotherClassName', 'Another\\Namespace', 'AnotherClassName', 'Another\\Namespace'],
];
}

#[DataProvider('inValidFqnProvider')]
public function testInValidClassNames(string $fqn): void
{
$this->expectException(\Exception::class);
$result = $this->fullyQualifiedNameService->getFullyQualifiedName($fqn);
}

/**
* @return array<int, mixed>
*/
Expand Down