Skip to content

Commit 716763a

Browse files
committed
Adding tests
1 parent 31e4df7 commit 716763a

File tree

8 files changed

+278
-4
lines changed

8 files changed

+278
-4
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ composer.lock
55
*.swp
66
*.swo
77
*~
8-
8+
/.phpunit.cache/

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,20 @@ Some ideas for future classes:
7070

7171
## Development
7272

73-
This project uses PHP CodeSniffer with PSR-12 standard for code quality.
73+
This project uses PHP CodeSniffer with PSR-12 standard for code quality and PHPUnit for testing.
7474

7575
```bash
76+
# Run tests
77+
composer test
78+
7679
# Check code style
7780
composer check
7881

7982
# Auto-fix code style issues
8083
composer fix
8184

8285
# Or use vendor binaries directly
86+
vendor/bin/phpunit
8387
vendor/bin/phpcs --standard=PSR12 src/
8488
vendor/bin/phpcbf --standard=PSR12 src/
8589
```

composer.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,23 @@
77
"php": "^8.1"
88
},
99
"require-dev": {
10-
"squizlabs/php_codesniffer": "^4.0"
10+
"squizlabs/php_codesniffer": "^4.0",
11+
"phpunit/phpunit": "^10.0"
1112
},
1213
"autoload": {
1314
"psr-4": {
1415
"Calvert\\DndErrors\\": "src/"
1516
}
1617
},
18+
"autoload-dev": {
19+
"psr-4": {
20+
"Calvert\\DndErrors\\Tests\\": "tests/"
21+
}
22+
},
1723
"scripts": {
1824
"check": "phpcs --standard=PSR12 src/",
19-
"fix": "phpcbf --standard=PSR12 src/"
25+
"fix": "phpcbf --standard=PSR12 src/",
26+
"test": "phpunit"
2027
},
2128
"minimum-stability": "stable"
2229
}

phpunit.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
cacheDirectory=".phpunit.cache"
7+
executionOrder="depends,defects"
8+
requireCoverageMetadata="false"
9+
beStrictAboutCoverageMetadata="true"
10+
beStrictAboutOutputDuringTests="true"
11+
failOnRisky="true"
12+
failOnWarning="true">
13+
<testsuites>
14+
<testsuite name="default">
15+
<directory>tests</directory>
16+
</testsuite>
17+
</testsuites>
18+
<source>
19+
<include>
20+
<directory suffix=".php">src</directory>
21+
</include>
22+
</source>
23+
</phpunit>
24+

tests/Archetype/PaladinTest.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace Calvert\DndErrors\Tests\Archetype;
4+
5+
use Calvert\DndErrors\Archetype\Paladin;
6+
use Calvert\DndErrors\ErrorContext;
7+
use PHPUnit\Framework\TestCase;
8+
9+
final class PaladinTest extends TestCase
10+
{
11+
private Paladin $paladin;
12+
13+
protected function setUp(): void
14+
{
15+
$this->paladin = new Paladin();
16+
}
17+
18+
public function testGetSlugReturnsPaladin(): void
19+
{
20+
$this->assertSame('paladin', $this->paladin->getSlug());
21+
}
22+
23+
public function testRenderContainsMessage(): void
24+
{
25+
$context = new ErrorContext(
26+
type: 'Error',
27+
message: 'Test error message',
28+
file: '/path/to/file.php',
29+
line: 42
30+
);
31+
32+
$output = $this->paladin->render($context);
33+
34+
$this->assertStringContainsString('Test error message', $output);
35+
$this->assertStringContainsString('/path/to/file.php:42', $output);
36+
}
37+
38+
public function testRenderContainsPaladinEmoji(): void
39+
{
40+
$context = new ErrorContext(
41+
type: 'Error',
42+
message: 'Test error',
43+
);
44+
45+
$output = $this->paladin->render($context);
46+
47+
// Should contain paladin emoji (✨ or ⚔️)
48+
$this->assertTrue(
49+
str_contains($output, '') || str_contains($output, '⚔️'),
50+
'Output should contain paladin emoji'
51+
);
52+
}
53+
54+
public function testRenderIncludesLocationWhenProvided(): void
55+
{
56+
$context = new ErrorContext(
57+
type: 'Error',
58+
message: 'Test error',
59+
file: 'test.php',
60+
line: 10
61+
);
62+
63+
$output = $this->paladin->render($context);
64+
65+
$this->assertStringContainsString('test.php:10', $output);
66+
}
67+
68+
public function testRenderShowsUnknownLocationWhenFileOrLineMissing(): void
69+
{
70+
$context = new ErrorContext(
71+
type: 'Error',
72+
message: 'Test error',
73+
);
74+
75+
$output = $this->paladin->render($context);
76+
77+
$this->assertStringContainsString('Unknown location', $output);
78+
}
79+
80+
public function testRenderForFatalError(): void
81+
{
82+
$context = new ErrorContext(
83+
type: 'fatal',
84+
message: 'Fatal error occurred',
85+
);
86+
87+
$output = $this->paladin->render($context);
88+
89+
// Fatal errors should have special headers
90+
$this->assertStringContainsString('Fatal error occurred', $output);
91+
}
92+
}
93+

