Completed
Pull Request — master (#16)
by Chris
02:24
created

SwiftypeCrawlerTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 80
c 1
b 0
f 0
dl 0
loc 159
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 13 1
A testCrawlFailResponseCode() 0 30 1
A testCrawlFailResponseData() 0 33 1
A testCrawlSuccess() 0 23 1
A testCrawlError() 0 30 1
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
     * @throws ValidationException
31
     */
32
    public function setUp(): void
33
    {
34
        parent::setUp();
35
36
        /** @var SiteConfig|SwiftypeSiteConfigFieldsExtension $siteConfig */
37
        $siteConfig = SiteConfig::current_site_config();
38
39
        $siteConfig->SwiftypeEnabled = 1;
40
        $siteConfig->SwiftypeEngineSlug = 1;
41
        $siteConfig->SwiftypeDomainID = 1;
42
        $siteConfig->SwiftypeAPIKey = 1;
43
44
        $siteConfig->write();
45
    }
46
47
    /**
48
     * Test that a crawl will succeed
49
     */
50
    public function testCrawlSuccess(): void
51
    {
52
        /** @var SiteConfig|SwiftypeSiteConfigFieldsExtension $config */
53
        $config = SiteConfig::current_site_config();
54
55
        $config->SwiftypeEnabled = 1;
56
        $config->SwiftypeEngineSlug = 'test';
57
        $config->SwiftypeDomainID = 'test';
58
        $config->SwiftypeAPIKey = 'test';
59
60
        $config->write();
61
62
        $responseCode = 201;
63
        $mock = new MockHandler([
64
            new Response($responseCode),
65
        ]);
66
67
        $handler = HandlerStack::create($mock);
68
        $client = new Client(['handler' => $handler]);
69
        $crawler = SwiftypeCrawler::create($client);
70
71
        // True represents a successful crawl request
72
        $this->assertTrue($crawler->send('https://www.someurl.com'));
73
    }
74
75
    /**
76
     * Test that a crawl will fail on invalid response code
77
     */
78
    public function testCrawlFailResponseCode(): void
79
    {
80
        /** @var SiteConfig|SwiftypeSiteConfigFieldsExtension $config */
81
        $config = SiteConfig::current_site_config();
82
83
        $config->SwiftypeEnabled = 1;
84
        $config->SwiftypeEngineSlug = 'test';
85
        $config->SwiftypeDomainID = 'test';
86
        $config->SwiftypeAPIKey = 'test';
87
88
        $config->write();
89
90
        $responseCode = 301;
91
        $mock = new MockHandler([
92
            new Response($responseCode),
93
        ]);
94
95
        $handler = HandlerStack::create($mock);
96
        $client = new Client(['handler' => $handler]);
97
        $crawler = new SwiftypeCrawler($client);
98
99
        $expectedMessage = sprintf(
100
            "Swiftype Crawl request failed - invalid response code \n%s\n%s\n%s",
101
            $responseCode,
102
            json_encode([]),
103
            ''
104
        );
105
106
        $this->assertFalse($crawler->send('https://www.someurl.com'));
107
        $this->assertEquals($expectedMessage, $crawler->getMessages()[0]);
108
    }
109
110
    /**
111
     * Test that a crawl will fail in invalid response data
112
     */
113
    public function testCrawlFailResponseData(): void
114
    {
115
        /** @var SiteConfig|SwiftypeSiteConfigFieldsExtension $config */
116
        $config = SiteConfig::current_site_config();
117
118
        $config->SwiftypeEnabled = 1;
119
        $config->SwiftypeEngineSlug = 'test';
120
        $config->SwiftypeDomainID = 'test';
121
        $config->SwiftypeAPIKey = 'test';
122
123
        $config->write();
124
125
        $responseCode = 200;
126
        $mockBody = json_encode([
127
            'error' => 'test error message',
128
        ]);
129
        $mock = new MockHandler([
130
            new Response($responseCode, [], $mockBody),
131
        ]);
132
133
        $handler = HandlerStack::create($mock);
134
        $client = new Client(['handler' => $handler]);
135
        $crawler = new SwiftypeCrawler($client);
136
137
        $expectedMessage = sprintf(
138
            "Swiftype Crawl request failed - invalid response data \n%s\n%s\n%s",
139
            $responseCode,
140
            json_encode([]),
141
            $mockBody
142
        );
143
144
        $this->assertFalse($crawler->send('https://www.someurl.com'));
145
        $this->assertEquals($expectedMessage, $crawler->getMessages()[0]);
146
    }
147
148
    /**
149
     * Test that a crawl will fail
150
     */
151
    public function testCrawlError(): void
152
    {
153
        /** @var SiteConfig|SwiftypeSiteConfigFieldsExtension $config */
154
        $config = SiteConfig::current_site_config();
155
156
        $config->SwiftypeEnabled = 1;
157
        $config->SwiftypeEngineSlug = 'test';
158
        $config->SwiftypeDomainID = 'test';
159
        $config->SwiftypeAPIKey = 'test';
160
161
        $config->write();
162
163
        $mock = new MockHandler([
164
            new RequestException("Error Communicating with Server", new Request('GET', 'test')),
165
        ]);
166
167
        $handler = HandlerStack::create($mock);
168
        $client = new Client(['handler' => $handler]);
169
        $crawler = new SwiftypeCrawler($client);
170
171
        $url = 'https://www.someurl.com';
172
        // Lets run it and get a not good response
173
        $expectedMessage = sprintf(
174
            'Exception %s for url: %s message: Error Communicating with Server',
175
            RequestException::class,
176
            $url
177
        );
178
179
        $this->assertFalse($crawler->send($url));
180
        $this->assertEquals($expectedMessage, $crawler->getMessages()[0]);
181
    }
182
}
183