Skip to content

Commit ecff8ec

Browse files
committed
Added PhpUnit tests
1 parent 0f5b334 commit ecff8ec

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@
44
# Composer
55
vendor/
66
composer.lock
7+
8+
# PHPUnit
9+
.phpunit.result.cache
10+
tests/coverage/

composer.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,24 @@
1717
"trait",
1818
"php"
1919
],
20+
"scripts": {
21+
"test": "phpunit",
22+
"test-coverage": "phpunit --coverage-html tests/coverage"
23+
},
2024
"require": {
2125
"php": ">=8.0"
2226
},
27+
"require-dev": {
28+
"phpunit/phpunit": ">=9.0"
29+
},
2330
"autoload": {
2431
"psr-4": {
2532
"Okapi\\Singleton\\": "src/"
2633
}
34+
},
35+
"autoload-dev": {
36+
"psr-4": {
37+
"Okapi\\Singleton\\Tests\\": "tests/"
38+
}
2739
}
2840
}

phpunit.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
5+
colors="true"
6+
>
7+
<testsuites>
8+
<testsuite name="Tests">
9+
<directory>tests/</directory>
10+
</testsuite>
11+
</testsuites>
12+
13+
<coverage>
14+
<include>
15+
<directory suffix=".php">src/</directory>
16+
</include>
17+
</coverage>
18+
</phpunit>

tests/SingletonTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Okapi\Singleton\Tests;
4+
5+
use Okapi\Singleton\Exceptions\AlreadyInitializedException;
6+
use Okapi\Singleton\Exceptions\NotInitializedException;
7+
use Okapi\Singleton\Tests\Stub\GovernmentOfUSA;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class SingletonTest extends TestCase
11+
{
12+
public function testEnsureAlreadyInitializedException(): void
13+
{
14+
$this->expectException(NotInitializedException::class);
15+
16+
GovernmentOfUSA::takeOverTheWorld();
17+
}
18+
19+
public function testIsInitialized(): void
20+
{
21+
$initialized = GovernmentOfUSA::isInitialized();
22+
$this->assertFalse($initialized);
23+
24+
$government = GovernmentOfUSA::getInstance();
25+
$this->assertInstanceOf(GovernmentOfUSA::class, $government);
26+
27+
// Should be false, because the instance should initialize itself
28+
$initialized = GovernmentOfUSA::isInitialized();
29+
$this->assertFalse($initialized);
30+
31+
GovernmentOfUSA::register();
32+
$initialized = GovernmentOfUSA::isInitialized();
33+
$this->assertTrue($initialized);
34+
}
35+
36+
public function testEnsureNotAlreadyInitialized(): void
37+
{
38+
$this->expectException(AlreadyInitializedException::class);
39+
40+
GovernmentOfUSA::register();
41+
}
42+
43+
public function testEnsureAlreadyInitialized(): void
44+
{
45+
GovernmentOfUSA::takeOverTheWorld();
46+
47+
$this->expectNotToPerformAssertions();
48+
}
49+
}

tests/Stub/GovernmentOfUSA.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Okapi\Singleton\Tests\Stub;
4+
5+
use Okapi\Singleton\Singleton;
6+
7+
class GovernmentOfUSA
8+
{
9+
use Singleton;
10+
11+
public static function register(): void
12+
{
13+
$instance = self::getInstance();
14+
15+
$instance->ensureNotAlreadyInitialized();
16+
17+
$instance->setInitialized();
18+
}
19+
20+
public static function takeOverTheWorld(): void
21+
{
22+
$instance = self::getInstance();
23+
24+
$instance->ensureAlreadyInitialized();
25+
}
26+
}

0 commit comments

Comments
 (0)