Completed
Pull Request — master (#146)
by Kenji
02:07
created

CIPHPUnitTestDouble::getDouble()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 6
nop 3
dl 0
loc 28
rs 8.5806
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  bool   $enable_constructor enable constructor 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
		$mock = $mock->setMethods($methods)->getMock();
52
53
		foreach ($params as $method => $return)
54
		{
55
			if (is_callable($return)) {
56
				$mock->expects($this->testCase->any())->method($method)
57
					->willReturnCallback($return);
58
			} else {
59
				$mock->expects($this->testCase->any())->method($method)
60
					->willReturn($return);
61
			}
62
		}
63
64
		return $mock;
65
	}
66
67
	protected function _verify($mock, $method, $params = null, $expects, $with)
68
	{
69
		$invocation = $mock->expects($expects)
70
			->method($method);
71
72
		$count = count($params);
73
74
		switch ($count) {
75
			case 0:
76
				break;
77
			case 1:
78
				$invocation->$with(
79
					$params[0]
80
				);
81
				break;
82
			case 2:
83
				$invocation->$with(
84
					$params[0], $params[1]
85
				);
86
				break;
87
			case 3:
88
				$invocation->$with(
89
					$params[0], $params[1], $params[2]
90
				);
91
				break;
92 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...
93
				$invocation->$with(
94
					$params[0], $params[1], $params[2], $params[3]
95
				);
96
				break;
97 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...
98
				$invocation->$with(
99
					$params[0], $params[1], $params[2], $params[3], $params[4]
100
				);
101
				break;
102
			default:
103
				throw new RuntimeException(
104
					'Sorry, ' . $count . ' params not implemented yet'
105
				);
106
		}
107
	}
108
109
	/**
110
	 * Verifies that method was called exactly $times times
111
	 *
112
	 * $loader->expects($this->exactly(2))
113
	 * 	->method('view')
114
	 * 	->withConsecutive(
115
	 *		['shop_confirm', $this->anything(), TRUE],
116
	 * 		['shop_tmpl_checkout', $this->anything()]
117
	 * 	);
118
	 *
119
	 *  will be
120
	 *
121
	 * $this->verifyInvokedMultipleTimes(
122
	 * 	$loader,
123
	 * 	'view',
124
	 * 	2,
125
	 * 	[
126
	 * 		['shop_confirm', $this->anything(), TRUE],
127
	 * 		['shop_tmpl_checkout', $this->anything()]
128
	 * 	]
129
	 * );
130
	 *
131
	 * @param object $mock   PHPUnit mock object
132
	 * @param string $method
133
	 * @param int    $times
134
	 * @param array  $params arguments
135
	 */
136
	public function verifyInvokedMultipleTimes($mock, $method, $times, $params = null)
137
	{
138
		$this->_verify(
139
			$mock, $method, $params, $this->testCase->exactly($times), 'withConsecutive'
140
		);
141
	}
142
143
	/**
144
	 * Verifies a method was invoked at least once
145
	 *
146
	 * @param object $mock   PHPUnit mock object
147
	 * @param string $method
148
	 * @param array  $params arguments
149
	 */
150
	public function verifyInvoked($mock, $method, $params = null)
151
	{
152
		$this->_verify(
153
			$mock, $method, $params, $this->testCase->atLeastOnce(), 'with'
154
		);
155
	}
156
157
	/**
158
	 * Verifies that method was invoked only once
159
	 *
160
	 * @param object $mock   PHPUnit mock object
161
	 * @param string $method
162
	 * @param array  $params arguments
163
	 */
164
	public function verifyInvokedOnce($mock, $method, $params = null)
165
	{
166
		$this->_verify(
167
			$mock, $method, $params, $this->testCase->once(), 'with'
168
		);
169
	}
170
171
	/**
172
	 * Verifies that method was not called
173
	 *
174
	 * @param object $mock   PHPUnit mock object
175
	 * @param string $method
176
	 * @param array  $params arguments
177
	 */
178
	public function verifyNeverInvoked($mock, $method, $params = null)
179
	{
180
		$this->_verify(
181
			$mock, $method, $params, $this->testCase->never(), 'with'
182
		);
183
	}
184
}
185