Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for declaring test case in data provider #2861

Merged
merged 6 commits into from
Nov 27, 2017
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/Framework/TestSuite.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ class PHPUnit_Framework_TestSuite implements PHPUnit_Framework_Test, PHPUnit_Fra
*/
private $iteratorFilter = null;

/**
* @var array
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

array of ? strings? then string[]

*/
private $declaredClasses;

/**
* Constructs a new TestSuite:
*
Expand All @@ -111,6 +116,8 @@ class PHPUnit_Framework_TestSuite implements PHPUnit_Framework_Test, PHPUnit_Fra
*/
public function __construct($theClass = '', $name = '')
{
$this->declaredClasses = get_declared_classes();

$argumentsValid = false;

if (is_object($theClass) &&
Expand Down Expand Up @@ -306,9 +313,8 @@ public function addTestFile($filename)

// The given file may contain further stub classes in addition to the
// test class itself. Figure out the actual test class.
$classes = get_declared_classes();
$filename = PHPUnit_Util_Fileloader::checkAndLoad($filename);
$newClasses = array_diff(get_declared_classes(), $classes);
$newClasses = array_diff(get_declared_classes(), $this->declaredClasses);

// The diff is empty in case a parent class (with test methods) is added
// AFTER a child class that inherited from it. To account for that case,
Expand All @@ -319,6 +325,7 @@ public function addTestFile($filename)
// process discovered classes in approximate LIFO order, so as to
// avoid unnecessary reflection.
$this->foundClasses = array_merge($newClasses, $this->foundClasses);
$this->declaredClasses = get_declared_classes();
}

// The test class's name must match the filename, either in full, or as
Expand All @@ -341,6 +348,10 @@ public function addTestFile($filename)
}

foreach ($newClasses as $className) {
if ($className === 'PHPUnit_Framework_TestSuite_DataProvider') {
continue;
}

$class = new ReflectionClass($className);

if (!$class->isAbstract()) {
Expand Down
17 changes: 17 additions & 0 deletions tests/TextUI/dataprovider-issue-2833.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
phpunit ../_files/DataProviderIssue2833
--FILE--
<?php
$_SERVER['argv'][1] = '--no-configuration';
$_SERVER['argv'][2] = __DIR__ . '/../_files/DataProviderIssue2833';

require __DIR__ . '/../bootstrap.php';
PHPUnit_TextUI_Command::main();
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

.. 2 / 2 (100%)

Time: %s, Memory: %s

OK (2 tests, 2 assertions)
17 changes: 17 additions & 0 deletions tests/TextUI/dataprovider-issue-2859.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
phpunit -c ../_files/DataProviderIssue2859/phpunit.xml
--FILE--
<?php
$_SERVER['argv'][1] = '-c';
$_SERVER['argv'][2] = __DIR__ . '/../_files/DataProviderIssue2859/phpunit.xml';

require __DIR__ . '/../bootstrap.php';
PHPUnit_TextUI_Command::main();
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

. 1 / 1 (100%)

Time: %s, Memory: %s

OK (1 test, 1 assertion)
23 changes: 23 additions & 0 deletions tests/_files/DataProviderIssue2833/FirstTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Foo\DataProviderIssue2833;

use PHPUnit\Framework\TestCase;

class FirstTest extends TestCase
{
/**
* @dataProvider provide
*/
public function testFirst($x)
{
$this->assertTrue(true);
}

public function provide()
{
SecondTest::DUMMY;

return [[true]];
}
}
15 changes: 15 additions & 0 deletions tests/_files/DataProviderIssue2833/SecondTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Foo\DataProviderIssue2833;

use PHPUnit\Framework\TestCase;

class SecondTest extends TestCase
{
const DUMMY = 'dummy';

public function testSecond()
{
$this->assertTrue(true);
}
}
10 changes: 10 additions & 0 deletions tests/_files/DataProviderIssue2859/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit>
<testsuites>
<testsuite name="Test Suite">
<directory>./tests/</directory>
<directory>./tests/*/</directory>
</testsuite>
</testsuites>
</phpunit>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Foo\DataProviderIssue2859;

use PHPUnit\Framework\TestCase;

class TestWithDataProviderTest extends TestCase
{
/**
* @dataProvider provide
*/
public function testFirst($x)
{
$this->assertTrue(true);
}

public function provide()
{
return [[true]];
}
}