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

Commit

Permalink
Add more methods (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanmai committed Nov 22, 2020
1 parent 973a3a9 commit b87c59f
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 11 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,11 @@ jobs:
- name: Run tests
run: |
vendor/bin/phpunit
vendor/bin/phpunit --coverage-clover build/logs/clover.xml
- name: Upload coverage results to Coveralls
continue-on-error: true
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
php vendor/bin/php-coveralls --coverage_clover=build/logs/clover.xml -v
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/composer.lock
/phpunit.xml
/vendor/
/build/
1 change: 1 addition & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ return PhpCsFixer\Config::create()
'combine_consecutive_unsets' => true,
'comment_to_phpdoc' => true,
'compact_nullable_typehint' => true,
'declare_strict_types' => false,
'escape_implicit_backslashes' => true,
'explicit_indirect_variable' => true,
'explicit_string_variable' => true,
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"phpunit/phpunit": "^4.8.36 || ^5.7.27 || ^6.5.14 || ^7.5.20 || ^8.0 || ^9.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2"
"friendsofphp/php-cs-fixer": "^2",
"php-coveralls/php-coveralls": "^2.4"
},
"config": {
"optimize-autoloader": true,
Expand Down
5 changes: 5 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./vendor/autoload.php">
<filter>
<whitelist>
<directory>src</directory>
</whitelist>
</filter>
<testsuites>
<testsuite name="Default">
<directory>tests</directory>
Expand Down
9 changes: 1 addition & 8 deletions src/PolyfillTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,7 @@
if (version_compare(\PHPUnit\Runner\Version::id(), '7.0.0') < 0) {
trait PolyfillTrait
{
public function expectException($exception)
{
if (\is_callable(['parent', 'expectException'])) {
parent::expectException($exception);
} else {
$this->setExpectedException($exception);
}
}
use PolyfillTrait6;
}
} else {
trait PolyfillTrait
Expand Down
137 changes: 137 additions & 0 deletions src/PolyfillTrait6.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

/*
* This file is part of PHPUnit Good Practices.
*
* (c) Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace PHPUnitGoodPractices\Polyfill;

/**
* @internal
*/
trait PolyfillTrait6
{
public function expectException($exception)
{
if (\is_callable(['parent', 'expectException'])) {
parent::expectException($exception);
} else {
$this->setExpectedException($exception);
}
}

public function expectExceptionMessageMatches($regexp)
{
if (\is_callable(['parent', 'expectExceptionMessageMatches'])) {
parent::expectExceptionMessageMatches($regexp);

return;
}

// In some PHPUnit versions just setting an expectation for specific
// expection message won't trigger exception handler. Therefore we need
// to set the expected type, but trying to keep the type.
if (\is_callable(['parent', 'getExpectedException'])) {
$expectedException = parent::getExpectedException(); // This is an @internal method.
}

if (null === $expectedException) {
$expectedException = \Exception::class;
}

$this->expectException($expectedException);

if (\is_callable(['parent', 'expectExceptionMessageRegExp'])) {
// Method available since Release 5.2.0
$this->expectExceptionMessageRegExp($regexp);
} else {
$this->setExpectedExceptionRegExp($expectedException, $regexp);
}
}

public static function assertIsArray($actual, $message = '')
{
if (\is_callable(['parent', 'assertIsArray'])) {
parent::assertIsArray($actual, $message);
} else {
static::assertInternalType('array', $actual, $message);
}
}

public static function assertIsString($actual, $message = '')
{
if (\is_callable(['parent', 'assertIsString'])) {
parent::assertIsString($actual, $message);
} else {
static::assertInternalType('string', $actual, $message);
}
}

public static function assertIsBool($actual, $message = '')
{
if (\is_callable(['parent', 'assertIsBool'])) {
parent::assertIsBool($actual, $message);
} else {
static::assertInternalType('bool', $actual, $message);
}
}

public static function assertIsCallable($actual, $message = '')
{
if (\is_callable(['parent', 'assertIsCallable'])) {
parent::assertIsCallable($actual, $message);
} else {
static::assertInternalType('callable', $actual, $message);
}
}

public static function assertIsInt($actual, $message = '')
{
if (\is_callable(['parent', 'assertIsInt'])) {
parent::assertIsInt($actual, $message);
} else {
static::assertInternalType('int', $actual, $message);
}
}

public static function assertMatchesRegularExpression($pattern, $string, $message = '')
{
if (\is_callable(['parent', 'assertMatchesRegularExpression'])) {
parent::assertMatchesRegularExpression($pattern, $string, $message);
} else {
static::assertRegExp($pattern, $string, $message);
}
}

public static function assertStringContainsString($needle, $haystack, $message = '')
{
if (\is_callable(['parent', 'assertStringContainsString'])) {
parent::assertStringContainsString($needle, $haystack, $message);
} else {
static::assertContains($needle, $haystack, $message);
}
}

public static function assertStringNotContainsString($needle, $haystack, $message = '')
{
if (\is_callable(['parent', 'assertStringNotContainsString'])) {
parent::assertStringNotContainsString($needle, $haystack, $message);
} else {
static::assertNotContains($needle, $haystack, $message);
}
}

public static function assertFileDoesNotExist($filename, $message = '')
{
if (\is_callable(['parent', 'assertFileDoesNotExist'])) {
parent::assertFileDoesNotExist($filename, $message);
} else {
static::assertFileNotExists($filename, $message);
}
}
}
27 changes: 27 additions & 0 deletions src/PolyfillTrait7.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,31 @@ public function expectException(string $exception): void
$this->setExpectedException($exception);
}
}

public function expectExceptionMessageMatches(string $regexp): void
{
if (\is_callable(['parent', 'expectExceptionMessageMatches'])) {
parent::expectExceptionMessageMatches($regexp);
} else {
$this->expectExceptionMessageRegExp($regexp);
}
}

public static function assertMatchesRegularExpression(string $pattern, string $string, string $message = ''): void
{
if (\is_callable(['parent', 'assertMatchesRegularExpression'])) {
parent::assertMatchesRegularExpression($pattern, $string, $message);
} else {
static::assertRegExp($pattern, $string, $message);
}
}

public static function assertFileDoesNotExist(string $filename, string $message = ''): void
{
if (\is_callable(['parent', 'assertFileDoesNotExist'])) {
parent::assertFileDoesNotExist($filename, $message);
} else {
static::assertFileNotExists($filename, $message);
}
}
}
53 changes: 52 additions & 1 deletion tests/PolyfillTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,57 @@ public function testExpectException()
$this->expectException(\RuntimeException::class);

throw new \RuntimeException();
$this->fail();
}

public function testExpectExceptionMessageMatches()
{
$this->expectExceptionMessageMatches('/example/');

throw new \RuntimeException('example');
}

public function testAssertIsArray()
{
$this->assertIsArray([]);
}

public function testAssertIsString()
{
$this->assertIsString('');
}

public function testAssertIsBool()
{
$this->assertIsBool(true);
}

public function testAssertIsCallable()
{
$this->assertIsCallable([$this, 'testAssertIsCallable']);
}

public function testAssertIsInt()
{
$this->assertIsInt(1);
}

public function testAssertMatchesRegularExpression()
{
$this->assertMatchesRegularExpression('/\d/', '123');
}

public function testAssertStringContainsString()
{
$this->assertStringContainsString('test', 'testing');
}

public function testAssertStringNotContainsString()
{
$this->assertStringNotContainsString('foobar', 'testing');
}

public function testAssertFileDoesNotExist()
{
$this->assertFileDoesNotExist('invalid');
}
}

0 comments on commit b87c59f

Please sign in to comment.