Skip to content

Commit

Permalink
stack trace sanity
Browse files Browse the repository at this point in the history
make sure the last element of the stack trace has a line number.
otherwise don't print the editor url

fixes #2
  • Loading branch information
wickedOne committed Oct 15, 2021
1 parent e5daba9 commit 83b02e9
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ specify this printer on the command line:
$ php vendor/bin/phpunit --printer='WickedOne\PHPUnitPrinter\PhpStormPrinter' src/
```

### phpunit xml configuration
### phpunit xml configuration:
specify this printer in your ``phpunit.xml.dist``:
```xml
<?xml version="1.0" encoding="UTF-8"?>
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<testsuites>
<testsuite name="unit">
<directory>tests/</directory>
<exclude>tests/Stub</exclude>
</testsuite>
</testsuites>

Expand Down
7 changes: 6 additions & 1 deletion src/PhpStormPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,14 @@ protected function printDefect(TestFailure $defect, int $count): void
*/
private function printDefectFooter(TestFailure $defect): void
{
$trace = explode(PHP_EOL, trim((string) $defect->thrownException()));
$trace = explode(\PHP_EOL, trim((string) $defect->thrownException()));
$offender = end($trace);

// the things you do to please infection... ;-)
if (false === \is_int(strpos($offender, ':'))) {
return;
}

[$file, $line] = explode(':', $offender);

if (isset($file, $line)) {
Expand Down
44 changes: 39 additions & 5 deletions tests/PhpStormPrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@

declare(strict_types=1);

/*
* This file is part of WickedOne\PHPUnitPrinter.
*
* (c) wicliff <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace WickedOne\PHPUnitPrinter\Tests;

use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\ErrorTestCase;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestResult;
use PHPUnit\Framework\TestSuite;
use WickedOne\PHPUnitPrinter\PhpStormPrinter;
use WickedOne\PHPUnitPrinter\Tests\Stub\EmptyTestClass;

/**
* PhpStorm Printer Test.
Expand All @@ -17,10 +28,9 @@
*/
class PhpStormPrinterTest extends TestCase
{

/**
* check for presence of editor url in output file
* and whether original messages are still printed
* and whether original messages are still printed.
*/
public function testPrintDefectFooter(): void
{
Expand All @@ -30,10 +40,9 @@ public function testPrintDefectFooter(): void

$result->addFailure($test, $throwable, time());

$filename = sys_get_temp_dir() . '/phpunit-printer.txt';
touch($filename);
$filename = sys_get_temp_dir().'/phpunit-printer.txt';

$printer = new PhpStormPrinter(fopen($filename, 'rb+'));
$printer = new PhpStormPrinter(fopen($filename, 'wb+'));
$printer->printResult($result);

$result = file_get_contents($filename);
Expand All @@ -45,4 +54,29 @@ public function testPrintDefectFooter(): void

@unlink($filename);
}

/**
* in some occasions no trace is provided.
* make sure printing editor url isn't printed in those occasions.
*/
public function testSkipDefectFooterOnWarning(): void
{
$result = (new TestSuite(EmptyTestClass::class))->run();

$filename = sys_get_temp_dir().'/phpunit-printer.txt';

$list = $GLOBALS['__PHPUNIT_ISOLATION_EXCLUDE_LIST'] ?? null;
$GLOBALS['__PHPUNIT_ISOLATION_EXCLUDE_LIST'] = [__FILE__];

$printer = new PhpStormPrinter(fopen($filename, 'wb+'));
$printer->printResult($result);

$GLOBALS['__PHPUNIT_ISOLATION_EXCLUDE_LIST'] = $list;

$result = file_get_contents($filename);

self::assertStringNotContainsString('phpstorm://open?file=', $result);

@unlink($filename);
}
}
25 changes: 25 additions & 0 deletions tests/Stub/EmptyTestClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

/*
* This file is part of WickedOne\PHPUnitPrinter.
*
* (c) wicliff <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace WickedOne\PHPUnitPrinter\Tests\Stub;

use PHPUnit\Framework\TestCase;

/**
* EmptyTest Class.
*
* @author wicliff <[email protected]>
*/
class EmptyTestClass extends TestCase
{
}
3 changes: 2 additions & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

/*
* This file is part of the SolrPHP SolariumBundle.
* This file is part of WickedOne\PHPUnitPrinter.
*
* (c) wicliff <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
Expand Down

0 comments on commit 83b02e9

Please sign in to comment.