Skip to content

Commit a981663

Browse files
committed
Add tests
1 parent 1621f5c commit a981663

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

phpunit.xml.dist

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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/9.3/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
backupGlobals="false"
6+
backupStaticAttributes="false"
7+
colors="true"
8+
verbose="true"
9+
convertErrorsToExceptions="true"
10+
convertNoticesToExceptions="true"
11+
convertWarningsToExceptions="true"
12+
processIsolation="false"
13+
stopOnFailure="false"
14+
>
15+
<coverage>
16+
<include>
17+
<directory suffix=".php">src/</directory>
18+
</include>
19+
</coverage>
20+
<testsuites>
21+
<testsuite name="Enum Test Suite">
22+
<directory>tests</directory>
23+
</testsuite>
24+
</testsuites>
25+
</phpunit>

tests/EnumerationTest.php

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DeSmart\Enum\Tests;
6+
7+
use DeSmart\Enum\Enumeration;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class EnumerationTest extends TestCase
11+
{
12+
/** @test */
13+
public function it_should_create_enum(): void
14+
{
15+
$this->assertInstanceOf(Character::class, Character::good());
16+
$this->assertEquals('good', Character::good()->getValue());
17+
$this->assertEquals('good', Character::GOOD()->getValue());
18+
$this->assertEquals('good', Character::fromName('good')->getValue());
19+
$this->assertEquals('good', Character::fromValue('good')->getValue());
20+
$this->assertEquals('evil', Character::evil()->getValue());
21+
$this->assertEquals('evil', Character::EVIL()->getValue());
22+
$this->assertEquals('evil', Character::fromName('evil')->getValue());
23+
$this->assertEquals('evil', Character::fromValue('evil')->getValue());
24+
$this->assertEquals('sometimes_good_or_evil', Character::sometimesGoodSometimesEvil()->getValue());
25+
$this->assertEquals('sometimes_good_or_evil', Character::SOMETIMES_GOOD_SOMETIMES_EVIL()->getValue());
26+
$this->assertEquals('sometimes_good_or_evil', Character::sometimes_good_sometimes_evil()->getValue());
27+
$this->assertEquals('sometimes_good_or_evil', Character::fromName('sometimes_good_sometimes_evil')->getValue());
28+
$this->assertEquals('sometimes_good_or_evil', Character::fromValue('sometimes_good_or_evil')->getValue());
29+
$this->assertTrue(Character::good()->equals(Character::good()));
30+
$this->assertTrue(Character::good()->equals(Character::fromName('good')));
31+
$this->assertTrue(Character::good()->equals(Character::fromValue('good')));
32+
}
33+
34+
/** @test */
35+
public function it_should_throw_exception_when_unknown_const_name(): void
36+
{
37+
$this->expectException(\BadMethodCallException::class);
38+
$this->expectErrorMessage('Constant DeSmart\Enum\Tests\Character::NON_EXISTING does not exist.');
39+
40+
Character::fromName('non_existing');
41+
}
42+
43+
/** @test */
44+
public function it_should_throw_exception_when_unknown_const_value(): void
45+
{
46+
$this->expectException(\UnexpectedValueException::class);
47+
$this->expectErrorMessage('Constant with value non_existing in enum class DeSmart\Enum\Tests\Character does not exist.');
48+
49+
Character::fromValue('non_existing');
50+
}
51+
52+
/** @test */
53+
public function it_should_throw_error_when_value_type_is_disallowed(): void
54+
{
55+
$this->expectException(\TypeError::class);
56+
$this->expectErrorMessage('Enum value can only be integer or string.');
57+
58+
Character::fromValue(['non_existing']);
59+
}
60+
}
61+
62+
/**
63+
* @method static Character good()
64+
* @method static Character evil()
65+
* @method static Character sometimesGoodSometimesEvil()
66+
*/
67+
class Character extends Enumeration
68+
{
69+
const GOOD = 'good';
70+
const EVIL = 'evil';
71+
const SOMETIMES_GOOD_SOMETIMES_EVIL = 'sometimes_good_or_evil';
72+
}

0 commit comments

Comments
 (0)