1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace League\Tactician\Bundle\Tests\Integration; |
5
|
|
|
|
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
7
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
8
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
9
|
|
|
use Symfony\Component\Yaml\Yaml; |
10
|
|
|
|
11
|
|
|
abstract class IntegrationTest extends KernelTestCase |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var Kernel |
15
|
|
|
*/ |
16
|
|
|
protected static $kernel; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Filesystem |
20
|
|
|
*/ |
21
|
|
|
private $filesystem; |
22
|
|
|
|
23
|
|
|
protected static function createKernel(array $options = array()) |
24
|
|
|
{ |
25
|
|
|
require_once __DIR__.'/../testapp/AppKernel.php'; |
26
|
|
|
|
27
|
|
|
return new \AppKernel('test', true); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
protected function setUp() |
31
|
|
|
{ |
32
|
|
|
static::$kernel = static::createKernel(); |
33
|
|
|
$this->filesystem = new Filesystem(); |
34
|
|
|
|
35
|
|
|
$cacheDir = sys_get_temp_dir().DIRECTORY_SEPARATOR.'tactician-bundle'.DIRECTORY_SEPARATOR.uniqid("tactician-bundle", true); |
36
|
|
|
|
37
|
|
|
$this->filesystem->mkdir($cacheDir); |
38
|
|
|
static::$kernel->defineCacheDir($cacheDir); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function tearDown() |
42
|
|
|
{ |
43
|
|
|
$this->filesystem->remove( |
44
|
|
|
static::$kernel->getCacheDir() |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function givenConfig($namespace, $config) |
49
|
|
|
{ |
50
|
|
|
static::$kernel->loadConfig($namespace, Yaml::parse((string) $config)); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function registerService($serviceId, $className, array $tags) |
54
|
|
|
{ |
55
|
|
|
static::$kernel->addServiceToRegister($serviceId, $className, $tags); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function handleCommand($busId, $commandClass, array $args) |
59
|
|
|
{ |
60
|
|
|
$class = new \ReflectionClass($commandClass); |
61
|
|
|
$command = $class->newInstanceArgs($args); |
62
|
|
|
|
63
|
|
|
static::$kernel->boot(); |
64
|
|
|
static::$kernel->getContainer()->get('tactician.commandbus.'.$busId)->handle($command); |
65
|
|
|
} |
66
|
|
|
} |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: