|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Spiral Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @author Anton Titov (Wolfy-J) |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Spiral\Tests\Framework\Interceptor; |
|
13
|
|
|
|
|
14
|
|
|
use Cycle\ORM\Transaction; |
|
15
|
|
|
use Spiral\Core\CoreInterface; |
|
16
|
|
|
use Spiral\Core\Exception\ControllerException; |
|
17
|
|
|
use Spiral\App\Controller\DemoController; |
|
18
|
|
|
use Spiral\App\User\Role; |
|
19
|
|
|
use Spiral\App\User\User; |
|
20
|
|
|
use Spiral\Tests\Framework\ConsoleTest; |
|
21
|
|
|
|
|
22
|
|
|
class CycleInterceptorTest extends ConsoleTest |
|
23
|
|
|
{ |
|
24
|
|
|
public function setUp(): void |
|
25
|
|
|
{ |
|
26
|
|
|
parent::setUp(); |
|
27
|
|
|
$this->runCommandDebug('cycle:sync'); |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
$u = new User('Antony'); |
|
31
|
|
|
$u->roles->add(new Role('admin')); |
|
32
|
|
|
|
|
33
|
|
|
$this->app->get(Transaction::class)->persist($u)->run(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function testInjectedInstance(): void |
|
37
|
|
|
{ |
|
38
|
|
|
/** @var CoreInterface $core */ |
|
39
|
|
|
$core = $this->app->get(CoreInterface::class); |
|
40
|
|
|
|
|
41
|
|
|
$this->expectException(ControllerException::class); |
|
42
|
|
|
$core->callAction(DemoController::class, 'entity', []); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function testInjectedInstance1(): void |
|
46
|
|
|
{ |
|
47
|
|
|
/** @var CoreInterface $core */ |
|
48
|
|
|
$core = $this->app->get(CoreInterface::class); |
|
49
|
|
|
|
|
50
|
|
|
$this->expectException(ControllerException::class); |
|
51
|
|
|
$core->callAction(DemoController::class, 'entity', ['user' => 2]); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function testInjectedInstance2(): void |
|
55
|
|
|
{ |
|
56
|
|
|
/** @var CoreInterface $core */ |
|
57
|
|
|
$core = $this->app->get(CoreInterface::class); |
|
58
|
|
|
|
|
59
|
|
|
$this->assertSame( |
|
60
|
|
|
'Antony', |
|
61
|
|
|
$core->callAction(DemoController::class, 'entity', ['user' => 1]) |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
// singular entity |
|
66
|
|
|
public function testInjectedInstance3(): void |
|
67
|
|
|
{ |
|
68
|
|
|
/** @var CoreInterface $core */ |
|
69
|
|
|
$core = $this->app->get(CoreInterface::class); |
|
70
|
|
|
|
|
71
|
|
|
$this->assertSame( |
|
72
|
|
|
'Antony', |
|
73
|
|
|
$core->callAction(DemoController::class, 'entity', ['id' => 1]) |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function testMultipleEntitiesButID(): void |
|
78
|
|
|
{ |
|
79
|
|
|
/** @var CoreInterface $core */ |
|
80
|
|
|
$core = $this->app->get(CoreInterface::class); |
|
81
|
|
|
|
|
82
|
|
|
$this->expectException(ControllerException::class); |
|
83
|
|
|
$core->callAction(DemoController::class, 'entity2', ['id' => 1]); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// singular entity |
|
87
|
|
|
public function testMultipleEntities(): void |
|
88
|
|
|
{ |
|
89
|
|
|
/** @var CoreInterface $core */ |
|
90
|
|
|
$core = $this->app->get(CoreInterface::class); |
|
91
|
|
|
|
|
92
|
|
|
$this->assertSame( |
|
93
|
|
|
'ok', |
|
94
|
|
|
$core->callAction(DemoController::class, 'entity2', ['user' => 1, 'role' => 1]) |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
// singular entity |
|
99
|
|
|
public function testBypass(): void |
|
100
|
|
|
{ |
|
101
|
|
|
/** @var CoreInterface $core */ |
|
102
|
|
|
$core = $this->app->get(CoreInterface::class); |
|
103
|
|
|
|
|
104
|
|
|
$this->assertSame( |
|
105
|
|
|
'Demo', |
|
106
|
|
|
$core->callAction(DemoController::class, 'entity', ['user' => new User('Demo')]) |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|