Skip to content

Commit c89f646

Browse files
committed
increate test coverage
1 parent 57e1012 commit c89f646

File tree

10 files changed

+83
-51
lines changed

10 files changed

+83
-51
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
/.idea
22
/vendor
33
/composer.lock
4+
/build
5+
.DS_Store

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</testsuites>
1313
<filter>
1414
<whitelist processUncoveredFilesFromWhitelist="true">
15-
<directory suffix=".php">./tests</directory>
15+
<directory suffix=".php">./src</directory>
1616
</whitelist>
1717
</filter>
1818
<logging>

src/Route.php

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,6 @@ public function getName(): ?string
112112
return $this->name;
113113
}
114114

115-
/**
116-
* @param string|null $name
117-
*/
118-
public function setName(?string $name): void
119-
{
120-
$this->name = $name;
121-
}
122-
123115
/**
124116
* @return string
125117
*/
@@ -128,14 +120,6 @@ public function getUri(): string
128120
return $this->uri;
129121
}
130122

131-
/**
132-
* @param string $uri
133-
*/
134-
public function setUri(string $uri): void
135-
{
136-
$this->uri = $uri;
137-
}
138-
139123
/**
140124
* @return string|null
141125
*/
@@ -144,14 +128,6 @@ public function getMethod(): ?string
144128
return $this->method;
145129
}
146130

147-
/**
148-
* @param string|null $method
149-
*/
150-
public function setMethod(?string $method): void
151-
{
152-
$this->method = $method;
153-
}
154-
155131
/**
156132
* @return callable|string
157133
*/
@@ -160,14 +136,6 @@ public function getController()
160136
return $this->controller;
161137
}
162138

163-
/**
164-
* @param callable|string $controller
165-
*/
166-
public function setController($controller): void
167-
{
168-
$this->controller = $controller;
169-
}
170-
171139
/**
172140
* @return callable[]|string[]
173141
*/
@@ -176,27 +144,11 @@ public function getMiddleware()
176144
return $this->middleware;
177145
}
178146

179-
/**
180-
* @param callable[]|string[] $middleware
181-
*/
182-
public function setMiddleware($middleware): void
183-
{
184-
$this->middleware = $middleware;
185-
}
186-
187147
/**
188148
* @return string|null
189149
*/
190150
public function getDomain(): ?string
191151
{
192152
return $this->domain;
193153
}
194-
195-
/**
196-
* @param string|null $domain
197-
*/
198-
public function setDomain(?string $domain): void
199-
{
200-
$this->domain = $domain;
201-
}
202154
}

src/Services/HttpPublisher.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* StreamPublisher publishes the given resp
1010
*
1111
* @package MiladRahimi\PhpRouter\Services
12+
* @codeCoverageIgnore
1213
*/
1314
class HttpPublisher implements PublisherInterface
1415
{
@@ -26,7 +27,7 @@ public function publish($content): void
2627

2728
foreach ($content->getHeaders() as $name => $values) {
2829
$value = $content->getHeaderLine($name);
29-
header($name.': '.$value);
30+
header($name . ': ' . $value);
3031
}
3132

3233
fwrite($output, $content->getBody());

test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./vendor/bin/phpunit --coverage-text --coverage-html build/html ./tests

tests/Classes/Publisher.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* Class Publisher
1010
*
1111
* @package MiladRahimi\PhpRouter\Tests\Classes
12+
* @codeCoverageIgnore
1213
*/
1314
class Publisher implements PublisherInterface
1415
{

tests/Classes/SampleController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Class SampleController
77
*
88
* @package Classes
9+
* @codeCoverageIgnore
910
*/
1011
class SampleController
1112
{

tests/Classes/SampleMiddleware.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* Class SampleMiddleware
1111
*
1212
* @package MiladRahimi\PhpRouter\Tests\Classes
13+
* @codeCoverageIgnore
1314
*/
1415
class SampleMiddleware implements Middleware
1516
{

tests/Classes/StopperMiddleware.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* Class StopperMiddleware
1212
*
1313
* @package MiladRahimi\PhpRouter\Tests\Classes
14+
* @codeCoverageIgnore
1415
*/
1516
class StopperMiddleware implements Middleware
1617
{

tests/RoutingTest.php

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,55 @@ public function test_injection_of_router_by_type()
365365
$this->assertEquals('home', $this->extract($router));
366366
}
367367

368+
/**
369+
* @throws Throwable
370+
*/
371+
public function test_injection_of_default_value()
372+
{
373+
$router = $this->router()
374+
->get('/', function ($default = "Default") {
375+
return $default;
376+
})
377+
->dispatch();
378+
379+
$this->assertEquals('Default', $this->extract($router));
380+
}
381+
382+
/**
383+
* @throws Throwable
384+
*/
385+
public function test_set_and_get_request()
386+
{
387+
$router = $this->router();
388+
389+
$router->get('/', function () use ($router) {
390+
$newRequest = $router->getRequest()->withMethod('CUSTOM');
391+
$router->setRequest($newRequest);
392+
393+
return $router->getRequest()->getMethod();
394+
})->dispatch();
395+
396+
$this->assertEquals('CUSTOM', $this->extract($router));
397+
}
398+
399+
/**
400+
* @throws Throwable
401+
*/
402+
public function test_default_publisher()
403+
{
404+
ob_start();
405+
406+
$router = new Router();
407+
408+
$router->get('/', function () {
409+
return 'home';
410+
})->dispatch();
411+
412+
$this->assertEquals('home', ob_get_contents());
413+
414+
ob_end_clean();
415+
}
416+
368417
/**
369418
* @throws Throwable
370419
*/
@@ -408,10 +457,33 @@ public function test_not_found_error()
408457
/**
409458
* @throws Throwable
410459
*/
411-
public function test_with_invalid_controller()
460+
public function test_with_class_method_but_invalid_controller_class()
412461
{
413462
$this->expectException(InvalidControllerException::class);
414463

415464
$this->router()->get('/', 'UnknownController@method')->dispatch();
416465
}
466+
467+
/**
468+
* @throws Throwable
469+
*/
470+
public function test_with_invalid_controller_class()
471+
{
472+
$this->expectException(InvalidControllerException::class);
473+
474+
$this->router()->get('/', 666)->dispatch();
475+
}
476+
477+
/**
478+
* @throws Throwable
479+
*/
480+
public function test_with_invalid_controller_method()
481+
{
482+
$this->expectException(InvalidControllerException::class);
483+
484+
$namespace = 'MiladRahimi\PhpRouter\Tests\Classes';
485+
$this->router('', $namespace)
486+
->get('/', 'SampleController@invalidMethod')
487+
->dispatch();
488+
}
417489
}

0 commit comments

Comments
 (0)