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

AbstractTestSuite::setTestDependencies()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.074

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 30
ccs 10
cts 12
cp 0.8333
rs 8.5806
cc 4
eloc 20
nc 6
nop 4
crap 4.074
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\TestSuite;
12
13
14
use aik099\PHPUnit\BrowserConfiguration\IBrowserConfigurationFactory;
15
use aik099\PHPUnit\BrowserTestCase;
16
use aik099\PHPUnit\IEventDispatcherAware;
17
use aik099\PHPUnit\RemoteCoverage\RemoteCoverageHelper;
18
use aik099\PHPUnit\Session\SessionStrategyManager;
19
use ReflectionClass;
20
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
21
22
/**
23
 * Base Test Suite class for browser tests.
24
 *
25
 * @method \Mockery\Expectation shouldReceive(string $name)
26
 */
27
abstract class AbstractTestSuite extends \PHPUnit_Framework_TestSuite implements IEventDispatcherAware
28
{
29
30
	/**
31
	 * Event dispatcher.
32
	 *
33
	 * @var EventDispatcherInterface
34
	 */
35
	private $_eventDispatcher;
36
37
	/**
38
	 * Overriding the default: Selenium suites are always built from a TestCase class.
39
	 *
40
	 * @var boolean
41
	 */
42
	protected $testCase = true;
43
44
	/**
45
	 * Sets event dispatcher.
46
	 *
47
	 * @param EventDispatcherInterface $event_dispatcher Event dispatcher.
48
	 *
49
	 * @return void
50
	 */
51 13
	public function setEventDispatcher(EventDispatcherInterface $event_dispatcher)
52
	{
53 13
		$this->_eventDispatcher = $event_dispatcher;
54 13
	}
55
56
	/**
57
	 * Adds test methods to the suite.
58
	 *
59
	 * @param string $class_name Test case class name.
60
	 *
61
	 * @return self
62
	 */
63 3
	public function addTestMethods($class_name)
64
	{
65 3
		$class = new \ReflectionClass($class_name);
66
67 3
		foreach ( $class->getMethods(\ReflectionMethod::IS_PUBLIC) as $method ) {
68 3
			$this->addTestMethod($class, $method);
69
		}
70
71 3
		return $this;
72
	}
73
74
	/**
75
	 * Sets session strategy manager recursively to all tests.
76
	 *
77
	 * @param SessionStrategyManager       $session_strategy_manager      Session strategy manager.
78
	 * @param IBrowserConfigurationFactory $browser_configuration_factory Browser configuration factory.
79
	 * @param RemoteCoverageHelper         $remote_coverage_helper        Remote coverage helper.
80
	 * @param array                        $tests                         Tests to process.
81
	 *
82
	 * @return self
83
	 */
84 3
	public function setTestDependencies(
85
		SessionStrategyManager $session_strategy_manager,
86
		IBrowserConfigurationFactory $browser_configuration_factory,
87
		RemoteCoverageHelper $remote_coverage_helper,
88
		array $tests = null
89
	) {
90 3
		if ( !isset($tests) ) {
91 3
			$tests = $this->tests();
92
		}
93
94 3
		foreach ( $tests as $test ) {
95 3
			if ( $test instanceof \PHPUnit_Framework_TestSuite_DataProvider ) {
96
				$this->setTestDependencies(
97
					$session_strategy_manager,
98
					$browser_configuration_factory,
99
					$remote_coverage_helper,
100
					$test->tests()
101
				);
102
			}
103
			else {
104
				/** @var BrowserTestCase $test */
105 3
				$test->setEventDispatcher($this->_eventDispatcher);
106 3
				$test->setSessionStrategyManager($session_strategy_manager);
107 3
				$test->setBrowserConfigurationFactory($browser_configuration_factory);
108 3
				$test->setRemoteCoverageHelper($remote_coverage_helper);
109
			}
110
		}
111
112 3
		return $this;
113
	}
114
115
	/**
116
	 * Report back suite ending to each it's test.
117
	 *
118
	 * @param array $tests Tests to process.
119
	 *
120
	 * @return void
121
	 */
122
	protected function tearDown(array $tests = null)
123
	{
124
		if ( !isset($tests) ) {
125
			$tests = $this->tests();
126
		}
127
128
		foreach ( $tests as $test ) {
129
			if ( $test instanceof \PHPUnit_Framework_TestSuite_DataProvider ) {
130
				$this->tearDown($test->tests());
131
			}
132
			else {
133
				/* @var $test BrowserTestCase */
134
				$test->onTestSuiteEnded();
135
			}
136
		}
137
	}
138
139
	/**
140
	 * Indicates end of the test suite.
141
	 *
142
	 * @return void
143
	 *
144
	 * @codeCoverageIgnore
145
	 */
146
	public function onTestSuiteEnded()
147
	{
148
		// Method created just to simplify tearDown method.
149
	}
150
151
}
152