Skip to content

Update the unit tests setup #9

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

Merged
merged 5 commits into from
Sep 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
.gitignore export-ignore
.travis.yml export-ignore
build.xml export-ignore
phpunit.xml export-ignore
phpunit.xml.dist export-ignore
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
build
vendor
composer.lock
phpunit.xml
8 changes: 7 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ cache:
- $HOME/.composer/cache

before_script:
- composer install --no-interaction --prefer-source
- |
if [[ $TRAVIS_PHP_VERSION != "nightly" ]]; then
composer install --no-suggest --no-interaction
else
# Ignore platform requirements to allow PHPUnit to install on PHP 8.
composer install --no-suggest --no-interaction --ignore-platform-reqs
fi

script:
- ant phplint
Expand Down
8 changes: 4 additions & 4 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@
<target name="phpunit" depends="prepare" description="PHP unit">
<delete file="${basedir}/build/logs/phpunit.xml" quiet="true" />

<exec executable="${phpunit}">
<arg line='--configuration ${basedir}/phpunit.xml' />
<exec executable="${phpunit}" failonerror="true">
<arg line='--configuration ${basedir}/phpunit.xml.dist' />
<arg line='-d memory_limit=256M' />
<arg line='--log-junit "${basedir}/build/logs/phpunit.xml"' />
<arg line='${colors-arg.color}' />
Expand All @@ -78,8 +78,8 @@
<delete dir="${basedir}/build/coverage" quiet="true" />
<mkdir dir="${basedir}/build/coverage" />

<exec executable="${phpunit}">
<arg line='--configuration ${basedir}/phpunit.xml' />
<exec executable="${phpunit}" failonerror="true">
<arg line='--configuration ${basedir}/phpunit.xml.dist' />
<arg line='-d memory_limit=256M' />
<arg line='--log-junit "${basedir}/build/logs/phpunit.xml"' />
<arg line='--coverage-clover "${basedir}/build/logs/clover.xml"' />
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"php": ">=5.4.0"
},
"require-dev": {
"phpunit/phpunit": "~4.3",
"phpunit/phpunit": "^4.8.36 || ^5.0 || ^6.0 || ^7.0",
"php-parallel-lint/php-parallel-lint": "^1.0",
"php-parallel-lint/php-var-dump-check": "0.*",
"squizlabs/php_codesniffer": "1.*",
Expand Down
11 changes: 6 additions & 5 deletions phpunit.xml → phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
backupGlobals="true"
bootstrap="./vendor/autoload.php">

<testsuites>
<testsuite>
<testsuite name="PHP-Console-Color">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>

<!-- Ignore vendor folder for code coverage -->
<filter>
<blacklist>
<directory>vendor</directory>
</blacklist>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
57 changes: 38 additions & 19 deletions tests/ConsoleColorTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
use JakubOnderka\PhpConsoleColor\ConsoleColor;
use PHPUnit\Framework\TestCase;

class ConsoleColorWithForceSupport extends ConsoleColor
{
Expand Down Expand Up @@ -28,7 +29,7 @@ public function are256ColorsSupported()
}
}

class ConsoleColorTest extends \PHPUnit_Framework_TestCase
class ConsoleColorTest extends TestCase
{
/** @var ConsoleColorWithForceSupport */
private $uut;
Expand All @@ -41,21 +42,21 @@ protected function setUp()
public function testNone()
{
$output = $this->uut->apply('none', 'text');
$this->assertEquals("text", $output);
$this->assertSame("text", $output);
}

public function testBold()
{
$output = $this->uut->apply('bold', 'text');
$this->assertEquals("\033[1mtext\033[0m", $output);
$this->assertSame("\033[1mtext\033[0m", $output);
}

public function testBoldColorsAreNotSupported()
{
$this->uut->setIsSupported(false);

$output = $this->uut->apply('bold', 'text');
$this->assertEquals("text", $output);
$this->assertSame("text", $output);
}

public function testBoldColorsAreNotSupportedButAreForced()
Expand All @@ -64,73 +65,73 @@ public function testBoldColorsAreNotSupportedButAreForced()
$this->uut->setForceStyle(true);

$output = $this->uut->apply('bold', 'text');
$this->assertEquals("\033[1mtext\033[0m", $output);
$this->assertSame("\033[1mtext\033[0m", $output);
}

public function testDark()
{
$output = $this->uut->apply('dark', 'text');
$this->assertEquals("\033[2mtext\033[0m", $output);
$this->assertSame("\033[2mtext\033[0m", $output);
}

