|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BasicTests; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use CommonTestClass; |
|
7
|
|
|
use kalanis\kw_notify\Extend\StackName; |
|
8
|
|
|
use kalanis\kw_notify\Interfaces\INotify; |
|
9
|
|
|
use kalanis\kw_notify\Notification; |
|
10
|
|
|
use kalanis\kw_notify\NotifyException; |
|
11
|
|
|
use kalanis\kw_notify\Stack; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
class NotifyTest extends CommonTestClass |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @throws NotifyException |
|
18
|
|
|
*/ |
|
19
|
|
|
public function testStack(): void |
|
20
|
|
|
{ |
|
21
|
|
|
$stack = new Stack(new \ArrayObject()); |
|
22
|
|
|
$stack->add('abc', 'def'); |
|
23
|
|
|
$stack->add('ghi', 'jkl'); |
|
24
|
|
|
$stack->add('ghi', 'mno'); |
|
25
|
|
|
$stack->add('ghi', 'pqr'); |
|
26
|
|
|
$this->assertEquals(['def'], $stack->get('abc')); |
|
27
|
|
|
$this->assertEquals(['jkl', 'mno', 'pqr', ], $stack->get('ghi')); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @throws NotifyException |
|
32
|
|
|
*/ |
|
33
|
|
|
public function testStackName(): void |
|
34
|
|
|
{ |
|
35
|
|
|
$stack = new Stack(new \ArrayObject()); |
|
36
|
|
|
$stackName = new StackName($stack, 'pre_', '_post'); |
|
37
|
|
|
$stackName->add('abc', 'def'); |
|
38
|
|
|
$stackName->add('ghi', 'jkl'); |
|
39
|
|
|
$stackName->add('ghi', 'mno'); |
|
40
|
|
|
$this->assertTrue($stackName->check('abc')); |
|
41
|
|
|
$this->assertFalse($stackName->check('def')); |
|
42
|
|
|
$this->assertEmpty($stackName->get('def')); |
|
43
|
|
|
$this->assertEquals(['def'], $stack->get('pre_abc_post')); |
|
44
|
|
|
$this->assertEquals(['jkl', 'mno', ], $stack->get('pre_ghi_post')); |
|
45
|
|
|
$stackName->reset('abc'); |
|
46
|
|
|
|
|
47
|
|
|
$stackName = new StackName($stack, '', '_po'); |
|
48
|
|
|
$stackName->add('abc', 'def'); |
|
49
|
|
|
$this->assertEquals(['def'], $stack->get('abc_po')); |
|
50
|
|
|
$this->assertEquals(['def'], $stackName->get('abc')); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @throws NotifyException |
|
55
|
|
|
*/ |
|
56
|
|
|
public function testNotify(): void |
|
57
|
|
|
{ |
|
58
|
|
|
$stack = new Stack(new \ArrayObject()); |
|
59
|
|
|
Notification::init($stack); |
|
60
|
|
|
$this->assertEmpty(Notification::getNotify()->get(INotify::TARGET_WARNING)); |
|
61
|
|
|
$this->assertEmpty(Notification::getNotify()->get(INotify::TARGET_SUCCESS)); |
|
62
|
|
|
Notification::addInfo('abc'); |
|
63
|
|
|
Notification::addInfo('def'); |
|
64
|
|
|
Notification::addWarning('ghi'); |
|
65
|
|
|
Notification::addError('jkl'); |
|
66
|
|
|
Notification::addSuccess('mno'); |
|
67
|
|
|
|
|
68
|
|
|
$this->assertEquals(['abc', 'def'], $stack->get(INotify::TARGET_INFO)); |
|
69
|
|
|
$this->assertEquals(['ghi'], $stack->get(INotify::TARGET_WARNING)); |
|
70
|
|
|
$this->assertEquals(['abc', 'def', ], Notification::getNotify()->get(INotify::TARGET_INFO)); |
|
71
|
|
|
$this->assertEquals(['ghi', ], Notification::getNotify()->get(INotify::TARGET_WARNING)); |
|
72
|
|
|
$this->assertEquals(['jkl', ], Notification::getNotify()->get(INotify::TARGET_ERROR)); |
|
73
|
|
|
$this->assertEquals(['mno', ], Notification::getNotify()->get(INotify::TARGET_SUCCESS)); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @throws NotifyException |
|
78
|
|
|
*/ |
|
79
|
|
|
public function testNotifyFail(): void |
|
80
|
|
|
{ |
|
81
|
|
|
Notify::unsetStack(); |
|
82
|
|
|
$this->expectException(NotifyException::class); |
|
83
|
|
|
Notify::addInfo('abc'); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
class Notify extends Notification |
|
89
|
|
|
{ |
|
90
|
|
|
public static function unsetStack(): void |
|
91
|
|
|
{ |
|
92
|
|
|
static::$storage = null; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|