Skip to content

Commit ae8e17e

Browse files
committed
add tests
1 parent f1acb35 commit ae8e17e

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

src/Dispatching/Caller.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,16 @@ public function call($callable)
6767
throw new InvalidCallableException('Invalid callable: ' . implode(',', $callable));
6868
}
6969

70-
list($class, $method) = $callable;
70+
[$class, $method] = $callable;
7171

7272
if (class_exists($class) == false) {
73-
throw new InvalidCallableException("Class `$callable` not found.");
73+
throw new InvalidCallableException("Class `$class` not found.");
7474
}
7575

7676
$object = $this->container->instantiate($class);
7777

7878
if (method_exists($object, $method) == false) {
79-
throw new InvalidCallableException("Method `$class::$method` not found.");
79+
throw new InvalidCallableException("Method `$class::$method` is not declared.");
8080
}
8181

8282
$callable = [$object, $method];
@@ -89,11 +89,11 @@ public function call($callable)
8989
}
9090
}
9191

92-
if (is_object($callable) && !($callable instanceof Closure)) {
92+
if (is_object($callable) && !$callable instanceof Closure) {
9393
if (method_exists($callable, 'handle')) {
9494
$callable = [$callable, 'handle'];
9595
} else {
96-
throw new InvalidCallableException("Method `$callable::handle` not found.");
96+
throw new InvalidCallableException("Method `handle` is not declared.");
9797
}
9898
}
9999
}

tests/ControllerTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,46 @@ public function test_with_an_invalid_array_as_controller_it_should_fail()
4343
$router->get('/', ['invalid', 'array', 'controller']);
4444

4545
$this->expectException(InvalidCallableException::class);
46+
$this->expectExceptionMessage('Invalid callable: invalid,array,controller');
47+
$router->dispatch();
48+
}
49+
50+
/**
51+
* @throws Throwable
52+
*/
53+
public function test_with_an_invalid_class_as_controller_it_should_fail()
54+
{
55+
$router = $this->router();
56+
$router->get('/', ['InvalidController', 'show']);
57+
58+
$this->expectException(InvalidCallableException::class);
59+
$this->expectExceptionMessage('Class `InvalidController` not found.');
60+
$router->dispatch();
61+
}
62+
63+
/**
64+
* @throws Throwable
65+
*/
66+
public function test_with_an_int_as_controller_it_should_fail()
67+
{
68+
$router = $this->router();
69+
$router->get('/', 666);
70+
71+
$this->expectException(InvalidCallableException::class);
72+
$this->expectExceptionMessage('Invalid callable.');
73+
$router->dispatch();
74+
}
75+
76+
/**
77+
* @throws Throwable
78+
*/
79+
public function test_with_an_handle_less_class_as_controller_it_should_fail()
80+
{
81+
$router = $this->router();
82+
$router->get('/', SampleController::class);
83+
84+
$this->expectException(InvalidCallableException::class);
85+
$this->expectExceptionMessage('Method `handle` is not declared.');
4686
$router->dispatch();
4787
}
4888

@@ -55,6 +95,7 @@ public function test_with_an_invalid_method_as_controller_it_should_fail()
5595
$router->get('/', [SampleController::class, 'invalid']);
5696

5797
$this->expectException(InvalidCallableException::class);
98+
$this->expectExceptionMessage('Method `' . SampleController::class . '::invalid` is not declared.');
5899
$router->dispatch();
59100
}
60101

0 commit comments

Comments
 (0)