Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Erased TestBase.php; refactored unit tests to use proper data providers #30

Merged
merged 3 commits into from
Sep 28, 2017

Conversation

voorproever
Copy link
Contributor

The TestBase class in this library is useless (exception assertion is provided by PHPUnit) and conflicts with the (legacy) TestBase class in our FCP project. PhpStorm is unable to provide correct auto-completion in such case:

screenshot from 2017-08-18 08-55-34

Travis will now also execute the test suite on PHP 7.0 and PHP 7.1 (see a4090da).

@voorproever voorproever requested a review from mburtscher August 18, 2017 08:37
@@ -36,19 +32,25 @@ public function testConcat_ReturnsConcatenatedElements()
$this->assertEquals("d", $all[3]);
}

public function testConcat_ThrowsArgumentExceptionIfNoTraversableArgument()
/**
* @expectedException InvalidArgumentException
Copy link
Member

Choose a reason for hiding this comment

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

@voorproever Exception asserting via attributes can be problematic. For example see this explanation from xunit (a popular .net unit testing library)

Long-term use of [ExpectedException] has uncovered various problems with it. First, it doesn’t specifically say which line of code should throw the exception, which allows subtle and difficult-to-track failures that show up as passing tests. Second, it doesn’t offer the opportunity to fully inspect details of the exception itself, since the handling is outside the normal code flow of the test. Assert.Throws allows you to test a specific set of code for throwing an exception, and returns the exception during success so you can write further asserts against the exception instance itself.

https://xunit.github.io/docs/comparisons.html#note1

See also:

If you are throwing a custom exception, then the chances of the same exception occurring in your code in the wrong place are minimal. However, things change when you are testing for system exceptions.

http://hadihariri.com/2008/10/17/testing-exceptions-with-xunit/

Copy link
Member

@davidroth davidroth Aug 18, 2017

Choose a reason for hiding this comment

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

@voorproever I am ok with these changes although personally I`d avoid the attribute approach.

So no need for changes ... just a general remark ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I (partially) agree with your first statement. However, there is no difference between the annotation and the old wrapper method. No unit test used the exception object afterwards. If I want to make sure that the correct exception is thrown during testing I would additionally use @expectedExceptionMessage; example from FCP project:

/**
 * @covers ModuleSetup::createFromDatabaseVersion
 *
 * @expectedException \LogicException
 * @expectedExceptionMessage Invalid config file for "johndoe" module.
 */
public function testCreateFromDatabaseVersion_corruptedModuleConfigFile()
{
    // "johndoe" module does not exist.

    ModuleSetup::createFromDatabaseVersion(
        "johndoe",
        \Core::getDB()->getConnection()
    );
}

A lot of unit tests in fusonic/linq could use @expectedExceptionMessage to fully eliminate the possibility of incorrect asserts, but I just wanted to get rid of the TestBase class 😉

Copy link
Member

@davidroth davidroth Aug 18, 2017

Choose a reason for hiding this comment

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

Yeah sure, narrowing it down further with exception message can make sense ;)
There is no black and white here ... just different approaches with pros&cons

Thx for the PR by the way ;)

@davidroth davidroth requested review from davidroth and removed request for davidroth August 18, 2017 12:18
@voorproever voorproever merged commit 065bfd3 into master Sep 28, 2017
@voorproever voorproever deleted the reorganize-tests branch September 28, 2017 11:13
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants