|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
|
4
|
|
|
|
|
5
|
|
|
namespace WMDE\Fundraising\Frontend\Tests\Unit\Cli; |
|
6
|
|
|
|
|
7
|
|
|
use FileFetcher\FileFetcher; |
|
8
|
|
|
use WMDE\Fundraising\Frontend\Cli\ConfigValidation\ConfigValidationException; |
|
9
|
|
|
use WMDE\Fundraising\Frontend\Cli\ConfigValidation\SchemaLoader; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @covers \WMDE\Fundraising\Frontend\Cli\ConfigValidation\SchemaLoader |
|
13
|
|
|
*/ |
|
14
|
|
|
class SchemaLoaderTest extends \PHPUnit\Framework\TestCase { |
|
15
|
|
|
|
|
16
|
|
|
public function testOnFileFetchingError_runtimeExceptionIsThrown(): void { |
|
17
|
|
|
$fileFetcher = $this->createMock( FileFetcher::class ); |
|
18
|
|
|
$fileFetcher->method( 'fetchFile' )->willThrowException( new \RuntimeException() ); |
|
19
|
|
|
$loader = new SchemaLoader( $fileFetcher ); |
|
20
|
|
|
$this->expectException( \RuntimeException::class ); |
|
21
|
|
|
$loader->loadSchema( 'test.json' ); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function testGivenInvalidJson_validationExceptionIsThrown(): void { |
|
25
|
|
|
$fileFetcher = $this->createMock( FileFetcher::class ); |
|
26
|
|
|
$fileFetcher->method( 'fetchFile' )->willReturn( 'Not a valid JSON string' ); |
|
27
|
|
|
$loader = new SchemaLoader( $fileFetcher ); |
|
28
|
|
|
$this->expectException( ConfigValidationException::class ); |
|
29
|
|
|
$loader->loadSchema( 'test.json' ); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function testGivenJsonRootIsNotAnObject_validationExceptionIsThrown(): void { |
|
33
|
|
|
$fileFetcher = $this->createMock( FileFetcher::class ); |
|
34
|
|
|
$fileFetcher->method( 'fetchFile' )->willReturn( '"A valid JSON string"' ); |
|
35
|
|
|
$loader = new SchemaLoader( $fileFetcher ); |
|
36
|
|
|
$this->expectException( ConfigValidationException::class ); |
|
37
|
|
|
$loader->loadSchema( 'test.json' ); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function testGivenValidJson_itIsReturnedAsObject(): void { |
|
41
|
|
|
$fileFetcher = $this->createMock( FileFetcher::class ); |
|
42
|
|
|
$fileFetcher->method( 'fetchFile' )->willReturn( '{"testProperty": "A valid JSON string"}' ); |
|
43
|
|
|
$loader = new SchemaLoader( $fileFetcher ); |
|
44
|
|
|
|
|
45
|
|
|
$expectedObject = new \stdClass(); |
|
46
|
|
|
$expectedObject->testProperty = 'A valid JSON string'; |
|
47
|
|
|
$this->assertEquals( $expectedObject, $loader->loadSchema( 'test.json' ) ); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
|