Skip to content

Commit 2f05cf1

Browse files
author
Marco Bunge
committed
Improve testing
1 parent 3e5b20c commit 2f05cf1

File tree

8 files changed

+187
-9
lines changed

8 files changed

+187
-9
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Hawkbit Changelog
22

3+
## 2.1.1
4+
5+
- Add tests for controller constructor injection
6+
37
## 2.1.0
48

59
### Fixed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,8 @@ $app->get('/', 'HomeController::index'); // calls index method on HomeController
267267
$app->run();
268268
```
269269

270+
*Please use boot method in Service Providers for correct injection of services into controller!*
271+
270272
```php
271273
<?php
272274

composer.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
}
2626
],
2727
"require": {
28-
"php": ">=5.5",
29-
"filp/whoops": "~2.0",
30-
"league/route": "dev-master#507606b53d3935e7830aa7c48c43337bc2b1b2ba",
31-
"league/container": "~2.0",
32-
"league/event": "~2.1",
33-
"monolog/monolog": "~1.12",
28+
"php": ">=5.5.9",
29+
"filp/whoops": "^2.1",
30+
"league/route": "dev-master#507606b53d3935e7830aa7c48c43337bc2b1b2ba@dev",
31+
"league/container": "^2.2",
32+
"league/event": "^2.1",
33+
"monolog/monolog": "^1.22",
3434
"zendframework/zend-config": "^2.6",
35-
"zendframework/zend-diactoros": "~1.3"
35+
"zendframework/zend-diactoros": "^1.3"
3636
},
3737
"require-dev": {
3838
"phpunit/phpunit": "~4.8",

tests/ServiceProviderTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: marco.bunge
5+
* Date: 02.01.2017
6+
* Time: 09:01
7+
*/
8+
9+
namespace Hawkbit\Tests;
10+
11+
12+
13+
use Hawkbit\Application;
14+
use Hawkbit\Tests\TestAsset\SingletonDummy;
15+
use Hawkbit\Tests\TestAsset\InjectableController;
16+
use Hawkbit\Tests\TestAsset\TestServiceProvider;
17+
use Zend\Diactoros\ServerRequestFactory;
18+
19+
class ServiceProviderTest extends \PHPUnit_Framework_TestCase
20+
{
21+
22+
23+
public function testGetSingleton()
24+
{
25+
$app = new Application();
26+
$app->register(TestServiceProvider::class);
27+
28+
/** @var SingletonDummy $dummy */
29+
$dummy = $app[SingletonDummy::class];
30+
31+
$this->assertInstanceOf(SingletonDummy::class, $dummy);
32+
$this->assertEquals('singleton', $dummy->getValue());
33+
}
34+
35+
public function testGetInjectedSingleton()
36+
{
37+
$app = new Application([
38+
Application::KEY_ERROR => true,
39+
Application::KEY_ERROR_CATCH => false,
40+
]);
41+
42+
$app->register(TestServiceProvider::class);
43+
44+
$app->get('/', [InjectableController::class, 'getIndex']);
45+
46+
$response = $app->handle(ServerRequestFactory::fromGlobals());
47+
48+
$this->assertEquals('singleton', $response->getBody()->__toString());
49+
50+
51+
}
52+
}

tests/TestAsset/Dummy.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: marco.bunge
5+
* Date: 02.01.2017
6+
* Time: 09:02
7+
*/
8+
9+
namespace Hawkbit\Tests\TestAsset;
10+
11+
12+
class sDummy
13+
{
14+
private $value;
15+
16+
/**
17+
* Dummy constructor.
18+
* @param $value
19+
*/
20+
public function __construct($value)
21+
{
22+
$this->value = $value;
23+
}
24+
25+
/**
26+
* @return mixed
27+
*/
28+
public function getValue()
29+
{
30+
return $this->value;
31+
}
32+
33+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* The Turbine Micro Framework. An advanced derivate of Proton Micro Framework
4+
*
5+
* @author Marco Bunge <marco_bunge@web.de>
6+
* @author Alex Bilbie <hello@alexbilbie.com>
7+
* @copyright Marco Bunge <marco_bunge@web.de>
8+
*
9+
* @license MIT
10+
*/
11+
12+
namespace Hawkbit\Tests\TestAsset;
13+
14+
15+
use Psr\Http\Message\ResponseInterface;
16+
use Psr\Http\Message\ServerRequestInterface;
17+
18+
/**
19+
* @codeCoverageIgnore
20+
*/
21+
class InjectableController
22+
{
23+
/**
24+
* @var SingletonDummy
25+
*/
26+
private $dummy;
27+
28+
/**
29+
* TestInjectableController constructor.
30+
* @param SingletonDummy $dummy
31+
*/
32+
public function __construct(SingletonDummy $dummy)
33+
{
34+
$this->dummy = $dummy;
35+
}
36+
37+
public function getIndex(ServerRequestInterface $request, ResponseInterface $response, array $args = [])
38+
{
39+
$response->getBody()->write($this->dummy->getValue());
40+
return $response;
41+
}
42+
}

tests/TestAsset/SingletonDummy.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: marco.bunge
5+
* Date: 02.01.2017
6+
* Time: 09:02
7+
*/
8+
9+
namespace Hawkbit\Tests\TestAsset;
10+
11+
12+
class SingletonDummy
13+
{
14+
private $value;
15+
16+
/**
17+
* Dummy constructor.
18+
* @param $value
19+
*/
20+
public function __construct($value)
21+
{
22+
$this->value = $value;
23+
}
24+
25+
/**
26+
* @return mixed
27+
*/
28+
public function getValue()
29+
{
30+
return $this->value;
31+
}
32+
33+
}

tests/TestAsset/TestServiceProvider.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313

1414

1515
use League\Container\ServiceProvider\AbstractServiceProvider;
16+
use League\Container\ServiceProvider\BootableServiceProviderInterface;
1617

17-
class TestServiceProvider extends AbstractServiceProvider
18+
class TestServiceProvider extends AbstractServiceProvider implements BootableServiceProviderInterface
1819
{
1920

20-
protected $provides= [
21+
protected $provides = [
2122
'TestService'
2223
];
2324

@@ -32,4 +33,15 @@ public function register()
3233
{
3334
$this->getContainer()->add('TestService', new \stdClass);
3435
}
36+
37+
/**
38+
* Method will be invoked on registration of a service provider implementing
39+
* this interface. Provides ability for eager loading of Service Providers.
40+
*
41+
* @return void
42+
*/
43+
public function boot()
44+
{
45+
$this->getContainer()->share(SingletonDummy::class, new SingletonDummy('singleton'));
46+
}
3547
}

0 commit comments

Comments
 (0)