Passed
Push — main ( 7d351e...d05000 )
by Michiel
07:03
created

WikiPublishTaskTest::array_find_key()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 3
nc 3
nop 2
1
<?php
2
3
/**
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the LGPL. For more information please see
18
 * <http://phing.info>.
19
 */
20
21
namespace Phing\Test\Task\Optional;
22
23
use Phing\Exception\BuildException;
24
use Phing\Task\Ext\WikiPublishTask;
25
use Phing\Test\Support\BuildFileTest;
26
use PHPUnit\Framework\MockObject\MockObject;
27
28
/**
29
 * WikiPublish task test.
30
 *
31
 * @author  Piotr Lewandowski <[email protected]>
32
 *
33
 * @internal
34
 * @phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
35
 */
36
class WikiPublishTaskTest extends BuildFileTest
37
{
38
    /**
39
     * Returns the KEY of the first element for which the $callback
40
     *  returns TRUE. If no matching element is found the function
41
     *  returns NULL.
42
     *
43
     * @param array $array The array that should be searched.
44
     * @param callable $callback The callback function to call to check
45
     *  each element. The first parameter contains the value ($value),
46
     *  the second parameter contains the corresponding key ($key). If
47
     *  this function returns TRUE, the key ($key) is returned
48
     *  immediately and the callback will not be called for further
49
     *  elements.
50
     *
51
     * @return mixed The key of the first element for which the
52
     *  $callback returns TRUE. NULL, If no matching element is found.
53
     */
54
    private function array_find_key(array $array, callable $callback)
55
    {
56
        foreach ($array as $key => $value) {
57
            if ($callback($value, $key)) {
58
                return $key;
59
            }
60
        }
61
62
        return null;
63
    }
64
65
    public function testApiEdit(): void
66
    {
67
        $task = $this->getWikiPublishMock();
68
69
        $task->setApiUrl('http://localhost/testApi.php');
70
        $task->setApiUser('testUser');
71
        $task->setApiPassword('testPassword');
72
73
        $task->setTitle('some page');
74
        $task->setContent('some content');
75
        $task->setMode('prepend');
76
77
        $callParams = [
78
            ['action=login', ['lgname' => 'testUser', 'lgpassword' => 'testPassword']],
79
            ['action=login', ['lgname' => 'testUser', 'lgpassword' => 'testPassword', 'lgtoken' => 'testLgToken']],
80
            ['action=tokens&type=edit'],
81
            ['action=edit&token=testEditToken%2B%2F', ['minor' => '', 'title' => 'some page', 'prependtext' => 'some content']]
82
        ];
83
        $returnResults = [
84
            ['login' => ['result' => 'NeedToken', 'token' => 'testLgToken']],
85
            ['login' => ['result' => 'Success']],
86
            ['tokens' => ['edittoken' => 'testEditToken+/']],
87
            ['edit' => ['result' => 'Success']]
88
        ];
89
90
        $task->expects($this->exactly(count($callParams)))
91
            ->method('callApi')
92
            ->willReturnCallback(function (string $action, array|null $args) use ($callParams, $returnResults): array {
93
                $index = $this->array_find_key($callParams, function (array $value) use ($action, $args): bool {
94
                    return $value[0] === $action && ($value[1] ?? null) === $args;
95
                });
96
                if (isset($callParams[$index])) {
97
                    $this->assertSame($callParams[$index][1] ?? null, $args);
98
                    return $returnResults[$index];
99
                }
100
                return [];
101
            })
102
        ;
103
104
        $task->main();
105
    }
106
107
    public function testInvalidAttributes(): void
108
    {
109
        $task = $this->getWikiPublishMock();
110
111
        try {
112
            $task->main();
113
        } catch (BuildException $e) {
114
            $this->assertEquals('Wiki apiUrl is required', $e->getMessage());
115
        }
116
117
        $task->setApiUrl('http://localhost/testApi.php');
118
119
        try {
120
            $task->main();
121
        } catch (BuildException $e) {
122
            $this->assertEquals('Wiki page id or title is required', $e->getMessage());
123
        }
124
    }
125
126
    /**
127
     * Creates WikiPublishTask mock.
128
     *
129
     * @return MockObject|WikiPublishTask
130
     */
131
    private function getWikiPublishMock()
132
    {
133
        $result = $this->getMockBuilder(WikiPublishTask::class);
134
135
        return $result->onlyMethods(['callApi'])->getMock();
136
    }
137
}
138