-
-
Notifications
You must be signed in to change notification settings - Fork 192
Expand file tree
/
Copy pathBaseTestCase.php
More file actions
70 lines (63 loc) · 2.33 KB
/
Copy pathBaseTestCase.php
File metadata and controls
70 lines (63 loc) · 2.33 KB
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
<?php
declare(strict_types=1);
namespace Neos\Cache\Tests;
use PHPUnit\Framework\TestCase;
/*
* This file is part of the Neos.Cache package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
/**
* The mother of all test cases.
*
*/
abstract class BaseTestCase extends TestCase
{
/**
* @var array
*/
protected $backupGlobalsBlacklist = ['GLOBALS', 'bootstrap', '__PHPUNIT_BOOTSTRAP'];
/**
* Enable or disable the backup and restoration of static attributes.
* @var boolean
*/
protected $backupStaticAttributes = false;
/**
* Injects $dependency into property $name of $target
*
* This is a convenience method for setting a protected or private property in
* a test subject for the purpose of injecting a dependency.
*
* @param object $target The instance which needs the dependency
* @param string $name Name of the property to be injected
* @param mixed $dependency The dependency to inject – usually an object but can also be any other type
* @return void
* @throws \RuntimeException
* @throws \InvalidArgumentException
*/
protected function inject($target, $name, $dependency)
{
if (!is_object($target)) {
throw new \InvalidArgumentException('Wrong type for argument $target, must be object.');
}
$objectReflection = new \ReflectionObject($target);
$methodNamePart = strtoupper($name[0]) . substr($name, 1);
if ($objectReflection->hasMethod('set' . $methodNamePart)) {
$methodName = 'set' . $methodNamePart;
$target->$methodName($dependency);
} elseif ($objectReflection->hasMethod('inject' . $methodNamePart)) {
$methodName = 'inject' . $methodNamePart;
$target->$methodName($dependency);
} elseif ($objectReflection->hasProperty($name)) {
$property = $objectReflection->getProperty($name);
$property->setAccessible(true);
$property->setValue($target, $dependency);
} else {
throw new \RuntimeException('Could not inject ' . $name . ' into object of type ' . get_class($target));
}
}
}