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
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\Marketplace\tests\Framework\Mock;
use Piwik\Filesystem;
class Service extends \Piwik\Plugins\Marketplace\Api\Service {
public $action;
public $params;
private $fixtureToReturn;
private $exception;
private $onFetchCallback;
private $onDownloadCallback;
public function __construct()
{
parent::__construct('http://plugins.piwik.org');
}
/**
* Will cause the service to throw an exception when any data is fetched
* @param $exception
*/
public function throwException($exception)
{
$this->exception = $exception;
$this->fixtureToReturn = null;
}
/**
* Will cause the service to use the content of the given fixture as a response of the plugins API.
* Should be either a filename of a file within the "plugins/Marketplace/tests/resources/" directory or
* an array of filenames. An array is useful if the service gets called multiple times and you want to return
* different results for each API call. If an array is given, first filename will be returned first, then next, ...
*
* @param string|array $fixtureName
*/
public function returnFixture($fixtureName)
{
$this->fixtureToReturn = $fixtureName;
$this->exception = null;
}
public function download($url, $destinationPath = null, $timeout = null)
{
if ($this->onDownloadCallback && is_callable($this->onDownloadCallback)) {
$result = call_user_func($this->onDownloadCallback, $this->action, $this->params);
if (!empty($result)) {
return $result;
}
}
if ($destinationPath) {
Filesystem::mkdir(@dirname($destinationPath));
file_put_contents($destinationPath, $url);
return true;
}
if (!empty($this->fixtureToReturn)) {
if (is_array($this->fixtureToReturn)) {
$fixture = array_shift($this->fixtureToReturn);
} else {
$fixture = $this->fixtureToReturn;
$this->fixtureToReturn = null;
}
return $this->getFixtureContent($fixture);
}
}
public function getFixtureContent($fixture)
{
$path = PIWIK_INCLUDE_PATH . '/plugins/Marketplace/tests/resources/' . $fixture;
return file_get_contents($path);
}
// here you can set a custom callback and record all actions/ params and even return a custom result for each
// action / params if wanted
public function setOnFetchCallback($callback)
{
$this->onFetchCallback = $callback;
}
// here you can set a custom callback and record all actions/ params and even return a custom result for each
// action / params if wanted
public function setOnDownloadCallback($callback)
{
$this->onDownloadCallback = $callback;
}
public function fetch($action, $params)
{
$this->action = $action;
$this->params = $params;
if ($this->onFetchCallback && is_callable($this->onFetchCallback)) {
$result = call_user_func($this->onFetchCallback, $action, $params);
if (!empty($result)) {
return $result;
}
}
if (isset($this->exception)) {
throw $this->exception;
} elseif (!empty($this->fixtureToReturn) || $this->onDownloadCallback) {
// we want to make sure to test as much of the service class as possible.
// Therefore we only mock the HTTP request in download()
return parent::fetch($action, $params);
}
return array();
}
}
|