-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathHookTest.php
121 lines (94 loc) · 2.85 KB
/
HookTest.php
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
namespace Utopia\Http;
use PHPUnit\Framework\TestCase;
use Utopia\DI\Container;
use Utopia\DI\Dependency;
use Utopia\Http\Validator\Numeric;
use Utopia\Http\Validator\Text;
class HookTest extends TestCase
{
/**
* @var Hook
*/
protected ?Hook $hook;
public function setUp(): void
{
$this->hook = new Hook();
}
public function testDescriptionCanBeSet()
{
$this->assertEquals('', $this->hook->getDesc());
$this->hook->desc('new hook');
$this->assertEquals('new hook', $this->hook->getDesc());
}
public function testGroupsCanBeSet()
{
$this->assertEquals([], $this->hook->getGroups());
$this->hook->groups(['api', 'homepage']);
$this->assertEquals(['api', 'homepage'], $this->hook->getGroups());
}
public function testActionCanBeSet()
{
$this->hook->action(fn () => 'hello world');
$this->assertIsCallable($this->hook->getAction());
$this->assertEquals('hello world', $this->hook->getAction()());
}
public function testParamCanBeSet()
{
$this->assertEquals([], $this->hook->getParams());
$this->hook
->param('x', '', new Text(10))
->param('y', '', new Text(10));
$this->assertCount(2, $this->hook->getParams());
}
public function testResourcesCanBeInjected()
{
$main = $this->hook
->setName('test')
->inject('user')
->inject('time')
->setCallback(function ($user, $time) {
return $user . ':' . $time;
});
$user = new Dependency();
$user
->setName('user')
->setCallback(function () {
return 'user';
});
$time = new Dependency();
$time
->setName('time')
->setCallback(function () {
return '00:00:00';
});
$context = new Container();
$context
->set($user)
->set($time)
;
$result = $context->inject($main);
$this->assertEquals('user:00:00:00', $result);
}
public function testParamValuesCanBeSet()
{
$this->assertEquals([], $this->hook->getParams());
$values = [
'x' => 'hello',
'y' => 'world',
];
$this->hook
->param('x', '', new Numeric())
->param('y', '', new Numeric());
foreach ($this->hook->getParams() as $key => $param) {
$this->hook->setParamValue($key, $values[$key]);
}
$this->assertCount(2, $this->hook->getParams());
$this->assertEquals('hello', $this->hook->getParams()['x']['value']);
$this->assertEquals('world', $this->hook->getParams()['y']['value']);
}
public function tearDown(): void
{
$this->hook = null;
}
}