Skip to content

Commit

Permalink
[TASK] Bump PHPUnit version to ^8.1 (#456)
Browse files Browse the repository at this point in the history
Bump PHPUnit to most recent version, adapt tests and adjust
the Travis testing matrix to no longer test on PHP below 7.2,
in preparation for the up-coming minimum PHP requirement.

* Fix getMockForAbstractClass usages

* Fix locale-based casting of expected float

Prevent comparing a float cast to localized comma-
or thousand-separators, causing assertion to fail.
  • Loading branch information
NamelessCoder authored and manuelselbach committed Jun 20, 2019
1 parent 7f37c8a commit 4312e8c
Show file tree
Hide file tree
Showing 43 changed files with 231 additions and 110 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,3 +4,4 @@ composer.lock
vendor
examples/cache/*.php
.numerolog-token-90d266e4c7e3463a4266086282f608c1121c3d95
/.phpunit.result.cache
5 changes: 1 addition & 4 deletions .travis.yml
Expand Up @@ -15,11 +15,8 @@ cache:
- $HOME/.composer/cache

php:
- 5.5
- 5.6
- 7
- 7.1
- 7.2
- 7.3

before_install:
- composer self-update
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -6,7 +6,7 @@
"php": ">=5.5.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8",
"phpunit/phpunit": "^8.1",
"mikey179/vfsstream": "^1.6",
"squizlabs/php_codesniffer": "^2.7",
"php-coveralls/php-coveralls": "^2.1"
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Cache/FluidCacheWarmupResult.php
Expand Up @@ -58,7 +58,7 @@ public function add(ParsedTemplateInterface $state, $templatePathAndFilename)
$currentlyCompiled = $state->isCompiled();
$this->results[$templatePathAndFilename] = [
static::RESULT_COMPILABLE => $currentlyCompiled || $state->isCompilable(),
static::RESULT_COMPILED => $state->isCompiled(),
static::RESULT_COMPILED => $currentlyCompiled,
static::RESULT_HASLAYOUT => $state->hasLayout(),
static::RESULT_COMPILEDCLASS => $state->getIdentifier()
];
Expand Down
97 changes: 94 additions & 3 deletions tests/BaseTestCase.php
Expand Up @@ -6,6 +6,9 @@
* See LICENSE.txt that was shipped with this package.
*/

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* The mother of all test cases.
*
Expand All @@ -14,7 +17,7 @@
*
* @api
*/
abstract class BaseTestCase extends \PHPUnit_Framework_TestCase
abstract class BaseTestCase extends TestCase
{

/**
Expand All @@ -34,12 +37,87 @@ abstract class BaseTestCase extends \PHPUnit_Framework_TestCase
* @param boolean $callOriginalConstructor
* @param boolean $callOriginalClone
* @param boolean $callAutoload
* @return \PHPUnit_Framework_MockObject_MockObject
* @return MockObject
* @api
*/
protected function getAccessibleMock($originalClassName, $methods = [], array $arguments = [], $mockClassName = '', $callOriginalConstructor = true, $callOriginalClone = true, $callAutoload = true)
{
return $this->getMock($this->buildAccessibleProxy($originalClassName), $methods, $arguments, $mockClassName, $callOriginalConstructor, $callOriginalClone, $callAutoload);
$builder = $this->getMockBuilder($this->buildAccessibleProxy($originalClassName))->setMethods($methods)->setConstructorArgs($arguments)->setMockClassName($mockClassName);
if (!$callAutoload) {
$builder->disableAutoload();
}
if (!$callOriginalClone) {
$builder->disableOriginalClone();
}
if (!$callOriginalConstructor) {
$builder->disableOriginalConstructor();
}

return $builder->getMock();
}

public static function assertAttributeEquals($expected, string $actualAttributeName, $actualClassOrObject, string $message = '', float $delta = 0.0, int $maxDepth = 10, bool $canonicalize = false, bool $ignoreCase = false): void
{
static::assertEquals($expected, static::extractNonPublicAttribute($actualClassOrObject, $actualAttributeName));
}

public static function assertAttributeSame($expected, string $actualAttributeName, $actualClassOrObject, string $message = '', float $delta = 0.0, int $maxDepth = 10, bool $canonicalize = false, bool $ignoreCase = false): void
{
static::assertSame($expected, static::extractNonPublicAttribute($actualClassOrObject, $actualAttributeName));
}

public static function assertAttributeContains($needle, string $haystackAttributeName, $haystackClassOrObject, string $message = '', bool $ignoreCase = false, bool $checkForObjectIdentity = true, bool $checkForNonObjectIdentity = false): void
{
static::assertContains($needle, static::extractNonPublicAttribute($haystackClassOrObject, $haystackAttributeName));
}

public static function assertAttributeNotEmpty(string $haystackAttributeName, $haystackClassOrObject, string $message = ''): void
{
static::assertNotEmpty(static::extractNonPublicAttribute($haystackClassOrObject, $haystackAttributeName));
}

public static function assertAttributeInstanceOf(string $expected, string $attributeName, $classOrObject, string $message = ''): void
{
static::assertInstanceOf($expected, static::extractNonPublicAttribute($classOrObject, $attributeName));
}

protected static function extractNonPublicAttribute($actualClassOrObject, string $actualAttributeName)
{
$reflection = new \ReflectionClass($actualClassOrObject);
$attribute = $reflection->getProperty($actualAttributeName);
$attribute->setAccessible(true);
return $attribute->getValue($actualClassOrObject);
}

/**
* Returns a mock object which allows for calling protected methods and access
* of protected properties.
*
* @param string $originalClassName Full qualified name of the original class
* @param array $methods
* @param array $arguments
* @param boolean $callOriginalConstructor
* @param boolean $callOriginalClone
* @param boolean $callAutoload
* @return MockObject
* @api
*/
protected function getMock($originalClassName, $methods = [], array $arguments = [], $callOriginalConstructor = true, $callOriginalClone = true, $callAutoload = true)
{
$builder = $this->getMockBuilder($originalClassName)->setMethods($methods)->setConstructorArgs($arguments);
if (!$callAutoload) {
$builder->disableAutoload();
}

if (!$callOriginalClone) {
$builder->disableOriginalClone();
}

if (!$callOriginalConstructor) {
$builder->disableOriginalConstructor();
}

return $builder->getMock();
}

/**
Expand Down Expand Up @@ -106,6 +184,19 @@ public function _get($propertyName) {
return $accessibleClassName;
}

protected function setExpectedException(string $class = \Exception::class, string $message = '', int $code = 0)
{
if ($class) {
$this->expectException($class);
}
if ($message) {
$this->expectExceptionMessage($message);
}
if ($code) {
$this->expectExceptionCode($code);
}
}

/**
* Injects $dependency into property $name of $target
*
Expand Down
2 changes: 2 additions & 0 deletions tests/Functional/BaseConditionalFunctionalTestCase.php
Expand Up @@ -125,6 +125,8 @@ public function testTemplateCodeFixtureWithCache($sourceOrStream, $expectation,
{
if ($this->getCache()) {
$this->testTemplateCodeFixture($sourceOrStream, $variables, $expected, $notExpected, true);
} else {
$this->markTestSkipped('Cache-specific test skipped');
}
}
}
6 changes: 4 additions & 2 deletions tests/Functional/BaseFunctionalTestCase.php
Expand Up @@ -109,14 +109,14 @@ public function testTemplateCodeFixture($source, array $variables, array $expect
}
foreach ($expected as $expectedValue) {
if (is_string($expectedValue) === true) {
$this->assertContains($expectedValue, $output);
$this->assertStringContainsString($expectedValue, $output);
} else {
$this->assertEquals($expectedValue, $output);
}
}
foreach ($notExpected as $notExpectedValue) {
if (is_string($notExpectedValue) === true) {
$this->assertNotContains($notExpectedValue, $output);
$this->assertStringNotContainsString($notExpectedValue, $output);
} else {
$this->assertNotEquals($notExpectedValue, $output);
}
Expand Down Expand Up @@ -146,6 +146,8 @@ public function testTemplateCodeFixtureWithCache($sourceOrStream, array $variabl
if ($this->getCache()) {
$this->testTemplateCodeFixture($sourceOrStream, $variables, $expected, $notExpected, $expectedException, true);
$this->testTemplateCodeFixture($sourceOrStream, $variables, $expected, $notExpected, $expectedException, true);
} else {
$this->markTestSkipped('Cache-specific test skipped');
}
}
}
Expand Up @@ -79,14 +79,14 @@ public function testTemplateCodeFixture($source, array $variables, array $expect

foreach ($expected as $expectedValue) {
if (is_string($expectedValue) === true) {
$this->assertContains($expectedValue, $output);
$this->assertStringContainsString($expectedValue, $output);
} else {
$this->assertEquals($expectedValue, $output);
}
}
foreach ($notExpected as $notExpectedValue) {
if (is_string($notExpectedValue) === true) {
$this->assertNotContains($notExpectedValue, $output);
$this->assertStringNotContainsString($notExpectedValue, $output);
} else {
$this->assertNotEquals($notExpectedValue, $output);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Functional/CommandTest.php
Expand Up @@ -18,7 +18,7 @@ class CommandTest extends BaseTestCase
/**
* @return void
*/
public static function setUpBeforeClass()
public static function setUpBeforeClass(): void
{
vfsStream::setup('fakecache/');
}
Expand All @@ -35,10 +35,10 @@ public function testCommand($argumentString, array $mustContain, array $mustNotC
$command = sprintf($argumentString, $bin);
$output = shell_exec($command);
foreach ($mustContain as $mustContainString) {
$this->assertContains($mustContainString, $output);
$this->assertStringContainsString($mustContainString, $output);
}
foreach ($mustNotContain as $mustNotContainString) {
$this->assertNotContains($mustNotContainString, $output);
$this->assertStringNotContainsString($mustNotContainString, $output);
}
}

Expand Down
6 changes: 3 additions & 3 deletions tests/Functional/ExamplesTest.php
Expand Up @@ -18,7 +18,7 @@ class ExamplesTest extends BaseTestCase
/**
* @return void
*/
public static function setUpBeforeClass()
public static function setUpBeforeClass(): void
{
vfsStream::setup('fakecache/');
}
Expand Down Expand Up @@ -132,8 +132,8 @@ public function getExampleScriptTestValues()
[
'Expression: $numberten % 4 = 2',
'Expression: 4 * $numberten = 40',
'Expression: 4 / $numberten = 0.4',
'Expression: $numberone / $numberten = 0.1',
'Expression: 4 / $numberten = ' . (0.4), // NOTE: concat'ing a float + string LC-casts the float to localized comma/t-sep. Hence, let PHP also cast the expected value.
'Expression: $numberone / $numberten = ' . (0.1), // NOTE: concat'ing a float + string LC-casts the float to localized comma/t-sep. Hence, let PHP also cast the expected value.
'Expression: 10 ^ $numberten = 10000000000'
]
],
Expand Down
12 changes: 6 additions & 6 deletions tests/Unit/Core/Cache/FluidCacheWarmupResultTest.php
Expand Up @@ -30,7 +30,7 @@ public function testMerge(array $results, array $expected)
$result2 = $this->getAccessibleMock(FluidCacheWarmupResult::class, ['dummy']);
$result2->_set('results', array_pop($results));
$result1->merge($result2);
$this->assertAttributeSame($expected, 'results', $result1);
$this->assertEquals($expected, $result1->getResults());
}

