Skip to content

Commit 51e01dc

Browse files
authored
Merge pull request #3 from kenjis/add-dd
Add dd() and d()
2 parents 19638f5 + 2435546 commit 51e01dc

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Provides helper traits for PHPUnit.
2323
- [`$this->getPrivateProperty()`](#this-getprivateproperty)
2424
- [`$this->setPrivateProperty()`](#this-setprivateproperty)
2525
- [`$this->getPrivateMethodInvoker()`](#this-getprivatemethodinvoker)
26+
- [`DebugHelper`](#debughelper)
2627
- [License](#license)
2728

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

343+
### `DebugHelper`
344+
345+
This trait provides helper functions, `dd()` and `d()` to dump variables.
346+
347+
Import the `Kenjis\PhpUnitHelper\DebugHelper` trait into your test class:
348+
349+
```php
350+
<?php
351+
352+
declare(strict_types=1);
353+
354+
namespace Foo\Bar\Test\Unit;
355+
356+
use Kenjis\PhpUnitHelper\DebugHelper;
357+
use PHPUnit\Framework;
358+
359+
final class BazTest extends Framework\TestCase
360+
{
361+
use DebugHelper;
362+
}
363+
```
364+
342365
## License
343366

344367
This package is licensed using the MIT License.

src/DebugHelper.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Kenjis\PhpUnitHelper;
6+
7+
require_once __DIR__ . '/functions.php'; // @codeCoverageIgnore
8+
9+
trait DebugHelper
10+
{
11+
}

src/functions.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/**
4+
* Helper Functions
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
/**
10+
* @SuppressWarnings(PHPMD.ExitExpression)
11+
* Does not work if I move to the function annotation
12+
* See https://github.com/phpmd/phpmd/issues/337
13+
*/
14+
if (! function_exists('dd')) { // @codeCoverageIgnore
15+
16+
/**
17+
* Alias of var_dump() and exit()
18+
*
19+
* @param mixed ...$vars
20+
*
21+
* @psalm-suppress ForbiddenCode Unsafe var_dump
22+
* @codeCoverageIgnore
23+
*/
24+
function dd(...$vars): void
25+
{
26+
var_dump(...$vars);
27+
exit;
28+
}
29+
}
30+
31+
if (! function_exists('d')) { // @codeCoverageIgnore
32+
33+
/**
34+
* Alias of var_dump()
35+
*
36+
* @param mixed ...$vars
37+
*
38+
* @psalm-suppress ForbiddenCode Unsafe var_dump
39+
*/
40+
function d(...$vars): void
41+
{
42+
var_dump(...$vars);
43+
}
44+
}

tests/DebugHelperTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Kenjis\PhpUnitHelper;
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
class DebugHelperTest extends TestCase
10+
{
11+
use DebugHelper;
12+
13+
public function test_d(): void
14+
{
15+
$this->expectOutputRegex(
16+
'/string\(4\) "var1"\n*.*\nint\(100\)\n/'
17+
);
18+
19+
$var1 = 'var1';
20+
$var2 = 100;
21+
d($var1, $var2);
22+
}
23+
}

0 commit comments

Comments
 (0)