1
|
|
|
<?php declare( strict_types = 1 ); |
2
|
|
|
|
3
|
|
|
namespace Coco\SourceWatcher\Tests\Core\Api; |
4
|
|
|
|
5
|
|
|
use Coco\SourceWatcher\Core\Api\ApiReader; |
6
|
|
|
use Coco\SourceWatcher\Core\SourceWatcherException; |
7
|
|
|
use Coco\SourceWatcher\Utils\i18n; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class ApiReaderTest |
12
|
|
|
* @package Coco\SourceWatcher\Tests\Core\Api |
13
|
|
|
*/ |
14
|
|
|
class ApiReaderTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* |
18
|
|
|
*/ |
19
|
|
|
public function testSetGetResourceURL () : void |
20
|
|
|
{ |
21
|
|
|
$apiReader = new ApiReader(); |
22
|
|
|
|
23
|
|
|
$givenResourceURL = "https://api.github.com/emojis"; |
24
|
|
|
$expectedResourceURL = "https://api.github.com/emojis"; |
25
|
|
|
|
26
|
|
|
$apiReader->setResourceURL( $givenResourceURL ); |
27
|
|
|
|
28
|
|
|
$this->assertEquals( $expectedResourceURL, $apiReader->getResourceURL() ); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* |
33
|
|
|
*/ |
34
|
|
|
public function testSetGetTimeout () : void |
35
|
|
|
{ |
36
|
|
|
$apiReader = new ApiReader(); |
37
|
|
|
|
38
|
|
|
$givenTimeout = 10; |
39
|
|
|
$expectedTimeout = 10; |
40
|
|
|
|
41
|
|
|
$apiReader->setTimeout( $givenTimeout ); |
42
|
|
|
|
43
|
|
|
$this->assertEquals( $expectedTimeout, $apiReader->getTimeout() ); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* |
48
|
|
|
*/ |
49
|
|
|
public function testSetGetHeaders () : void |
50
|
|
|
{ |
51
|
|
|
$apiReader = new ApiReader(); |
52
|
|
|
|
53
|
|
|
$givenHeaders = [ "Cache-Control: no-cache", "Content-Type: application/x-www-form-urlencoded; charset=utf-8", "Host: www.example.com" ]; |
54
|
|
|
$expectedHeaders = [ "Cache-Control: no-cache", "Content-Type: application/x-www-form-urlencoded; charset=utf-8", "Host: www.example.com" ]; |
55
|
|
|
|
56
|
|
|
$apiReader->setHeaders( $givenHeaders ); |
57
|
|
|
|
58
|
|
|
$this->assertEquals( $expectedHeaders, $apiReader->getHeaders() ); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @throws SourceWatcherException |
63
|
|
|
*/ |
64
|
|
|
public function testGetExceptionFromNoResourceURL () : void |
65
|
|
|
{ |
66
|
|
|
$apiReader = new ApiReader(); |
67
|
|
|
|
68
|
|
|
$this->expectException( SourceWatcherException::class ); |
69
|
|
|
$this->expectExceptionMessage( i18n::getInstance()->getText( "en_US", ApiReader::class, "No_Resource_URL_Found" ) ); |
70
|
|
|
|
71
|
|
|
$apiReader->read(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @throws SourceWatcherException |
76
|
|
|
*/ |
77
|
|
|
public function testGetResult () : void |
78
|
|
|
{ |
79
|
|
|
$apiReader = new ApiReader(); |
80
|
|
|
$apiReader->setResourceURL( "https://api.github.com/emojis" ); |
81
|
|
|
$apiReader->setHeaders( [ "User-Agent: request" ] ); |
82
|
|
|
|
83
|
|
|
$this->assertNotNull( $apiReader->read() ); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @throws SourceWatcherException |
88
|
|
|
*/ |
89
|
|
|
public function testAttemptsLoop () : void |
90
|
|
|
{ |
91
|
|
|
$apiReader = new ApiReader(); |
92
|
|
|
$apiReader->setResourceURL( "https://thiswebsiteisnotreal.com" ); |
93
|
|
|
|
94
|
|
|
$this->assertNotNull( $apiReader->read() ); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|