Skip to content

Commit b9ef7da

Browse files
committed
add more tests
1 parent bf8a5bf commit b9ef7da

6 files changed

+84
-4
lines changed

tests/Common/SampleClass.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace MiladRahimi\PhpRouter\Tests\Common;
4+
5+
class SampleClass implements SampleInterface
6+
{
7+
//
8+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace MiladRahimi\PhpRouter\Tests\Common;
4+
5+
class SampleConstructorController
6+
{
7+
/**
8+
* @var SampleInterface
9+
*/
10+
public $sample;
11+
12+
public function __construct(SampleInterface $sample)
13+
{
14+
$this->sample = $sample;
15+
}
16+
17+
public function getSampleClassName(): string
18+
{
19+
return get_class($this->sample);
20+
}
21+
}

tests/Common/SampleInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace MiladRahimi\PhpRouter\Tests\Common;
4+
5+
interface SampleInterface
6+
{
7+
//
8+
}

tests/ContainerTest.php

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
namespace MiladRahimi\PhpRouter\Tests;
44

55
use MiladRahimi\PhpContainer\Container;
6+
use MiladRahimi\PhpRouter\Tests\Common\SampleClass;
7+
use MiladRahimi\PhpRouter\Tests\Common\SampleConstructorController;
8+
use MiladRahimi\PhpRouter\Tests\Common\SampleInterface;
69
use Throwable;
710

811
class ContainerTest extends TestCase
@@ -15,15 +18,53 @@ public function test_binding_and_resolving_with_container()
1518
$router = $this->router();
1619

1720
$container = $router->getContainer();
18-
$container->singleton('name', 'Pink Floyd');
21+
$container->singleton(SampleInterface::class, SampleClass::class);
1922
$router->setContainer($container);
2023

2124
$router->get('/', function (Container $container) {
22-
return $container->get('name');
25+
return get_class($container->get(SampleInterface::class));
2326
});
2427

2528
$router->dispatch();
2629

27-
$this->assertEquals('Pink Floyd', $this->output($router));
30+
$this->assertEquals(SampleClass::class, $this->output($router));
31+
}
32+
33+
/**
34+
* @throws Throwable
35+
*/
36+
public function test_binding_and_resolving_with_controller_method()
37+
{
38+
$router = $this->router();
39+
40+
$container = $router->getContainer();
41+
$container->singleton(SampleInterface::class, SampleClass::class);
42+
$router->setContainer($container);
43+
44+
$router->get('/', function (SampleInterface $sample) {
45+
return get_class($sample);
46+
});
47+
48+
$router->dispatch();
49+
50+
$this->assertEquals(SampleClass::class, $this->output($router));
51+
}
52+
53+
/**
54+
* @throws Throwable
55+
*/
56+
public function test_binding_and_resolving_with_controller_constructor()
57+
{
58+
$router = $this->router();
59+
60+
$container = $router->getContainer();
61+
$container->singleton(SampleInterface::class, SampleClass::class);
62+
$router->setContainer($container);
63+
64+
$router->get('/', [SampleConstructorController::class, 'getSampleClassName']);
65+
66+
$router->dispatch();
67+
68+
$this->assertEquals(SampleClass::class, $this->output($router));
2869
}
2970
}

tests/HttpMethodTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace MiladRahimi\PhpRouter\Tests;
44

55
use Laminas\Diactoros\ServerRequest;
6-
use MiladRahimi\PhpRouter\Exceptions\RouteNotFoundException;
76
use MiladRahimi\PhpRouter\Tests\Common\SampleController;
87
use Throwable;
98

tests/RouteTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ public function test_current_route_for_a_route_with_all_attributes()
4949
$this->assertEquals(json_encode($expected), $this->output($router));
5050
}
5151

52+
/**
53+
* @throws Throwable
54+
*/
5255
public function test_lately_added_attributes_of_route()
5356
{
5457
$this->mockRequest('POST', 'http://shop.com/admin/profile/666');

0 commit comments

Comments
 (0)