public function testBoldAndDark()
{
$output = $this->uut->apply(array('bold', 'dark'), 'text');
$this->assertEquals("\033[1;2mtext\033[0m", $output);
$this->assertSame("\033[1;2mtext\033[0m", $output);
}

public function test256ColorForeground()
{
$output = $this->uut->apply('color_255', 'text');
$this->assertEquals("\033[38;5;255mtext\033[0m", $output);
$this->assertSame("\033[38;5;255mtext\033[0m", $output);
}

public function test256ColorWithoutSupport()
{
$this->uut->setAre256ColorsSupported(false);

$output = $this->uut->apply('color_255', 'text');
$this->assertEquals("text", $output);
$this->assertSame("text", $output);
}

public function test256ColorBackground()
{
$output = $this->uut->apply('bg_color_255', 'text');
$this->assertEquals("\033[48;5;255mtext\033[0m", $output);
$this->assertSame("\033[48;5;255mtext\033[0m", $output);
}

public function test256ColorForegroundAndBackground()
{
$output = $this->uut->apply(array('color_200', 'bg_color_255'), 'text');
$this->assertEquals("\033[38;5;200;48;5;255mtext\033[0m", $output);
$this->assertSame("\033[38;5;200;48;5;255mtext\033[0m", $output);
}

public function testSetOwnTheme()
{
$this->uut->setThemes(array('bold_dark' => array('bold', 'dark')));
$output = $this->uut->apply(array('bold_dark'), 'text');
$this->assertEquals("\033[1;2mtext\033[0m", $output);
$this->assertSame("\033[1;2mtext\033[0m", $output);
}

public function testAddOwnTheme()
{
$this->uut->addTheme('bold_own', 'bold');
$output = $this->uut->apply(array('bold_own'), 'text');
$this->assertEquals("\033[1mtext\033[0m", $output);
$this->assertSame("\033[1mtext\033[0m", $output);
}

public function testAddOwnThemeArray()
{
$this->uut->addTheme('bold_dark', array('bold', 'dark'));
$output = $this->uut->apply(array('bold_dark'), 'text');
$this->assertEquals("\033[1;2mtext\033[0m", $output);
$this->assertSame("\033[1;2mtext\033[0m", $output);
}

public function testOwnWithStyle()
{
$this->uut->addTheme('bold_dark', array('bold', 'dark'));
$output = $this->uut->apply(array('bold_dark', 'italic'), 'text');
$this->assertEquals("\033[1;2;3mtext\033[0m", $output);
$this->assertSame("\033[1;2;3mtext\033[0m", $output);
}

public function testHasAndRemoveTheme()
Expand All @@ -146,25 +147,25 @@ public function testHasAndRemoveTheme()

public function testApplyInvalidArgument()
{
$this->setExpectedException('\InvalidArgumentException');
$this->exceptionHelper('\InvalidArgumentException');
$this->uut->apply(new stdClass(), 'text');
}

public function testApplyInvalidStyleName()
{
$this->setExpectedException('\JakubOnderka\PhpConsoleColor\InvalidStyleException');
$this->exceptionHelper('\JakubOnderka\PhpConsoleColor\InvalidStyleException');
$this->uut->apply('invalid', 'text');
}

public function testApplyInvalid256Color()
{
$this->setExpectedException('\JakubOnderka\PhpConsoleColor\InvalidStyleException');
$this->exceptionHelper('\JakubOnderka\PhpConsoleColor\InvalidStyleException');
$this->uut->apply('color_2134', 'text');
}

public function testThemeInvalidStyle()
{
$this->setExpectedException('\JakubOnderka\PhpConsoleColor\InvalidStyleException');
$this->exceptionHelper('\JakubOnderka\PhpConsoleColor\InvalidStyleException');
$this->uut->addTheme('invalid', array('invalid'));
}

Expand All @@ -180,5 +181,23 @@ public function testGetPossibleStyles()
$this->assertInternalType('array', $this->uut->getPossibleStyles());
$this->assertNotEmpty($this->uut->getPossibleStyles());
}

public function exceptionHelper($exception, $msg = null)
{
if (\method_exists($this, 'expectException')) {
// PHPUnit 5+.
$this->expectException($exception);
if (isset($msg)) {
$this->expectExceptionMessage($msg);
}
} else {
// PHPUnit 4.
if (isset($msg)) {
$this->setExpectedException($exception, $msg);
} else {
$this->setExpectedException($exception);
}
}
}
}