Passed
Push — master ( bacc5f...5eccc7 )
by Dispositif
02:22
created

WstatImportTest::testGetData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 49
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 28
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 49
rs 9.472
1
<?php
2
/**
3
 * This file is part of dispositif/wikibot application
4
 * 2019 : Philippe M. <[email protected]>
5
 * For the full copyright and MIT license information, please view the LICENSE file.
6
 */
7
8
declare(strict_types=1);
9
10
namespace App\Infrastructure;
11
12
use GuzzleHttp\Client;
13
use GuzzleHttp\Handler\MockHandler;
14
use GuzzleHttp\HandlerStack;
15
use GuzzleHttp\Psr7\Response;
16
use PHPUnit\Framework\TestCase;
17
18
/**
19
 * @unused
20
 * Class WstatImportTest
21
 *
22
 * @package App\Infrastructure
23
 */
24
class WstatImportTest extends TestCase
25
{
26
    public function testGetData()
27
    {
28
        $this::markTestSkipped('long time test (delayed http request');
29
        // 2 sets by json string
30
        $json
31
            = '["Alexandre S. Giffard|{{Ouvrage|pr\u00e9nom1=Karel,|isbn=2763772358|isbn2=9782763772356}}","Alexandre Saint-Yves d\'Alveydre|{{Ouvrage|nom1= Saunier|titre= La Synarchie}}", "<!-- + -->"]';
32
        $json2
33
            = '["Bob|{{Ouvrage|pr\u00e9nom1=Karel,|titre=Dice|isbn=2763772358|isbn2=9782763772356}}","Bob2|{{Ouvrage|nom1= Saunier|titre= La Synarchie}}", "<!-- + -->"]';
34
35
        // Mocking : stack of Responses or Exception for multiple http requests
36
        $mock = new MockHandler(
37
            [
38
                new Response(200, ['X-Foo' => 'Bar'], $json),
39
                new Response(200, [], $json2),
40
                new Response(200, [], $json2),
41
            ]
42
        );
43
        $handler = HandlerStack::create($mock);
44
        $client = new Client(['handler' => $handler]);
45
46
        $wstat = new WstatImport(
47
            $client, [
48
            'title' => 'Ouvrage',
49
            'query' => 'inclusions',
50
            'param' => 'isbn',
51
            'start' => 50000,
52
            'limit' => 10000, // no influence here
53
        ], 4 // so 2 loop
54
        );
55
56
        $data = $wstat->getData();
57
        // first request
58
        $this::assertEquals(
59
            [
60
                'title' => 'Alexandre S. Giffard',
61
                'template' => '{{Ouvrage|prénom1=Karel,|isbn=2763772358|isbn2=9782763772356}}',
62
            ],
63
            $data[0]
64
        );
65
        // second httpRequest
66
        $this::assertEquals(
67
            [
68
                'title' => 'Bob2',
69
                'template' => '{{Ouvrage|nom1= Saunier|titre= La Synarchie}}',
70
            ],
71
            $data[3]
72
        );
73
        // No 3nd httpRequest
74
        $this::assertFalse(isset($data[5]));
75
    }
76
77
    public function testGetUrl()
78
    {
79
        // todo verify
80
        $this::markTestSkipped('long time test ??? HTTTP request ??? (delayed http request)');
81
        $wstat = new WstatImport(
82
            new Client(['timeout'=>10, 'headers' => ['User-Agent' => getenv('USER_AGENT')]]),
83
            [
84
                'title' => 'Ouvrage',
85
                'query' => 'inclusions',
86
                'param' => 'isbn',
87
                'start' => 50000,
88
                'limit' => 500,
89
            ], 10000
90
        );
91
        $this::assertEquals(
92
            'https://wstat.fr/template/index.php?title=Ouvrage&query=inclusions&param=isbn&start=50000&limit=500&format=json',
93
            $wstat->getUrl()
94
        );
95
    }
96
}
97