tests/Archetype/RangerTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Calvert\DndErrors\Tests\Archetype;
4+
5+
use Calvert\DndErrors\Archetype\Ranger;
6+
use Calvert\DndErrors\ErrorContext;
7+
use PHPUnit\Framework\TestCase;
8+
9+
final class RangerTest extends TestCase
10+
{
11+
private Ranger $ranger;
12+
13+
protected function setUp(): void
14+
{
15+
$this->ranger = new Ranger();
16+
}
17+
18+
public function testGetSlugReturnsRanger(): void
19+
{
20+
$this->assertSame('ranger', $this->ranger->getSlug());
21+
}
22+
23+
public function testRenderContainsMessage(): void
24+
{
25+
$context = new ErrorContext(
26+
type: 'Error',
27+
message: 'Trail lost',
28+
file: '/forest/path.php',
29+
line: 15
30+
);
31+
32+
$output = $this->ranger->render($context);
33+
34+
$this->assertStringContainsString('Trail lost', $output);
35+
$this->assertStringContainsString('/forest/path.php:15', $output);
36+
}
37+
38+
public function testRenderContainsRangerEmoji(): void
39+
{
40+
$context = new ErrorContext(
41+
type: 'Error',
42+
message: 'Test error',
43+
);
44+
45+
$output = $this->ranger->render($context);
46+
47+
// Should contain ranger emoji 🏹
48+
$this->assertStringContainsString('🏹', $output);
49+
}
50+
}
51+

tests/Archetype/WizardTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Calvert\DndErrors\Tests\Archetype;
4+
5+
use Calvert\DndErrors\Archetype\Wizard;
6+
use Calvert\DndErrors\ErrorContext;
7+
use PHPUnit\Framework\TestCase;
8+
9+
final class WizardTest extends TestCase
10+
{
11+
private Wizard $wizard;
12+
13+
protected function setUp(): void
14+
{
15+
$this->wizard = new Wizard();
16+
}
17+
18+
public function testGetSlugReturnsWizard(): void
19+
{
20+
$this->assertSame('wizard', $this->wizard->getSlug());
21+
}
22+
23+
public function testRenderContainsMessage(): void
24+
{
25+
$context = new ErrorContext(
26+
type: 'Error',
27+
message: 'Arcane failure',
28+
file: '/spellbook/incantation.php',
29+
line: 7
30+
);
31+
32+
$output = $this->wizard->render($context);
33+
34+
$this->assertStringContainsString('Arcane failure', $output);
35+
$this->assertStringContainsString('/spellbook/incantation.php:7', $output);
36+
}
37+
38+
public function testRenderContainsWizardEmoji(): void
39+
{
40+
$context = new ErrorContext(
41+
type: 'Error',
42+
message: 'Test error',
43+
);
44+
45+
$output = $this->wizard->render($context);
46+
47+
// Should contain wizard emoji 🧙
48+
$this->assertStringContainsString('🧙', $output);
49+
}
50+
}
51+

tests/ErrorContextTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Calvert\DndErrors\Tests;
4+
5+
use Calvert\DndErrors\ErrorContext;
6+
use PHPUnit\Framework\TestCase;
7+
8+
final class ErrorContextTest extends TestCase
9+
{
10+
public function testCanCreateErrorContextWithAllProperties(): void
11+
{
12+
$context = new ErrorContext(
13+
type: 'RuntimeException',
14+
message: 'Test error message',
15+
file: '/path/to/file.php',
16+
line: 42,
17+
trace: array('trace' => 'data'),
18+
phpVersion: '8.1.0'
19+
);
20+
21+
$this->assertSame('RuntimeException', $context->type);
22+
$this->assertSame('Test error message', $context->message);
23+
$this->assertSame('/path/to/file.php', $context->file);
24+
$this->assertSame(42, $context->line);
25+
$this->assertSame(array('trace' => 'data'), $context->trace);
26+
$this->assertSame('8.1.0', $context->phpVersion);
27+
}
28+
29+
public function testCanCreateErrorContextWithMinimalProperties(): void
30+
{
31+
$context = new ErrorContext(
32+
type: 'Error',
33+
message: 'Test message'
34+
);
35+
36+
$this->assertSame('Error', $context->type);
37+
$this->assertSame('Test message', $context->message);
38+
$this->assertNull($context->file);
39+
$this->assertNull($context->line);
40+
$this->assertSame(array(), $context->trace);
41+
$this->assertNull($context->phpVersion);
42+
}
43+
}
44+

0 commit comments

Comments
 (0)