-
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathCallableFactoryTest.php
More file actions
78 lines (65 loc) · 2.18 KB
/
CallableFactoryTest.php
File metadata and controls
78 lines (65 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
declare(strict_types=1);
namespace Yiisoft\Queue\Tests\Unit\Middleware;
use PHPUnit\Framework\TestCase;
use Yiisoft\Queue\Middleware\CallableFactory;
use Yiisoft\Queue\Middleware\InvalidCallableConfigurationException;
use Yiisoft\Test\Support\Container\SimpleContainer;
final class CallableFactoryTest extends TestCase
{
public function testCreateFromContainerStringInvokable(): void
{
$invokable = new class () {
public function __invoke(): string
{
return 'ok';
}
};
$container = new SimpleContainer([
'invokable' => $invokable,
]);
$factory = new CallableFactory($container);
$callable = $factory->create('invokable');
self::assertIsCallable($callable);
self::assertSame('ok', $callable());
}
public function testCreateFromStaticMethodArray(): void
{
$class = new class () {
public static function ping(): string
{
return 'pong';
}
};
$className = $class::class;
$container = new SimpleContainer();
$factory = new CallableFactory($container);
$callable = $factory->create([$className, 'ping']);
self::assertIsCallable($callable);
self::assertSame('pong', $callable());
}
public function testCreateFromContainerObjectMethod(): void
{
$service = new class () {
public function go(): string
{
return 'ok';
}
};
$className = $service::class;
$container = new SimpleContainer([
$className => $service,
]);
$factory = new CallableFactory($container);
$callable = $factory->create([$className, 'go']);
self::assertIsCallable($callable);
self::assertSame('ok', $callable());
}
public function testFriendlyException(): void
{
$e = new InvalidCallableConfigurationException();
self::assertSame('Invalid callable configuration.', $e->getName());
self::assertNotNull($e->getSolution());
self::assertStringContainsString('callable', (string)$e->getSolution());
}
}