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