Skip to content

Commit d37c113

Browse files
author
Alexander Obuhovich
committed
Support for Data Providers
Closes #25
1 parent cf45cfb commit d37c113

3 files changed

Lines changed: 99 additions & 15 deletions

File tree

library/aik099/PHPUnit/TestSuite/AbstractTestSuite.php

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,37 @@ public function addTestMethods($class_name)
7777
* @param SessionStrategyManager $session_strategy_manager Session strategy manager.
7878
* @param IBrowserConfigurationFactory $browser_configuration_factory Browser configuration factory.
7979
* @param RemoteCoverageHelper $remote_coverage_helper Remote coverage helper.
80+
* @param array $tests Tests to process.
8081
*
8182
* @return self
8283
*/
8384
public function setTestDependencies(
8485
SessionStrategyManager $session_strategy_manager,
8586
IBrowserConfigurationFactory $browser_configuration_factory,
86-
RemoteCoverageHelper $remote_coverage_helper
87+
RemoteCoverageHelper $remote_coverage_helper,
88+
array $tests = null
8789
)
8890
{
89-
/* @var $test BrowserTestCase */
90-
foreach ( $this->tests() as $test ) {
91-
$test->setEventDispatcher($this->_eventDispatcher);
92-
$test->setSessionStrategyManager($session_strategy_manager);
93-
$test->setBrowserConfigurationFactory($browser_configuration_factory);
94-
$test->setRemoteCoverageHelper($remote_coverage_helper);
91+
if ( !isset($tests) ) {
92+
$tests = $this->tests();
93+
}
94+
95+
foreach ( $tests as $test ) {
96+
if ( $test instanceof \PHPUnit_Framework_TestSuite_DataProvider ) {
97+
$this->setTestDependencies(
98+
$session_strategy_manager,
99+
$browser_configuration_factory,
100+
$remote_coverage_helper,
101+
$test->tests()
102+
);
103+
}
104+
else {
105+
/** @var BrowserTestCase $test */
106+
$test->setEventDispatcher($this->_eventDispatcher);
107+
$test->setSessionStrategyManager($session_strategy_manager);
108+
$test->setBrowserConfigurationFactory($browser_configuration_factory);
109+
$test->setRemoteCoverageHelper($remote_coverage_helper);
110+
}
95111
}
96112

97113
return $this;
@@ -100,14 +116,24 @@ public function setTestDependencies(
100116
/**
101117
* Report back suite ending to each it's test.
102118
*
119+
* @param array $tests Tests to process.
120+
*
103121
* @return void
104122
*/
105-
protected function tearDown()
123+
protected function tearDown(array $tests = null)
106124
{
107-
/* @var $test BrowserTestCase */
125+
if ( !isset($tests) ) {
126+
$tests = $this->tests();
127+
}
108128

109-
foreach ( $this->tests() as $test ) {
110-
$test->onTestSuiteEnded();
129+
foreach ( $tests as $test ) {
130+
if ( $test instanceof \PHPUnit_Framework_TestSuite_DataProvider ) {
131+
$this->tearDown($test->tests());
132+
}
133+
else {
134+
/* @var $test BrowserTestCase */
135+
$test->onTestSuiteEnded();
136+
}
111137
}
112138
}
113139

library/aik099/PHPUnit/TestSuite/BrowserTestSuite.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,24 @@ public function nameFromBrowser(array $browser)
4343
* Sets given browser to be used in each underlying test cases and test suites.
4444
*
4545
* @param array $browser Browser configuration.
46+
* @param array $tests Tests to process.
4647
*
4748
* @return self
4849
*/
49-
public function setBrowserFromConfiguration(array $browser)
50+
public function setBrowserFromConfiguration(array $browser, array $tests = null)
5051
{
51-
/* @var $test BrowserTestCase */
52-
foreach ( $this->tests() as $test ) {
53-
$test->setBrowserFromConfiguration($browser);
52+
if ( !isset($tests) ) {
53+
$tests = $this->tests();
54+
}
55+
56+
foreach ( $tests as $test ) {
57+
if ( $test instanceof \PHPUnit_Framework_TestSuite_DataProvider ) {
58+
$this->setBrowserFromConfiguration($browser, $test->tests());
59+
}
60+
else {
61+
/* @var $test BrowserTestCase */
62+
$test->setBrowserFromConfiguration($browser);
63+
}
5464
}
5565

5666
return $this;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 <aik.bold@gmail.com>
8+
* @link https://github.com/aik099/phpunit-mink
9+
*/
10+
11+
namespace tests\aik099\PHPUnit\Integration;
12+
13+
14+
use aik099\PHPUnit\BrowserTestCase;
15+
use Mockery as m;
16+
17+
class DataProviderTest extends BrowserTestCase
18+
{
19+
20+
public function sampleDataProvider()
21+
{
22+
return array(
23+
array('case1'),
24+
array('case2'),
25+
);
26+
}
27+
28+
/**
29+
* @dataProvider sampleDataProvider
30+
*/
31+
public function testDataProvider($case)
32+
{
33+
$this->customMethod();
34+
35+
if ( $case === 'case1' || $case === 'case2' ) {
36+
$this->assertTrue(true);
37+
}
38+
else {
39+
$this->fail('Unknown $case: ' . $case);
40+
}
41+
}
42+
43+
protected function customMethod()
44+
{
45+
return 5;
46+
}
47+
48+
}

0 commit comments

Comments
 (0)