Completed
Push — master ( f8b3c5...acbd93 )
by mw
05:07
created

JsonParseOnFauxHttpResponseTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 12
c 2
b 1
f 0
lcom 0
cbo 7
dl 0
loc 101
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 16 1
B testQueryResultFetcherFromCannedJsonResponse() 0 26 1
A assertSubjectList() 0 17 4
A assertPrintRequestPropertyList() 0 17 4
A jsonFileProvider() 0 14 2
1
<?php
2
3
namespace SEQL\Tests\Integration\ByAskApiHttpRequest;
4
5
use SMW\Tests\Utils\UtilityFactory;
6
use Onoi\HttpRequest\HttpRequestFactory;
7
use SEQL\ByAskApiHttpRequest\QueryResultFetcher;
8
use SEQL\ByAskApiHttpRequest\JsonResponseParser;
9
use SEQL\QueryResultFactory;
10
use SEQL\DataValueDeserializer;
11
use SEQL\HookRegistry;
12
use SMW\DIWikiPage;
13
use SMW\DIProperty;
14
15
/**
16
 * @group semantic-external-query-lookup
17
 * @group medium
18
 *
19
 * @license GNU GPL v2+
20
 * @since 1.0
21
 *
22
 * @author mwjames
23
 */
24
class JsonParseOnFauxHttpResponseTest extends \PHPUnit_Framework_TestCase {
25
26
	protected function setUp() {
27
28
		$externalRepositoryEndpoints = array(
29
			'api-foo' => array(
30
				'http://example.org/index.php/$1',
31
				'http://example.org/api.php/',
32
				true
33
			)
34
		);
35
36
		$hookRegistry = new HookRegistry( array(
37
			'externalRepositoryEndpoints' => $externalRepositoryEndpoints
38
		) );
39
40
		$hookRegistry->register();
41
	}
42
43
	/**
44
	 * @dataProvider jsonFileProvider
45
	 */
46
	public function testQueryResultFetcherFromCannedJsonResponse( $file ) {
47
48
		$jsonFileReader = UtilityFactory::getInstance()->newJsonFileReader( $file );
49
		$content = $jsonFileReader->read();
50
51
		$instance = new JsonResponseParser(
52
			new DataValueDeserializer( $content['query-source'] )
53
		);
54
55
		$instance->doParse( $content['api-response'] );
56
57
		$this->assertSubjectList(
58
			$content['expected']['subjectList'],
59
			$instance->getResultSubjectList()
60
		);
61
62
		$this->assertEquals(
63
			$content['expected']['hasFurtherResults'],
64
			$instance->hasFurtherResults()
65
		);
66
67
		$this->assertPrintRequestPropertyList(
68
			$content['expected']['printRequestPropertyList'],
69
			$instance->getPrintRequestPropertyList()
70
		);
71
	}
72
73
	private function assertSubjectList( $expectedSubjectList, $subjectList ) {
74
75
		foreach ( $subjectList as $subject ) {
76
			foreach ( $expectedSubjectList as $key => $sub ) {
77
				$sub = DIWikiPage::doUnserialize( str_replace( " ", "_", $sub ) );
78
				if ( $subject->equals( $sub ) ) {
79
					unset( $expectedSubjectList[$key] );
80
					break;
81
				}
82
			}
83
		}
84
85
		$this->assertEmpty(
86
			$expectedSubjectList,
87
			'Failed because of a missing match: ' . implode( ',', $expectedSubjectList )
88
		);
89
	}
90
91
	private function assertPrintRequestPropertyList( $expectedPropertyList, $printRequestPropertyList ) {
92
93
		foreach ( $printRequestPropertyList as $property ) {
94
			foreach ( $expectedPropertyList as $key => $prop ) {
95
				$prop = new DIProperty( str_replace( " ", "_", $prop ) );
96
				if ( $property->equals( $prop ) ) {
97
					unset( $expectedPropertyList[$key] );
98
					break;
99
				}
100
			}
101
		}
102
103
		$this->assertEmpty(
104
			$expectedPropertyList,
105
			'Failed because of a missing match: ' . implode( ',', $expectedPropertyList )
106
		);
107
	}
108
109
	public function jsonFileProvider() {
110
111
		$provider = array();
112
		$location = __DIR__ . '/Fixtures';
113
114
		$bulkFileProvider = UtilityFactory::getInstance()->newBulkFileProvider( $location );
115
		$bulkFileProvider->searchByFileExtension( 'json' );
116
117
		foreach ( $bulkFileProvider->getFiles() as $file ) {
118
			$provider[] = array( $file );
119
		}
120
121
		return $provider;
122
	}
123
124
}
125