Completed
Push — 2.0-dev ( 13430b...59c481 )
by George
07:19
created

Mocker::createMockItem()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 33
rs 8.8571
cc 3
eloc 19
nc 1
nop 0
1
<?php
2
/**
3
 * @copyright  Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
4
 * @license    GNU General Public License version 2 or later; see LICENSE
5
 */
6
7
namespace Joomla\Cache\Tests;
8
9
use Joomla\Test\TestHelper;
10
11
/**
12
 * Class to mock Joomla\Mocker\Cache.
13
 *
14
 * @since  1.0
15
 */
16
class Mocker
17
{
18
	/**
19
	 * @var    \PHPUnit_Framework_TestCase
20
	 * @since  1.0
21
	 */
22
	private $test;
23
24
	/**
25
	 * Class contructor.
26
	 *
27
	 * @param   \PHPUnit_Framework_TestCase  $test  A test class.
28
	 *
29
	 * @since   1.0
30
	 */
31
	public function __construct(\PHPUnit_Framework_TestCase $test)
32
	{
33
		$this->test = $test;
34
	}
35
36
	/**
37
	 * Creates and instance of a mock Joomla\Mocker\Cache object.
38
	 *
39
	 * @return  object
40
	 *
41
	 * @since   1.0
42
	 */
43
	public function createMockCache()
44
	{
45
		// Collect all the relevant methods in JDatabase.
46
		$methods = array(
47
			'clear',
48
			'hasItem',
49
			'getItem',
50
			'getItems',
51
			'deleteItem',
52
			'deleteItems',
53
			'save'
54
		);
55
56
		// Create the mock.
57
		$mockObject = $this->test->getMock(
58
			'Joomla\Cache\Cache',
59
			$methods,
60
			// Constructor arguments.
61
			array(),
62
			// Mock class name.
63
			'',
64
			// Call original constructor.
65
			false
66
		);
67
68
		TestHelper::assignMockCallbacks(
69
			$mockObject,
70
			$this->test,
71
			array(
72
				'getItem' => array((is_callable(array($this->test, 'mockCacheGetItem')) ? $this->test : $this), 'mockCacheGetItem'),
73
			)
74
		);
75
76
		return $mockObject;
77
	}
78
79
	/**
80
	 * Creates and instance of a mock Joomla\Cache\Item object.
81
	 *
82
	 * @return  object
83
	 *
84
	 * @since   1.0
85
	 */
86
	public function createMockItem()
87
	{
88
		// Collect all the relevant methods in JDatabase.
89
		$methods = array(
90
			'getKey',
91
			'get',
92
			'isHit',
93
			'set',
94
		);
95
96
		// Create the mock.
97
		$mockObject = $this->test->getMock(
98
			'Joomla\Cache\Item\Item',
99
			$methods,
100
			// Constructor arguments.
101
			array(),
102
			// Mock class name.
103
			'',
104
			// Call original constructor.
105
			false
106
		);
107
108
		TestHelper::assignMockCallbacks(
109
			$mockObject,
110
			$this->test,
111
			array(
112
				'get' => array((is_callable(array($this->test, 'mockCacheItemGet')) ? $this->test : $this), 'mockCacheItemGet'),
113
				'isHit' => array((is_callable(array($this->test, 'mockCacheItemIsHit')) ? $this->test : $this), 'mockCacheItemIsHit'),
114
			)
115
		);
116
117
		return $mockObject;
118
	}
119
120
	/**
121
	 * Callback to mock the Joomla\Cache\Cache::get method.
122
	 *
123
	 * @param   string  $key  The input text.
124
	 *
125
	 * @return  string
126
	 *
127
	 * @since   1.0
128
	 */
129
	public function mockCacheGetItem($key)
130
	{
131
		return $this->createMockItem();
132
	}
133
134
	/**
135
	 * Callback to mock the Joomla\Cache\Item::getValue method.
136
	 *
137
	 * @return  string
138
	 *
139
	 * @since   1.0
140
	 */
141
	public function mockCacheItemGet()
142
	{
143
		return 'value';
144
	}
145
146
	/**
147
	 * Callback to mock the Joomla\Cache\Item::isHit method.
148
	 *
149
	 * @return  boolean
150
	 *
151
	 * @since   1.0
152
	 */
153
	public function mockCacheItemIsHit()
154
	{
155
		return false;
156
	}
157
}
158