summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticFormsSelect/tests/phpunit/Unit/ApiSemanticFormsSelectRequestProcessorTest.php
blob: 7c2b292b31c0b96714fcd37057821bc790501978 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php

namespace SFS\Tests;

use SFS\ApiSemanticFormsSelectRequestProcessor;
use ApiMain;
use RequestContext;
use WebRequest;
use FauxRequest;

/**
 * @covers  \SFS\ApiSemanticFormsSelectRequestProcessor
 * @group   semantic-forms-select
 *
 * @license GNU GPL v2+
 * @since   3.0.0
 *
 * @author  FelixAba
 */
class ApiSemanticFormsSelectRequestProcessorTest
	extends \PHPUnit_Framework_TestCase {

	private $ApiSFSRP;

	protected function setUp() {
		parent::setUp();
		$parser = $this->getMockBuilder( '\Parser' )
			->disableOriginalConstructor()->getMock();
		$this->ApiSFSRP = new ApiSemanticFormsSelectRequestProcessor( $parser );
	}

	protected function tearDown() {
		unset( $this->ApiSFSRP );
		parent::tearDown();
	}

	public function testCanConstruct() {
		$this->assertInstanceOf(
			'\SFS\ApiSemanticFormsSelectRequestProcessor', $this->ApiSFSRP
		);
	}

	public function testMissingParametersThrowsException() {

		$parameters = [];

		$this->setExpectedException( 'InvalidArgumentException' );
		$this->ApiSFSRP->getJsonDecodedResultValuesForRequestParameters(
			$parameters
		);
	}

	public function testJsonResultValuesFromRequestParameters() {

		$parameters = [ 'query' => 'foo', 'sep' => ',' ];

		$this->assertInternalType(
			'object',
			$this->ApiSFSRP->getJsonDecodedResultValuesForRequestParameters(
				$parameters
			)
		);
	}

	public function testJsonResultValuesFromRequestParameters_doProcessQueryFor(
	) {

		$parameters = [ 'approach' => 'smw', 'query' => 'foo, baa, gaah',
		                     'sep'      => ',' ];

		$this->assertInternalType(
			'object',
			$this->ApiSFSRP->getJsonDecodedResultValuesForRequestParameters(
				$parameters
			)
		);
	}

	public function testSetDebugFlag() {
		$this->ApiSFSRP->setDebugFlag( true );
		$parameters = [ 'query' => 'foo , function', 'sep' => ',' ];

		$this->assertInternalType(
			'object',
			$this->ApiSFSRP->getJsonDecodedResultValuesForRequestParameters(
				$parameters
			)
		);
	}

	public function testSetDebugFlag_doProcessQueryFor() {
		$this->ApiSFSRP->setDebugFlag( true );
		$parameters = [ 'approach' => 'smw', 'query' => 'my Query,query2',
		                     'sep'      => ',' ];

		$this->assertInternalType(
			'object',
			$this->ApiSFSRP->getJsonDecodedResultValuesForRequestParameters(
				$parameters
			)
		);
	}

	public function testGetFormattedValuesFrom() {
		$sep = ",";
		$values = "my Query,query2";
		$result = [ "", "my Query", "query2" ];
		$formattedValues = $this->invokeMethod(
			$this->ApiSFSRP, 'getFormattedValuesFrom', [ $sep, $values ]
		);
		$this->assertEquals( $result, $formattedValues );
	}

	/**
	 * Call protected/private method of a class.
	 *
	 * @param object &$object Instantiated object that we will run method on.
	 * @param string $methodName Method name to call
	 * @param array  $parameters Array of parameters to pass into method.
	 *
	 * @return mixed Method return.
	 */
	public function invokeMethod( &$object, $methodName,
		array $parameters = []
	) {
		$reflection = new \ReflectionClass( get_class( $object ) );
		$method = $reflection->getMethod( $methodName );
		$method->setAccessible( true );

		return $method->invokeArgs( $object, $parameters );
	}


}