summaryrefslogtreecommitdiff
path: root/tests/units/Core/Plugin/HookTest.php
blob: acadede0abb2a59fad4b0c9feb92312cf49a6650 (plain)
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
<?php

require_once __DIR__.'/../../Base.php';

use Kanboard\Core\Plugin\Hook;

class HookTest extends Base
{
    public function testGetListeners()
    {
        $hook = new Hook;
        $this->assertEmpty($hook->getListeners('myhook'));

        $hook->on('myhook', 'A');
        $hook->on('myhook', 'B');

        $this->assertEquals(array('A', 'B'), $hook->getListeners('myhook'));
    }

    public function testExists()
    {
        $hook = new Hook;
        $this->assertFalse($hook->exists('myhook'));

        $hook->on('myhook', 'A');

        $this->assertTrue($hook->exists('myhook'));
    }

    public function testMergeWithNoBinding()
    {
        $hook = new Hook;
        $values = array('A', 'B');

        $result = $hook->merge('myhook', $values, array('p' => 'c'));
        $this->assertEquals($values, $result);
    }

    public function testMergeWithBindings()
    {
        $hook = new Hook;
        $values = array('A', 'B');
        $expected = array('A', 'B', 'c', 'D');

        $hook->on('myhook', function ($p) {
            return array($p);
        });

        $hook->on('myhook', function () {
            return array('D');
        });

        $result = $hook->merge('myhook', $values, array('p' => 'c'));
        $this->assertEquals($expected, $result);
        $this->assertEquals($expected, $values);
    }

    public function testMergeWithBindingButReturningBadData()
    {
        $hook = new Hook;
        $values = array('A', 'B');
        $expected = array('A', 'B');

        $hook->on('myhook', function () {
            return 'string';
        });

        $result = $hook->merge('myhook', $values);
        $this->assertEquals($expected, $result);
        $this->assertEquals($expected, $values);
    }

    public function testFirstWithNoBinding()
    {
        $hook = new Hook;

        $result = $hook->first('myhook', array('p' => 2));
        $this->assertEquals(null, $result);
    }

    public function testFirstWithMultipleBindings()
    {
        $hook = new Hook;

        $hook->on('myhook', function ($p) {
            return $p + 1;
        });

        $hook->on('myhook', function ($p) {
            return $p;
        });

        $result = $hook->first('myhook', array('p' => 3));
        $this->assertEquals(4, $result);
    }

    public function testHookWithReference()
    {
        $hook = new Hook();

        $hook->on('myhook', function (&$p) {
            $p = 2;
        });

        $param = 123;
        $result = $hook->reference('myhook', $param);
        $this->assertSame(2, $result);
        $this->assertSame(2, $param);
    }
}