Completed
Pull Request — master (#130)
by
unknown
02:41
created

CIPHPUnitTestDouble::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Part of ci-phpunit-test
4
 *
5
 * @author     Kenji Suzuki <https://github.com/kenjis>
6
 * @license    MIT License
7
 * @copyright  2015 Kenji Suzuki
8
 * @link       https://github.com/kenjis/ci-phpunit-test
9
 */
10
11
class CIPHPUnitTestDouble
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
12
{
13
	protected $testCase;
14
15
	public function __construct(PHPUnit_Framework_TestCase $testCase)
16
	{
17
		$this->testCase = $testCase;
18
	}
19
20
	/**
21
	 * Get Mock Object
22
	 *
23
	 * $email = $this->getMockBuilder('CI_Email')
24
	 *	->disableOriginalConstructor()
25
	 *	->setMethods(['send'])
26
	 *	->getMock();
27
	 * $email->method('send')->willReturn(TRUE);
28
	 *
29
	 *  will be
30
	 *
31
	 * $email = $this->getDouble('CI_Email', ['send' => TRUE]);
32
	 *
33
	 * @param  string $classname
34
	 * @param  array  $params             [method_name => return_value]
35
	 * @param  mixed   $enable_constructor enable constructor and args or not
36
	 * @return object PHPUnit mock object
37
	 */
38
	public function getDouble($classname, $params, $enable_constructor = false)
39
	{
40
		$methods = array_keys($params);
41
42
		// `disableOriginalConstructor()` is the default, because if we call
43
		// construnctor, it may call `$this->load->...` or other CodeIgniter
44
		// methods in it. But we can't use them in
45
		// `$this->request->setCallablePreConstructor()`
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
46
		$mock = $this->testCase->getMockBuilder($classname);
47
		if (! $enable_constructor)
48
		{
49
			$mock->disableOriginalConstructor();
50
		}
51
		elseif (is_array($enable_constructor))
52
		{
53
			$mock->setConstructorArgs($enable_constructor);
54
		}
55
		$mock = $mock->setMethods($methods)->getMock();
56
57
		foreach ($params as $method => $return)
58
		{
59
			$mock->expects($this->testCase->any())->method($method)
60
				->willReturn($return);
61
		}
62
63
		return $mock;
64
	}
65
66
	protected function _verify($mock, $method, $params = null, $expects, $with)
67
	{
68
		$invocation = $mock->expects($expects)
69
			->method($method);
70
71
		$count = count($params);
72
73
		switch ($count) {
74
			case 0:
75
				break;
76
			case 1:
77
				$invocation->$with(
78
					$params[0]
79
				);
80
				break;
81
			case 2:
82
				$invocation->$with(
83
					$params[0], $params[1]
84
				);
85
				break;
86
			case 3:
87
				$invocation->$with(
88
					$params[0], $params[1], $params[2]
89
				);
90
				break;
91 View Code Duplication
			case 4:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
				$invocation->$with(
93
					$params[0], $params[1], $params[2], $params[3]
94
				);
95
				break;
96 View Code Duplication
			case 5:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
				$invocation->$with(
98
					$params[0], $params[1], $params[2], $params[3], $params[4]
99
				);
100
				break;
101
			default:
102
				throw new RuntimeException(
103
					'Sorry, ' . $count . ' params not implemented yet'
104
				);
105
		}
106
	}
107
108
	/**
109
	 * Verifies that method was called exactly $times times
110
	 *
111
	 * $loader->expects($this->exactly(2))
112
	 * 	->method('view')
113
	 * 	->withConsecutive(
114
	 *		['shop_confirm', $this->anything(), TRUE],
115
	 * 		['shop_tmpl_checkout', $this->anything()]
116
	 * 	);
117
	 *
118
	 *  will be
119
	 *
120
	 * $this->verifyInvokedMultipleTimes(
121
	 * 	$loader,
122
	 * 	'view',
123
	 * 	2,
124
	 * 	[
125
	 * 		['shop_confirm', $this->anything(), TRUE],
126
	 * 		['shop_tmpl_checkout', $this->anything()]
127
	 * 	]
128
	 * );
129
	 *
130
	 * @param object $mock   PHPUnit mock object
131
	 * @param string $method
132
	 * @param int    $times
133
	 * @param array  $params arguments
134
	 */
135
	public function verifyInvokedMultipleTimes($mock, $method, $times, $params = null)
136
	{
137
		$this->_verify(
138
			$mock, $method, $params, $this->testCase->exactly($times), 'withConsecutive'
139
		);
140
	}
141
142
	/**
143
	 * Verifies a method was invoked at least once
144
	 *
145
	 * @param object $mock   PHPUnit mock object
146
	 * @param string $method
147
	 * @param array  $params arguments
148
	 */
149
	public function verifyInvoked($mock, $method, $params = null)
150
	{
151
		$this->_verify(
152
			$mock, $method, $params, $this->testCase->atLeastOnce(), 'with'
153
		);
154
	}
155
156
	/**
157
	 * Verifies that method was invoked only once
158
	 *
159
	 * @param object $mock   PHPUnit mock object
160
	 * @param string $method
161
	 * @param array  $params arguments
162
	 */
163
	public function verifyInvokedOnce($mock, $method, $params = null)
164
	{
165
		$this->_verify(
166
			$mock, $method, $params, $this->testCase->once(), 'with'
167
		);
168
	}
169
170
	/**
171
	 * Verifies that method was not called
172
	 *
173
	 * @param object $mock   PHPUnit mock object
174
	 * @param string $method
175
	 * @param array  $params arguments
176
	 */
177
	public function verifyNeverInvoked($mock, $method, $params = null)
178
	{
179
		$this->_verify(
180
			$mock, $method, $params, $this->testCase->never(), 'with'
181
		);
182
	}
183
}
184