Skip to content

Commit

Permalink
Merge pull request #3 from kenjis/add-dd
Browse files Browse the repository at this point in the history
Add dd() and d()
  • Loading branch information
kenjis authored Mar 4, 2021
2 parents 19638f5 + 2435546 commit 51e01dc
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Provides helper traits for PHPUnit.
- [`$this->getPrivateProperty()`](#this-getprivateproperty)
- [`$this->setPrivateProperty()`](#this-setprivateproperty)
- [`$this->getPrivateMethodInvoker()`](#this-getprivatemethodinvoker)
- [`DebugHelper`](#debughelper)
- [License](#license)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->
Expand Down Expand Up @@ -339,6 +340,28 @@ $this->assertEquals(
);
~~~

### `DebugHelper`

This trait provides helper functions, `dd()` and `d()` to dump variables.

Import the `Kenjis\PhpUnitHelper\DebugHelper` trait into your test class:

```php
<?php

declare(strict_types=1);

namespace Foo\Bar\Test\Unit;

use Kenjis\PhpUnitHelper\DebugHelper;
use PHPUnit\Framework;

final class BazTest extends Framework\TestCase
{
use DebugHelper;
}
```

## License

This package is licensed using the MIT License.
Expand Down
11 changes: 11 additions & 0 deletions src/DebugHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Kenjis\PhpUnitHelper;

require_once __DIR__ . '/functions.php'; // @codeCoverageIgnore

trait DebugHelper
{
}
44 changes: 44 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* Helper Functions
*/

declare(strict_types=1);

/**
* @SuppressWarnings(PHPMD.ExitExpression)
* Does not work if I move to the function annotation
* See https://github.com/phpmd/phpmd/issues/337
*/
if (! function_exists('dd')) { // @codeCoverageIgnore

/**
* Alias of var_dump() and exit()
*
* @param mixed ...$vars
*
* @psalm-suppress ForbiddenCode Unsafe var_dump
* @codeCoverageIgnore
*/
function dd(...$vars): void
{
var_dump(...$vars);
exit;
}
}

if (! function_exists('d')) { // @codeCoverageIgnore

/**
* Alias of var_dump()
*
* @param mixed ...$vars
*
* @psalm-suppress ForbiddenCode Unsafe var_dump
*/
function d(...$vars): void
{
var_dump(...$vars);
}
}
23 changes: 23 additions & 0 deletions tests/DebugHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Kenjis\PhpUnitHelper;

use PHPUnit\Framework\TestCase;

class DebugHelperTest extends TestCase
{
use DebugHelper;

public function test_d(): void
{
$this->expectOutputRegex(
'/string\(4\) "var1"\n*.*\nint\(100\)\n/'
);

$var1 = 'var1';
$var2 = 100;
d($var1, $var2);
}
}

0 comments on commit 51e01dc

Please sign in to comment.