Skip to content

Commit 17e5ccc

Browse files
committed
add tests
1 parent 1cdc02f commit 17e5ccc

File tree

5 files changed

+153
-25
lines changed

5 files changed

+153
-25
lines changed

tests/DemoClassTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace skrtdev\PrototypesTests;
4+
5+
use skrtdev\Prototypes\Prototypeable;
6+
7+
class DemoClassTest {
8+
use Prototypeable;
9+
10+
protected static bool $static_property = true;
11+
protected bool $property = true;
12+
13+
public function existentMethod(): void
14+
{
15+
16+
}
17+
18+
public static function existentStaticMethod(): void
19+
{
20+
21+
}
22+
}

tests/MainTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace skrtdev\PrototypesTests;
4+
5+
use skrtdev\Prototypes\Exception;
6+
use skrtdev\Prototypes\Prototypes;
7+
use PHPUnit\Framework\TestCase;
8+
use stdClass;
9+
10+
class MainTest extends TestCase
11+
{
12+
public function testNonExistentClass(): void
13+
{
14+
$this->expectException(Exception::class);
15+
Prototypes::addClassMethod('RandomClass', 'RandomMethod', fn() => true);
16+
}
17+
18+
public function testNonPrototypeableClass(): void
19+
{
20+
$this->expectException(Exception::class);
21+
Prototypes::addClassMethod(stdClass::class, 'RandomMethod', fn() => true);
22+
}
23+
24+
public function testMainClassCannotBeInstantiated(): void
25+
{
26+
$this->expectException(Exception::class);
27+
$_ = new Prototypes();
28+
}
29+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace skrtdev\PrototypesTests;
4+
5+
use Error;
6+
use skrtdev\Prototypes\Exception;
7+
use PHPUnit\Framework\TestCase;
8+
use skrtdev\Prototypes\Prototypes;
9+
10+
class PrototypesIsPrototypeableTest extends TestCase
11+
{
12+
13+
public function testPrototypeCanBeCreated(): void
14+
{
15+
$this->assertNull(Prototypes::addStaticMethod('prototypeStaticMethod', fn() => true));
16+
}
17+
18+
public function testPrototypeCanBeCalled(): void
19+
{
20+
$this->assertTrue(Prototypes::prototypeStaticMethod());
21+
}
22+
23+
public function testErrorIsThrownInNonExistentMethods(): void
24+
{
25+
$this->expectException(Error::class);
26+
Prototypes::nonExistentStaticMethod();
27+
}
28+
29+
public function testCantOverridePrototypes(): void
30+
{
31+
$this->expectException(Exception::class);
32+
Prototypes::addStaticMethod('prototypeStaticMethod', fn() => true);
33+
}
34+
35+
public function testCantOverrideMethods(): void
36+
{
37+
$this->expectException(Exception::class);
38+
// isPrototypeable is an existent static method
39+
Prototypes::addStaticMethod('isPrototypeable', fn() => true);
40+
}
41+
42+
public function testCantAddNonStaticMethods(): void
43+
{
44+
$this->expectException(Exception::class);
45+
// Prototypes class is not intended to have non-static methods
46+
Prototypes::addMethod('justARandomMethod', fn() => true);
47+
}
48+
49+
}

tests/PrototypesTest.php

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,40 @@
11
<?php
22

3-
namespace skrtdev\Prototypes;
3+
namespace skrtdev\PrototypesTests;
44

5+
use Error;
56
use PHPUnit\Framework\TestCase;
7+
use skrtdev\Prototypes\Exception;
68

7-
class DemoClass {
8-
use Prototypeable;
9-
10-
public function existentMethod(): void
11-
{
12-
13-
}
14-
}
159

1610
class PrototypesTest extends TestCase
1711
{
1812
public function testPrototypeCanBeCreated(): void
1913
{
20-
$this->assertNull(DemoClass::addMethod('prototypeMethod', fn() => true));
14+
$this->assertNull(DemoClassTest::addMethod('prototypeMethod', fn() => $this->property));
2115
}
2216

23-
public function testErrorIsThrownInNonExistentMethods(): void
17+
public function testPrototypeCanBeCalled(): void
2418
{
25-
$this->expectException(\Error::class);
26-
(new DemoClass)->nonExistentMethod();
19+
$this->assertTrue((new DemoClassTest)->prototypeMethod());
2720
}
2821

29-
public function testCantOverridePrototypes(): void
22+
public function testErrorIsThrownInNonExistentMethods(): void
3023
{
31-
$this->expectException(Exception::class);
32-
DemoClass::addMethod('prototypeMethod', fn() => true);
24+
$this->expectException(Error::class);
25+
(new DemoClassTest)->nonExistentMethod();
3326
}
3427

35-
public function testCantOverrideMethods(): void
28+
public function testCantOverridePrototypes(): void
3629
{
3730
$this->expectException(Exception::class);
38-
DemoClass::addMethod('existentMethod', fn() => true);
31+
DemoClassTest::addMethod('prototypeMethod', fn() => $this->property);
3932
}
4033

41-
public function testNonExistentClass(): void
34+
public function testCantOverrideMethods(): void
4235
{
4336
$this->expectException(Exception::class);
44-
Prototypes::addClassMethod('RandomClass', 'RandomMethod', fn() => true);
37+
DemoClassTest::addMethod('existentMethod', fn() => $this->property);
4538
}
4639

47-
public function testNonPrototypeableClass(): void
48-
{
49-
$this->expectException(Exception::class);
50-
Prototypes::addClassMethod(\stdClass::class, 'RandomMethod', fn() => true);
51-
}
5240
}

tests/StaticPrototypesTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace skrtdev\PrototypesTests;
4+
5+
use Error;
6+
use skrtdev\Prototypes\Exception;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class StaticPrototypesTest extends TestCase
10+
{
11+
12+
public function testPrototypeCanBeCreated(): void
13+
{
14+
$this->assertNull(DemoClassTest::addStaticMethod('prototypeStaticMethod', fn() => self::$static_property));
15+
}
16+
17+
public function testPrototypeCanBeCalled(): void
18+
{
19+
$this->assertTrue(DemoClassTest::prototypeStaticMethod());
20+
}
21+
22+
public function testErrorIsThrownInNonExistentMethods(): void
23+
{
24+
$this->expectException(Error::class);
25+
DemoClassTest::nonExistentStaticMethod();
26+
}
27+
28+
public function testCantOverridePrototypes(): void
29+
{
30+
$this->expectException(Exception::class);
31+
DemoClassTest::addStaticMethod('prototypeStaticMethod', fn() => self::$static_property);
32+
}
33+
34+
public function testCantOverrideMethods(): void
35+
{
36+
$this->expectException(Exception::class);
37+
DemoClassTest::addStaticMethod('existentStaticMethod', fn() => self::$static_property);
38+
}
39+
40+
}

0 commit comments

Comments
 (0)