SwiftypeCrawlerTest::testCrawlSuccess()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 13
rs 10
1
<?php
2
3
namespace Ichaber\SSSwiftype\Tests\Service;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\RequestException;
7
use GuzzleHttp\Handler\MockHandler;
8
use GuzzleHttp\HandlerStack;
9
use GuzzleHttp\Psr7\Request;
10
use Ichaber\SSSwiftype\Extensions\SwiftypeSiteConfigFieldsExtension;
11
use Ichaber\SSSwiftype\Service\SwiftypeCrawler;
12
use SilverStripe\Dev\SapphireTest;
13
use GuzzleHttp\Psr7\Response;
14
use SilverStripe\SiteConfig\SiteConfig;
15
16
/**
17
 * Class SwiftypeCrawlerTest
18
 *
19
 * @package Ichaber\SSSwiftype\Tests\Service
20
 */
21
class SwiftypeCrawlerTest extends SapphireTest
22
{
23
    /**
24
     * @var string
25
     */
26
    protected static $fixture_file = 'SwiftypeCrawlerTest.yml';
27
28
    /**
29
     * @var array
30
     */
31
    protected static $required_extensions = [
32
        SiteConfig::class => [
33
            SwiftypeSiteConfigFieldsExtension::class,
34
        ],
35
    ];
36
37
    /**
38
     * Test that a crawl will succeed
39
     */
40
    public function testCrawlSuccess(): void
41
    {
42
        $responseCode = 201;
43
        $mock = new MockHandler([
44
            new Response($responseCode),
45
        ]);
46
47
        $handler = HandlerStack::create($mock);
48
        $client = new Client(['handler' => $handler]);
49
        $crawler = SwiftypeCrawler::create($client);
50
51
        // True represents a successful crawl request
52
        $this->assertTrue($crawler->send('https://www.someurl.com'));
53
    }
54
55
    /**
56
     * Test that a crawl will fail on invalid response code
57
     */
58
    public function testCrawlFailResponseCode(): void
59
    {
60
        $responseCode = 301;
61
        $mock = new MockHandler([
62
            new Response($responseCode),
63
        ]);
64
65
        $handler = HandlerStack::create($mock);
66
        $client = new Client(['handler' => $handler]);
67
        $crawler = SwiftypeCrawler::create($client);
68
69
        $expectedMessage = sprintf(
70
            "Swiftype Crawl request failed - invalid response code \n%s\n%s\n%s",
71
            $responseCode,
72
            json_encode([]),
73
            ''
74
        );
75
76
        $this->assertFalse($crawler->send('https://www.someurl.com'));
77
        $this->assertEquals($expectedMessage, $crawler->getMessages()[0]);
78
    }
79
80
    /**
81
     * Test that a crawl will fail in invalid response data
82
     */
83
    public function testCrawlFailResponseData(): void
84
    {
85
        $responseCode = 200;
86
        $mockBody = json_encode([
87
            'error' => 'test error message',
88
        ]);
89
        $mock = new MockHandler([
90
            new Response($responseCode, [], $mockBody),
91
        ]);
92
93
        $handler = HandlerStack::create($mock);
94
        $client = new Client(['handler' => $handler]);
95
        $crawler = SwiftypeCrawler::create($client);
96
97
        $expectedMessage = sprintf(
98
            "Swiftype Crawl request failed - invalid response data \n%s\n%s\n%s",
99
            $responseCode,
100
            json_encode([]),
101
            $mockBody
102
        );
103
104
        $this->assertFalse($crawler->send('https://www.someurl.com'));
105
        $this->assertEquals($expectedMessage, $crawler->getMessages()[0]);
106
    }
107
108
    /**
109
     * Test that a crawl will fail
110
     */
111
    public function testCrawlError(): void
112
    {
113
        $mock = new MockHandler([
114
            new RequestException("Error Communicating with Server", new Request('GET', 'test')),
115
        ]);
116
117
        $handler = HandlerStack::create($mock);
118
        $client = new Client(['handler' => $handler]);
119
        $crawler = SwiftypeCrawler::create($client);
120
121
        $url = 'https://www.someurl.com';
122
        // Lets run it and get a not good response
123
        $expectedMessage = sprintf(
124
            'Exception %s for url: %s message: Error Communicating with Server',
125
            RequestException::class,
126
            $url
127
        );
128
129
        $this->assertFalse($crawler->send($url));
130
        $this->assertEquals($expectedMessage, $crawler->getMessages()[0]);
131
    }
132
}
133