Passed
Pull Request — master (#16)
by Chris
02:06
created

SwiftypeCrawlerTest::testCrawlFailResponseData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 21
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 32
rs 9.584
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\ORM\ValidationException;
15
use SilverStripe\SiteConfig\SiteConfig;
16
17
/**
18
 * Class SwiftypeCrawlerTest
19
 *
20
 * @package Ichaber\SSSwiftype\Tests\Service
21
 */
22
class SwiftypeCrawlerTest extends SapphireTest
23
{
24
    /**
25
     * @var bool
26
     */
27
    protected $usesDatabase = true;
28
29
    /**
30
     * Test that a crawl will succeed
31
     *
32
     * @throws ValidationException
33
     */
34
    public function testCrawlSuccess(): void
35
    {
36
        /** @var SiteConfig|SwiftypeSiteConfigFieldsExtension $siteConfig */
37
        $siteConfig = SiteConfig::current_site_config();
38
        $siteConfig->SwiftypeEnabled = 1;
39
        $siteConfig->SwiftypeEngineSlug = 1;
40
        $siteConfig->SwiftypeDomainID = 1;
41
        $siteConfig->SwiftypeAPIKey = 1;
42
43
        $siteConfig->write();
44
45
        $responseCode = 201;
46
        $mock = new MockHandler([
47
            new Response($responseCode),
48
        ]);
49
50
        $handler = HandlerStack::create($mock);
51
        $client = new Client(['handler' => $handler]);
52
        $crawler = SwiftypeCrawler::create($client);
53
54
        // True represents a successful crawl request
55
        $this->assertTrue($crawler->send('https://www.someurl.com'));
56
    }
57
58
    /**
59
     * Test that a crawl will fail on invalid response code
60
     */
61
    public function testCrawlFailResponseCode(): void
62
    {
63
        /** @var SiteConfig|SwiftypeSiteConfigFieldsExtension $siteConfig */
64
        $siteConfig = SiteConfig::current_site_config();
65
        $siteConfig->SwiftypeEnabled = 1;
66
        $siteConfig->SwiftypeEngineSlug = 1;
67
        $siteConfig->SwiftypeDomainID = 1;
68
        $siteConfig->SwiftypeAPIKey = 1;
69
70
        $siteConfig->write();
71
72
        $responseCode = 301;
73
        $mock = new MockHandler([
74
            new Response($responseCode),
75
        ]);
76
77
        $handler = HandlerStack::create($mock);
78
        $client = new Client(['handler' => $handler]);
79
        $crawler = new SwiftypeCrawler($client);
80
81
        $expectedMessage = sprintf(
82
            "Swiftype Crawl request failed - invalid response code \n%s\n%s\n%s",
83
            $responseCode,
84
            json_encode([]),
85
            ''
86
        );
87
88
        $this->assertFalse($crawler->send('https://www.someurl.com'));
89
        $this->assertEquals($expectedMessage, $crawler->getMessages()[0]);
90
    }
91
92
    /**
93
     * Test that a crawl will fail in invalid response data
94
     */
95
    public function testCrawlFailResponseData(): void
96
    {
97
        /** @var SiteConfig|SwiftypeSiteConfigFieldsExtension $siteConfig */
98
        $siteConfig = SiteConfig::current_site_config();
99
        $siteConfig->SwiftypeEnabled = 1;
100
        $siteConfig->SwiftypeEngineSlug = 1;
101
        $siteConfig->SwiftypeDomainID = 1;
102
        $siteConfig->SwiftypeAPIKey = 1;
103
104
        $siteConfig->write();
105
106
        $responseCode = 200;
107
        $mockBody = json_encode([
108
            'error' => 'test error message',
109
        ]);
110
        $mock = new MockHandler([
111
            new Response($responseCode, [], $mockBody),
112
        ]);
113
114
        $handler = HandlerStack::create($mock);
115
        $client = new Client(['handler' => $handler]);
116
        $crawler = new SwiftypeCrawler($client);
117
118
        $expectedMessage = sprintf(
119
            "Swiftype Crawl request failed - invalid response data \n%s\n%s\n%s",
120
            $responseCode,
121
            json_encode([]),
122
            $mockBody
123
        );
124
125
        $this->assertFalse($crawler->send('https://www.someurl.com'));
126
        $this->assertEquals($expectedMessage, $crawler->getMessages()[0]);
127
    }
128
129
    /**
130
     * Test that a crawl will fail
131
     */
132
    public function testCrawlError(): void
133
    {
134
        /** @var SiteConfig|SwiftypeSiteConfigFieldsExtension $siteConfig */
135
        $siteConfig = SiteConfig::current_site_config();
136
        $siteConfig->SwiftypeEnabled = 1;
137
        $siteConfig->SwiftypeEngineSlug = 1;
138
        $siteConfig->SwiftypeDomainID = 1;
139
        $siteConfig->SwiftypeAPIKey = 1;
140
141
        $siteConfig->write();
142
143
        $mock = new MockHandler([
144
            new RequestException("Error Communicating with Server", new Request('GET', 'test')),
145
        ]);
146
147
        $handler = HandlerStack::create($mock);
148
        $client = new Client(['handler' => $handler]);
149
        $crawler = new SwiftypeCrawler($client);
150
151
        $url = 'https://www.someurl.com';
152
        // Lets run it and get a not good response
153
        $expectedMessage = sprintf(
154
            'Exception %s for url: %s message: Error Communicating with Server',
155
            RequestException::class,
156
            $url
157
        );
158
159
        $this->assertFalse($crawler->send($url));
160
        $this->assertEquals($expectedMessage, $crawler->getMessages()[0]);
161
    }
162
}
163