Completed
Push — master ( 93a702...008e2c )
by Alexander
03:47
created

TestSuiteFactory::setApplication()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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\IApplicationAware;
16
use aik099\PHPUnit\RemoteCoverage\RemoteCoverageHelper;
17
use aik099\PHPUnit\Session\SessionStrategyManager;
18
use aik099\PHPUnit\Application;
19
20
/**
21
 * Creates test suites based on test case class configuration.
22
 *
23
 * @method \Mockery\Expectation shouldReceive(string $name)
24
 */
25
class TestSuiteFactory implements IApplicationAware
26
{
27
28
	/**
29
	 * Session strategy manager.
30
	 *
31
	 * @var SessionStrategyManager
32
	 */
33
	private $_sessionStrategyManager;
34
35
	/**
36
	 * Application.
37
	 *
38
	 * @var Application
39
	 */
40
	protected $application;
41
42
	/**
43
	 * Browser configuration factory.
44
	 *
45
	 * @var IBrowserConfigurationFactory
46
	 */
47
	private $_browserConfigurationFactory;
48
49
	/**
50
	 * Remote coverage helper.
51
	 *
52
	 * @var RemoteCoverageHelper
53
	 */
54
	private $_remoteCoverageHelper;
55
56
	/**
57
	 * Creates test suite builder instance.
58
	 *
59
	 * @param SessionStrategyManager       $session_strategy_manager      Session strategy manager.
60
	 * @param IBrowserConfigurationFactory $browser_configuration_factory Browser configuration factory.
61
	 * @param RemoteCoverageHelper         $remote_coverage_helper        Remote coverage helper.
62
	 */
63 6
	public function __construct(
64
		SessionStrategyManager $session_strategy_manager,
65
		IBrowserConfigurationFactory $browser_configuration_factory,
66
		RemoteCoverageHelper $remote_coverage_helper
67
	) {
68 6
		$this->_sessionStrategyManager = $session_strategy_manager;
69 6
		$this->_browserConfigurationFactory = $browser_configuration_factory;
70 6
		$this->_remoteCoverageHelper = $remote_coverage_helper;
71 6
	}
72
73
	/**
74
	 * Sets application.
75
	 *
76
	 * @param Application $application The application.
77
	 *
78
	 * @return void
79
	 */
80 6
	public function setApplication(Application $application)
81
	{
82 6
		$this->application = $application;
83 6
	}
84
85
	/**
86
	 * Creates test suite based on given test case class.
87
	 *
88
	 * @param string $class_name Test case class name.
89
	 *
90
	 * @return AbstractTestSuite
91
	 */
92 6
	public function createSuiteFromTestCase($class_name)
93
	{
94
		/** @var RegularTestSuite $suite */
95 6
		$suite = $this->application->getObject('regular_test_suite');
96 6
		$suite->setName($class_name);
97
98 6
		$browsers = $this->_getBrowsers($class_name);
99
100 6
		if ( $browsers ) {
101
			// Create tests from test methods for multiple browsers.
102 3
			foreach ( $browsers as $browser ) {
103 3
				$suite->addTest($this->_createBrowserSuite($class_name, $browser));
104 3
			}
105 3
		}
106
		else {
107
			// Create tests from test methods for single browser.
108 3
			$suite->addTestMethods($class_name);
109 3
			$suite->setTestDependencies(
110 3
				$this->_sessionStrategyManager,
111 3
				$this->_browserConfigurationFactory,
112 3
				$this->_remoteCoverageHelper
113 3
			);
114
		}
115
116 6
		return $suite;
117
	}
118
119
	/**
120
	 * Returns browser configuration of a class.
121
	 *
122
	 * @param string $class_name Test case class name.
123
	 *
124
	 * @return array
125
	 */
126 6
	private function _getBrowsers($class_name)
127
	{
128 6
		$class = new \ReflectionClass($class_name);
129 6
		$static_properties = $class->getStaticProperties();
130
131 6
		return !empty($static_properties['browsers']) ? $static_properties['browsers'] : array();
132
	}
133
134
	/**
135
	 * Creates browser suite.
136
	 *
137
	 * @param string $class_name Descendant of TestCase class.
138
	 * @param array  $browser    Browser configuration.
139
	 *
140
	 * @return BrowserTestSuite
141
	 */
142 3
	private function _createBrowserSuite($class_name, array $browser)
143
	{
144
		/** @var BrowserTestSuite $suite */
145 3
		$suite = $this->application->getObject('browser_test_suite');
146 3
		$suite->setName($class_name . ': ' . $suite->nameFromBrowser($browser));
147
148 3
		$suite->addTestMethods($class_name);
149 3
		$suite->setTestDependencies(
150 3
			$this->_sessionStrategyManager,
151 3
			$this->_browserConfigurationFactory,
152 3
			$this->_remoteCoverageHelper
153 3
		);
154 3
		$suite->setBrowserFromConfiguration($browser);
155
156 3
		return $suite;
157
	}
158
159
}
160