Skip to content
This repository has been archived by the owner on Mar 30, 2018. It is now read-only.

Commit

Permalink
Handle mocking objects for both PHPUnit 4 and 5 (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-zuralski authored and theofidry committed Apr 19, 2017
1 parent 017b96d commit 58df20b
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 46 deletions.
6 changes: 1 addition & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@ language: php
sudo: false

php:
- 7.0
- 5.6
- 5.5
- 5.4
- 7.0
- hhvm
- hhvm-nightly

matrix:
fast_finish: true
allow_failures:
- php: 7.0
- php: hhvm
- php: hhvm-nightly

cache:
directories:
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"issues": "https://github.com/padraic/humbug/issues"
},
"require": {
"php": ">=5.4.0",
"phpunit/phpunit": "^4.5|^5.0",
"php": ">=5.6.0",
"phpunit/phpunit": "^5.0",
"symfony/console": "^2.6|^3.0",
"symfony/finder": "^2.6|^3.0",
"symfony/process": "^2.6|^3.0",
Expand Down
3 changes: 2 additions & 1 deletion tests/Adapter/Phpunit/ConfigurationLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use Humbug\Adapter\Phpunit\ConfigurationLocator;
use Humbug\Adapter\Phpunit\XmlConfiguration;
use Humbug\Exception\RuntimeException;

class ConfigurationLocatorTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -43,7 +44,7 @@ public function testShouldRiseExceptionWhileLocatingConfiguration()
{
$directory = __DIR__;

$this->setExpectedException('\Humbug\Exception\RuntimeException');
$this->expectException(RuntimeException::class);

(new ConfigurationLocator())->locate($directory);
}
Expand Down
1 change: 0 additions & 1 deletion tests/Adapter/Phpunit/JobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class JobTest extends \PHPUnit_Framework_TestCase
{
public function testGenerateReturnsPHPScriptRenderedWithCurrentRunnersSettingsAndSerialisedMutationArray()
{
$this->markTestIncomplete();
$script = Job::generate('the_file.php', '/path/to/bootstrap.php');
$bootstrap = realpath(__DIR__ . '/../../../bootstrap.php');
$this->assertFileExists($bootstrap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function testShouldAddManyDifferentArguments()

public function testShouldRiseExceptionWhenArgumentTypeIsNotSupported()
{
$this->setExpectedException('\InvalidArgumentException');
$this->expectException(\InvalidArgumentException::class);

$objectVisitor = new ObjectVisitor('This\Is\Test\ClassName', [new \stdClass()]);

Expand Down
9 changes: 4 additions & 5 deletions tests/Adapter/Phpunit/XmlConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class XmlConfigurationTest extends \PHPUnit_Framework_TestCase
{
public function testShouldThrowExceptionIfNoDocumentElementIsPresent()
{
$this->setExpectedException('\LogicException', 'No document element present. Document should not be empty!');
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('No document element present. Document should not be empty!');

$dom = new \DOMDocument();
new XmlConfiguration($dom);
Expand Down Expand Up @@ -165,8 +166,7 @@ public function testShouldAddListener()

$xmlConfiguration = new XmlConfiguration($dom);

$visitor = $this->getMock('Humbug\Adapter\Phpunit\XmlConfiguration\Visitor');

$visitor = $this->createMock(XmlConfiguration\Visitor::class);
$visitor->expects($this->once())->method('visitElement')->with($this->isInstanceOf('\DOMElement'));

$xmlConfiguration->addListener($visitor);
Expand All @@ -182,8 +182,7 @@ public function testShouldAddListeners()

$xmlConfiguration = new XmlConfiguration($dom);

$visitor = $this->getMock('Humbug\Adapter\Phpunit\XmlConfiguration\Visitor');

$visitor = $this->createMock(XmlConfiguration\Visitor::class);
$visitor->expects($this->exactly(2))->method('visitElement')->with($this->isInstanceOf('\DOMElement'));

$xmlConfiguration->addListener($visitor);
Expand Down
18 changes: 7 additions & 11 deletions tests/Config/JsonParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Humbug\Test\Config;

use Humbug\Config\JsonParser;
use Humbug\Exception\JsonConfigException;

class JsonParserTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -41,27 +42,22 @@ public function testParseShouldReturnDistConfigIfNoOther()

public function testParsesNonDistFilePreferentially()
{
$this->setExpectedException(
'\Humbug\Exception\JsonConfigException'
);
$this->expectException(JsonConfigException::class);
$config = $this->parser->parseFile(__DIR__ . '/../_files/config4/');
}

public function testShouldRiseExceptionWhenFileNotExists()
{
$this->setExpectedException(
'\Humbug\Exception\JsonConfigException',
'Please create a humbug.json(.dist) file.'
);
$this->expectException(JsonConfigException::class);
$this->expectExceptionMessage('Please create a humbug.json(.dist) file.');
$this->parser->parseFile('it/not/exists/');
}

public function testShouldRiseExceptionWhenFileContainsInvalidJson()
{
$this->setExpectedExceptionRegExp(
'\Humbug\Exception\JsonConfigException',
'/Error parsing configuration file JSON.*/'
);
$this->expectException(JsonConfigException::class);
$this->expectExceptionMessageRegExp('/Error parsing configuration file JSON.*/');

$this->parser->parseFile(__DIR__ . '/../_files/config2/');
}
}
19 changes: 7 additions & 12 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Humbug\Test;

use Humbug\Config;
use Humbug\Exception\JsonConfigException;

class ConfigTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -61,10 +62,8 @@ public function testShouldRiseExceptionWhenSourceIsNotPresent()

$config = new Config($configData);

$this->setExpectedException(
'Humbug\Exception\JsonConfigException',
'Source code data is not included in configuration file'
);
$this->expectException(JsonConfigException::class);
$this->expectExceptionMessage('Source code data is not included in configuration file');

$config->getSource();
}
Expand All @@ -75,10 +74,8 @@ public function testShouldRiseExceptionWhenSourceDirectoriesAndExcludesAreNotPre
'source' => (object)[]
];

$this->setExpectedException(
'Humbug\Exception\JsonConfigException',
'You must set at least one source directory or exclude in the configuration file'
);
$this->expectException(JsonConfigException::class);
$this->expectExceptionMessage('You must set at least one source directory or exclude in the configuration file');

$config = new Config($configData);

Expand Down Expand Up @@ -134,10 +131,8 @@ public function testShouldRiseExceptionWhenChDirNotExists()

$config = new Config($configData);

$this->setExpectedExceptionRegExp(
'Humbug\Exception\JsonConfigException',
'/Directory in which to run tests does not exist: .+/'
);
$this->expectException(JsonConfigException::class);
$this->expectExceptionMessageRegExp('/Directory in which to run tests does not exist: .+/');

$config->getChDir();
}
Expand Down
5 changes: 3 additions & 2 deletions tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Humbug\Test;

use Humbug\Container;
use Humbug\Exception\InvalidArgumentException;
use Mockery as m;
use Symfony\Component\Finder\Finder;

Expand Down Expand Up @@ -50,7 +51,7 @@ public function testGetShouldRiseExceptionForUnknownOption()
$input = [
'options' => null
];
$this->setExpectedException('\InvalidArgumentException');
$this->expectException(\InvalidArgumentException::class);
$container = new Container($input);
$container->get('invalid-option');
}
Expand Down Expand Up @@ -88,7 +89,7 @@ public function testsetTempDirectory()

public function testsetTempDirectoryThrowsExceptionOnUnwriteableParam()
{
$this->setExpectedException('Humbug\\Exception\\InvalidArgumentException');
$this->expectException(InvalidArgumentException::class);
$result = $this->container->setTempDirectory('/really/does/not/exist');
}

Expand Down
3 changes: 2 additions & 1 deletion tests/MutableIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public function testIteratorContainsMutableFilesReturnedByFinder()
{
$finder = new Finder();

$finder->files('*.php');
$finder->files();
$finder->name('*.php');
$finder->in([ __DIR__ . '/_files/mutables' ]);

$container = new Container([ 'options' => '' ]);
Expand Down
2 changes: 1 addition & 1 deletion tests/Mutator/Arithmetic/ExponentiationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testReturnsTokenEquivalentToDivisionOperator()
public function testMutatesExponentiationToDivision()
{
if (!defined('T_POW')) {
$this->markTestSkipped();
$this->markTestSkipped('No "T_POW" PHP constant');
}

$tokens = [10 => [T_POW]];
Expand Down
2 changes: 1 addition & 1 deletion tests/Mutator/Arithmetic/PowEqualTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testReturnsTokenEquivalentToDivEqualOperator()
public function testMutatesMulEqualToDivEqual()
{
if (!defined('T_POW')) {
$this->markTestSkipped();
$this->markTestSkipped('No "T_POW" PHP constant');
}

$tokens = [10 => [T_POW_EQUAL, '**=']];
Expand Down
9 changes: 6 additions & 3 deletions tests/Report/TextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
namespace Humbug\Test\Report;

use Humbug\Mutant;
use Humbug\Mutation;
use Humbug\Report\Text;
use Humbug\TestSuite\Mutant\Result;
Expand Down Expand Up @@ -71,7 +72,9 @@ public function testShouldPrepareAllMutantsReport()
$this->createMutantResult()
];

$textReport = $this->getMock('Humbug\Report\Text', ['prepareReportForMutant']);
$textReport = $this->getMockBuilder(Text::class)
->setMethods(['prepareReportForMutant'])
->getMock();

$textReport->expects($this->at(0))->method('prepareReportForMutant')->with($results[0]);
$textReport->expects($this->at(1))->method('prepareReportForMutant')->with($results[1]);
Expand Down Expand Up @@ -103,12 +106,12 @@ private function createMutantResultStub($errorOutput)

private function createMutant()
{
return $this->getMock('Humbug\Mutant', [], [], '', false);
return $this->getMockBuilder(Mutant::class)->disableOriginalConstructor()->getMock();
}

private function createMutantResult()
{
return $this->getMock('Humbug\TestSuite\Mutant\Result', [], [], '', false);
return $this->getMockBuilder(Result::class)->disableOriginalConstructor()->getMock();
}

private function getExpectedMutantReport()
Expand Down

0 comments on commit 58df20b

Please sign in to comment.