-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathAbstractLoader.php
265 lines (236 loc) · 9.22 KB
/
AbstractLoader.php
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
namespace PHPCR\Test;
use PHPCR\NoSuchWorkspaceException;
use PHPCR\RepositoryFactoryInterface;
/**
* Base class for the bootstrapping to load your phpcr implementation for the
* test suite.
*
* See the README file Bootstrapping section for an introduction how this works
*/
abstract class AbstractLoader
{
protected $factoryclass;
protected $workspacename;
protected $otherWorkspacename;
/**
* array with chapter names to skip all test cases in (without the numbers)
*/
protected $unsupportedChapters = array();
/**
* array in the format Chapter\FeatureTest with all cases to skip
*/
protected $unsupportedCases = array();
/**
* array in the format Chapter\FeatureTest::testName with all single tests to skip
*/
protected $unsupportedTests = array();
/**
* Create the loader
*
* @param string $factoryclass the class name of your implementations
* RepositoryFactory. You can pass null but then you must overwrite
* the getRepository method.
* @param string $workspacename the workspace to use for the tests, defaults to 'tests'
* @param string $otherWorkspacename name of second workspace, defaults to 'testsAdditional'
* Needed to test certain operations, such as clone, that span workspaces.
*/
protected function __construct($factoryclass, $workspacename = 'tests', $otherWorkspacename = 'testsAdditional')
{
$this->factoryclass = $factoryclass;
$this->workspacename = $workspacename;
$this->otherWorkspacename = $otherWorkspacename;
}
/**
* The loader is a singleton.
*
* Implement this class to return an ImplementationLoader instance
* configured to provide things from your implementation.
*
* @return AbstractLoader loader for your implementation
*/
public static function getInstance()
{
throw new \Exception('You need to overwrite this method, but php does not allow to declare it abstract.');
}
/**
* @return string classname of the repository factory
*/
public function getRepositoryFactoryClass()
{
return $this->factoryclass;
}
/**
* @return array hashmap with the parameters for the repository factory
*/
public abstract function getRepositoryFactoryParameters();
/**
* You should overwrite this to instantiate the repository without the
* factory.
*
* The default implementation uses the factory, but if the factory has an
* error, you will get failing tests all over.
*
* @return \PHPCR\RepositoryInterface the repository instance
*/
public function getRepository()
{
$factoryclass = $this->getRepositoryFactoryClass();
$factory = new $factoryclass;
if (! $factory instanceof RepositoryFactoryInterface) {
throw new \Exception("$factoryclass is not of type RepositoryFactoryInterface");
}
/** @var $factory RepositoryFactoryInterface */
return $factory->getRepository($this->getRepositoryFactoryParameters());
}
/**
* @return \PHPCR\CredentialsInterface the login credentials that lead to successful login into the repository
*/
public abstract function getCredentials();
/**
* @return \PHPCR\CredentialsInterface the login credentials that lead to login failure
*/
public abstract function getInvalidCredentials();
/**
* Used when impersonating another user in Reading\SessionReadMethodsTests::testImpersonate
* And for Reading\SessionReadMethodsTest::testCheckPermissionAccessControlException
*
* The user may not have write access to /tests_general_base/numberPropertyNode/jcr:content/foo
*
* @return \PHPCR\CredentialsInterface the login credentials with limited permissions for testing impersonate and access control
*/
public abstract function getRestrictedCredentials();
/**
* @return string the user id that is used in the credentials
*/
public abstract function getUserId();
/**
* Make the repository ready for login with null credentials, handling the
* case where authentication is passed outside the login method.
*
* If the implementation does not support this feature, it must return
* false for this method, otherwise true.
*
* @return boolean true if anonymous login is supposed to work
*/
public abstract function prepareAnonymousLogin();
/**
* @return string the workspace name used for the tests
*/
public function getWorkspaceName()
{
return $this->workspacename;
}
/**
* @return string name of the default workspace of this repository
*/
public function getDefaultWorkspaceName()
{
return 'default';
}
/**
* @return string the additional workspace name used for tests that need it
*/
public function getOtherWorkspaceName()
{
return $this->otherWorkspacename;
}
/**
* Get a session for this implementation.
*
* @param \PHPCR\CredentialsInterface $credentials The credentials to log into the repository. If omitted, self::getCredentials should be used
* @return \PHPCR\SessionInterface the session resulting from logging into the repository with the provided credentials
*/
public function getSession($credentials = false)
{
return $this->getSessionForWorkspace($credentials, $this->getWorkspaceName());
}
/**
* Get a session corresponding to the additional workspace for this implementation.
*
* @param \PHPCR\CredentialsInterface $credentials The credentials to log into the repository. If omitted, self::getCredentials should be used
* @return \PHPCR\SessionInterface the session resulting from logging into the repository with the provided credentials
*/
public function getAdditionalSession($credentials = false)
{
return $this->getSessionForWorkspace($credentials, $this->getOtherWorkspaceName());
}
/**
* If the implementation can automatically update mix:lastModified nodes,
* this should return a session configured to do that.
*
* Otherwise, the test regarding this feature is skipped.
*
* @return \PHPCR\SessionInterface
*
* @throws \PHPUnit_Framework_SkippedTestSuiteError to make whole test
* suite skip if implementation does not support updating the
* properties automatically.
*/
public function getSessionWithLastModified()
{
if ($this->doesSessionLastModified()) {
return $this->getSession();
}
throw new \PHPUnit_Framework_SkippedTestSuiteError('Not supported');
}
/**
* The implementation loader should provide a session that does not update
* mix:lastModified. If that is not possible, this method should return
* true, which will skip the test about this feature.
*
* @return boolean
*/
public function doesSessionLastModified()
{
return false;
}
/**
* Decide whether this test can be executed.
*
* The default implementation uses the unsupported... arrays to decide.
* Overwrite if you need a different logic.
*
* @param string $chapter the chapter name (folder name without number, i.e. Writing)
* @param string $case the test case full class name but without PHPCR\Tests , i.e. Writing\CopyMethodsTest
* @param string $name the test name as returned by TestCase::getName(), i.e. Writing\CopyMethodsTest::testCopyUpdateOnCopy - is null when checking for general support of test case in setupBeforeClass
*
* @return bool true if the implementation supports the features of this test
*/
public function getTestSupported($chapter, $case, $name)
{
return ! ( in_array($chapter, $this->unsupportedChapters)
|| in_array($case, $this->unsupportedCases)
|| in_array($name, $this->unsupportedTests)
);
}
/**
* @return \PHPCR\Test\FixtureLoaderInterface implementation that is used to load test fixtures
*/
public abstract function getFixtureLoader();
/**
* @param $credentials
* @param $workspaceName
* @return mixed
*/
private function getSessionForWorkspace($credentials, $workspaceName)
{
$repository = $this->getRepository();
if (false === $credentials) {
$credentials = $this->getCredentials();
}
try {
return $repository->login($credentials, $workspaceName);
} catch (NoSuchWorkspaceException $e) {
$adminRepository = $this->getRepository(); // get a new repository to log into
$session = $adminRepository->login($this->getCredentials(), 'default');
$workspace = $session->getWorkspace();
if (in_array($workspaceName, $workspace->getAccessibleWorkspaceNames())) {
throw new \Exception(sprintf('Workspace "%s" already exists but could not login to it', $workspaceName), null, $e);
}
$workspace->createWorkspace($workspaceName);
$repository = $this->getRepository(); // get a new repository to log into
return $repository->login($credentials, $workspaceName);
}
}
}