Completed
Push — master ( 799b5e...a63a8d )
by Kenji
12s
created

CIPHPUnitTestDouble   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 148
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C getDouble() 0 35 8
A _verify() 0 7 1
A verifyInvokedMultipleTimes() 0 6 1
A verifyInvoked() 0 6 1
A verifyInvokedOnce() 0 6 1
A verifyNeverInvoked() 0 6 1
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  $constructor_params false: disable construntor, array: construntor params
36
	 * 
37
	 * @return object PHPUnit mock object
38
	 */
39
	public function getDouble($classname, $params, $constructor_params = false)
40
	{
41
		$methods = array_keys($params);
42
43
		// `disableOriginalConstructor()` is the default, because if we call
44
		// construnctor, it may call `$this->load->...` or other CodeIgniter
45
		// methods in it. But we can't use them in
46
		// `$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...
47
		$mock = $this->testCase->getMockBuilder($classname);
48
		if ($constructor_params === false)
49
		{
50
			$mock->disableOriginalConstructor();
51
		}
52
		elseif (is_array($constructor_params))
53
		{
54
			$mock->setConstructorArgs($constructor_params);
55
		}
56
		$mock = $mock->setMethods($methods)->getMock();
57
58
		foreach ($params as $method => $return)
59
		{
60
			if (is_object($return) && $return instanceof PHPUnit_Framework_MockObject_Stub) {
0 ignored issues
show
Bug introduced by
The class PHPUnit_Framework_MockObject_Stub does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
61
				$mock->expects($this->testCase->any())->method($method)
62
					->will($return);
63
			} elseif (is_object($return) && $return instanceof Closure) {
64
				$mock->expects($this->testCase->any())->method($method)
65
					->willReturnCallback($return);
66
			} else {
67
				$mock->expects($this->testCase->any())->method($method)
68
					->willReturn($return);
69
			}
70
		}
71
72
		return $mock;
73
	}
74
75
	protected function _verify($mock, $method, $params = null, $expects, $with)
76
	{
77
		$invocation = $mock->expects($expects)
78
			->method($method);
79
80
		call_user_func_array([$invocation, $with], $params);
81
	}
82
83
	/**
84
	 * Verifies that method was called exactly $times times
85
	 *
86
	 * $loader->expects($this->exactly(2))
87
	 * 	->method('view')
88
	 * 	->withConsecutive(
89
	 *		['shop_confirm', $this->anything(), TRUE],
90
	 * 		['shop_tmpl_checkout', $this->anything()]
91
	 * 	);
92
	 *
93
	 *  will be
94
	 *
95
	 * $this->verifyInvokedMultipleTimes(
96
	 * 	$loader,
97
	 * 	'view',
98
	 * 	2,
99
	 * 	[
100
	 * 		['shop_confirm', $this->anything(), TRUE],
101
	 * 		['shop_tmpl_checkout', $this->anything()]
102
	 * 	]
103
	 * );
104
	 *
105
	 * @param object $mock   PHPUnit mock object
106
	 * @param string $method
107
	 * @param int    $times
108
	 * @param array  $params arguments
109
	 */
110
	public function verifyInvokedMultipleTimes($mock, $method, $times, $params = null)
111
	{
112
		$this->_verify(
113
			$mock, $method, $params, $this->testCase->exactly($times), 'withConsecutive'
114
		);
115
	}
116
117
	/**
118
	 * Verifies a method was invoked at least once
119
	 *
120
	 * @param object $mock   PHPUnit mock object
121
	 * @param string $method
122
	 * @param array  $params arguments
123
	 */
124
	public function verifyInvoked($mock, $method, $params = null)
125
	{
126
		$this->_verify(
127
			$mock, $method, $params, $this->testCase->atLeastOnce(), 'with'
128
		);
129
	}
130
131
	/**
132
	 * Verifies that method was invoked only once
133
	 *
134
	 * @param object $mock   PHPUnit mock object
135
	 * @param string $method
136
	 * @param array  $params arguments
137
	 */
138
	public function verifyInvokedOnce($mock, $method, $params = null)
139
	{
140
		$this->_verify(
141
			$mock, $method, $params, $this->testCase->once(), 'with'
142
		);
143
	}
144
145
	/**
146
	 * Verifies that method was not called
147
	 *
148
	 * @param object $mock   PHPUnit mock object
149
	 * @param string $method
150
	 * @param array  $params arguments
151
	 */
152
	public function verifyNeverInvoked($mock, $method, $params = null)
153
	{
154
		$this->_verify(
155
			$mock, $method, $params, $this->testCase->never(), 'with'
156
		);
157
	}
158
}
159