Completed
Pull Request — master (#78)
by Christophe
03:11
created

Application::__clone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * This file is part of the phpunit-mink library.
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 *
7
 * @copyright Alexander Obuhovich <[email protected]>
8
 * @link      https://github.com/aik099/phpunit-mink
9
 */
10
11
namespace aik099\PHPUnit;
12
13
14
use aik099\PHPUnit\TestSuite\TestSuiteFactory;
15
16
/**
17
 * Main application class.
18
 *
19
 * @method \Mockery\Expectation shouldReceive(string $name)
20
 */
21
class Application
22
{
23
24
	/**
25
	 * Dependency injection container.
26
	 *
27
	 * @var DIContainer
28
	 */
29
	protected $container;
30
31
	/**
32
	 * Returns instance of strategy manager.
33
	 *
34
	 * @param DIContainer $container Dependency injection container.
35
	 *
36
	 * @return self
37
	 */
38 3
	public static function getInstance(DIContainer $container = null)
39
	{
40 3
		static $instance = null;
41
42 3
		if ( null === $instance ) {
43
			$instance = new static($container);
44
		}
45
46 3
		return $instance;
47
	}
48
49
	/**
50
	 * Prevents direct instantiation.
51
	 *
52
	 * @param DIContainer $container Dependency injection container.
53
	 */
54 19
	public function __construct(DIContainer $container = null)
55
	{
56 19
		if ( !isset($container) ) {
57 19
			$container = new DIContainer();
58
		}
59
60 19
		$this->container = $container;
61 19
		$this->container->setApplication($this);
62 19
	}
63
64
	/**
65
	 * Returns test suite builder.
66
	 *
67
	 * @return TestSuiteFactory
68
	 * @see    BrowserTestCase::suite()
69
	 */
70 3
	public function getTestSuiteFactory()
71
	{
72 3
		return $this->getObject('test_suite_factory');
73
	}
74
75
	/**
76
	 * Returns object from the container.
77
	 *
78
	 * @param string $service_id Name of the object in the container.
79
	 *
80
	 * @return \stdClass
81
	 */
82 5
	public function getObject($service_id)
83
	{
84 5
		return $this->container[$service_id];
85
	}
86
87
	/**
88
	 * Replaces object in the container.
89
	 *
90
	 * @param string   $service_id Name of the object in the container.
91
	 * @param callable $callable   The callable that will return the object.
92
	 * @param boolean  $is_factory The callable should be considered as a factory.
93
	 *
94
	 * @return callable Previous service version.
95
	 * @throws \InvalidArgumentException When attempt is made to replace non-existing service.
96
	 */
97 2
	public function replaceObject($service_id, $callable, $is_factory = false)
98
	{
99 2
		if ( !isset($this->container[$service_id]) ) {
100 1
			throw new \InvalidArgumentException('Service "' . $service_id . '" not found');
101
		}
102
103 1
		$backup = $this->container->raw($service_id);
104 1
		unset($this->container[$service_id]);
105 1
		$this->container[$service_id] = $is_factory ? $this->container->factory($callable) : $callable;
106
107 1
		return $backup;
108
	}
109
110
	/**
111
	 * Prevents cloning.
112
	 *
113
	 * @return void
114
	 *
115
	 * @codeCoverageIgnore
116
	 */
117
	private function __clone()
118
	{
119
120
	}
121
122
}
123