/**
Expand All @@ -51,7 +51,7 @@ public function testGetResults()
{
$subject = $this->getAccessibleMock(FluidCacheWarmupResult::class, ['dummy']);
$subject->_set('results', ['foo' => 'bar']);
$this->assertAttributeEquals(['foo' => 'bar'], 'results', $subject);
$this->assertEquals(['foo' => 'bar'], $subject->getResults());
}

/**
Expand All @@ -64,7 +64,7 @@ public function testAdd(ParsedTemplateInterface $subject, array $expected)
{
$result = new FluidCacheWarmupResult();
$result->add($subject, 'foobar');
$this->assertAttributeEquals(['foobar' => $expected], 'results', $result);
$this->assertEquals(['foobar' => $expected], $result->getResults());
}

/**
Expand All @@ -77,7 +77,7 @@ public function getAddTestValues()
)->setMethods(
['isCompiled', 'isCompilable', 'hasLayout', 'getIdentifier']
)->getMockForAbstractClass();
$subject1->expects($this->exactly(2))->method('isCompiled')->willReturn(false);
$subject1->expects($this->once())->method('isCompiled')->willReturn(false);
$subject1->expects($this->once())->method('isCompilable')->willReturn(true);
$subject1->expects($this->once())->method('hasLayout')->willReturn(false);
$subject1->expects($this->once())->method('getIdentifier')->willReturn('subject1-identifier');
Expand All @@ -86,8 +86,8 @@ public function getAddTestValues()
)->setMethods(
['isCompiled', 'isCompilable', 'hasLayout', 'getIdentifier', 'getFailureReason', 'getMitigations']
)->getMockForAbstractClass();
$subject2->expects($this->exactly(2))->method('isCompiled')->willReturn(true);
$subject2->expects($this->once())->method('isCompilable')->willReturn(true);
$subject2->expects($this->once())->method('isCompiled')->willReturn(true);
$subject2->expects($this->never())->method('isCompilable');
$subject2->expects($this->once())->method('hasLayout')->willReturn(true);
$subject2->expects($this->once())->method('getIdentifier')->willReturn('subject2-identifier');
$subject2->expects($this->once())->method('getFailureReason')->willReturn('failure-reason');
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Core/Cache/SimpleFileCacheTest.php
Expand Up @@ -26,7 +26,7 @@ class SimpleFileCacheTest extends UnitTestCase
/**
* @return void
*/
public function setUp()
public function setUp(): void
{
$this->directory = vfsStream::setup('cache');
}
Expand Down
5 changes: 3 additions & 2 deletions tests/Unit/Core/Compiler/FailedCompilingStateTest.php
Expand Up @@ -40,8 +40,9 @@ public function testSetter($property, $value)
$subject = $this->getAccessibleMock(FailedCompilingState::class, ['dummy']);
$subject->_set($property, $value);
$method = 'set' . ucfirst($property);
$getter = 'get' . ucfirst($property);
$subject->$method($value);
$this->assertAttributeEquals($value, $property, $subject);
$this->assertEquals($value, $subject->$getter());
}

