Source of file SwiftypeCrawlerTest.php
Size: 3,814 Bytes - Last Modified: 2021-12-24T05:17:12+00:00
/var/www/docs.ssmods.com/process/src/tests/php/Service/SwiftypeCrawlerTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 | <?php namespace Ichaber\SSSwiftype\Tests\Service; use GuzzleHttp\Client; use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\HandlerStack; use GuzzleHttp\Psr7\Request; use Ichaber\SSSwiftype\Extensions\SwiftypeSiteConfigFieldsExtension; use Ichaber\SSSwiftype\Service\SwiftypeCrawler; use SilverStripe\Dev\SapphireTest; use GuzzleHttp\Psr7\Response; use SilverStripe\SiteConfig\SiteConfig; /** * Class SwiftypeCrawlerTest * * @package Ichaber\SSSwiftype\Tests\Service */ class SwiftypeCrawlerTest extends SapphireTest { /** * @var string */ protected static $fixture_file = 'SwiftypeCrawlerTest.yml'; /** * @var array */ protected static $required_extensions = [ SiteConfig::class => [ SwiftypeSiteConfigFieldsExtension::class, ], ]; /** * Test that a crawl will succeed */ public function testCrawlSuccess(): void { $responseCode = 201; $mock = new MockHandler([ new Response($responseCode), ]); $handler = HandlerStack::create($mock); $client = new Client(['handler' => $handler]); $crawler = SwiftypeCrawler::create($client); // True represents a successful crawl request $this->assertTrue($crawler->send('https://www.someurl.com')); } /** * Test that a crawl will fail on invalid response code */ public function testCrawlFailResponseCode(): void { $responseCode = 301; $mock = new MockHandler([ new Response($responseCode), ]); $handler = HandlerStack::create($mock); $client = new Client(['handler' => $handler]); $crawler = SwiftypeCrawler::create($client); $expectedMessage = sprintf( "Swiftype Crawl request failed - invalid response code \n%s\n%s\n%s", $responseCode, json_encode([]), '' ); $this->assertFalse($crawler->send('https://www.someurl.com')); $this->assertEquals($expectedMessage, $crawler->getMessages()[0]); } /** * Test that a crawl will fail in invalid response data */ public function testCrawlFailResponseData(): void { $responseCode = 200; $mockBody = json_encode([ 'error' => 'test error message', ]); $mock = new MockHandler([ new Response($responseCode, [], $mockBody), ]); $handler = HandlerStack::create($mock); $client = new Client(['handler' => $handler]); $crawler = SwiftypeCrawler::create($client); $expectedMessage = sprintf( "Swiftype Crawl request failed - invalid response data \n%s\n%s\n%s", $responseCode, json_encode([]), $mockBody ); $this->assertFalse($crawler->send('https://www.someurl.com')); $this->assertEquals($expectedMessage, $crawler->getMessages()[0]); } /** * Test that a crawl will fail */ public function testCrawlError(): void { $mock = new MockHandler([ new RequestException("Error Communicating with Server", new Request('GET', 'test')), ]); $handler = HandlerStack::create($mock); $client = new Client(['handler' => $handler]); $crawler = SwiftypeCrawler::create($client); $url = 'https://www.someurl.com'; // Lets run it and get a not good response $expectedMessage = sprintf( 'Exception %s for url: %s message: Error Communicating with Server', RequestException::class, $url ); $this->assertFalse($crawler->send($url)); $this->assertEquals($expectedMessage, $crawler->getMessages()[0]); } } |