/**
Expand All @@ -52,7 +53,7 @@ public function testAddMitigation()
$subject = $this->getAccessibleMock(FailedCompilingState::class, ['dummy']);
$subject->_set('mitigations', ['m1']);
$subject->addMitigation('m2');
$this->assertAttributeEquals(['m1', 'm2'], 'mitigations', $subject);
$this->assertEquals(['m1', 'm2'], $subject->getMitigations());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Core/Parser/BooleanParserTest.php
Expand Up @@ -24,7 +24,7 @@ class BooleanParserTest extends UnitTestCase
/**
* Setup fixture
*/
public function setUp()
public function setUp(): void
{
$this->renderingContext = new RenderingContextFixture();
}
Expand Down
10 changes: 5 additions & 5 deletions tests/Unit/Core/Parser/Interceptor/EscapeTest.php
Expand Up @@ -22,26 +22,26 @@ class EscapeTest extends UnitTestCase
{

/**
* @var Escape|\PHPUnit_Framework_MockObject_MockObject
* @var Escape|\PHPUnit\Framework\MockObject\MockObject
*/
protected $escapeInterceptor;

/**
* @var AbstractViewHelper|\PHPUnit_Framework_MockObject_MockObject
* @var AbstractViewHelper|\PHPUnit\Framework\MockObject\MockObject
*/
protected $mockViewHelper;

/**
* @var ViewHelperNode|\PHPUnit_Framework_MockObject_MockObject
* @var ViewHelperNode|\PHPUnit\Framework\MockObject\MockObject
*/
protected $mockNode;

/**
* @var ParsingState|\PHPUnit_Framework_MockObject_MockObject
* @var ParsingState|\PHPUnit\Framework\MockObject\MockObject
*/
protected $mockParsingState;

public function setUp()
public function setUp(): void
{
$this->escapeInterceptor = $this->getAccessibleMock(Escape::class, ['dummy']);
$this->mockViewHelper = $this->getMockBuilder(AbstractViewHelper::class)->disableOriginalConstructor()->getMock();
Expand Down

0 comments on commit 4312e8c

Please sign in to comment.