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

Commit 7c96a2d

Browse files
authored
Add StrictAssertionTrait (#3)
1 parent c6c3bd5 commit 7c96a2d

4 files changed

Lines changed: 240 additions & 0 deletions

File tree

src/Reporter.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/*
4+
* This file is part of PHPUnit Good Practices.
5+
*
6+
* (c) Dariusz Rumiński <dariusz.ruminski@gmail.com>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace PHPUnitGoodPractices;
13+
14+
use Closure;
15+
16+
final class Reporter
17+
{
18+
/**
19+
* @var ?Closure
20+
*/
21+
private static $customReporter;
22+
23+
/**
24+
* @var Closure
25+
*/
26+
private static $defaultReporter;
27+
28+
/**
29+
* @var bool
30+
*/
31+
private static $useHeader = true;
32+
33+
/**
34+
* @param $reporter ?Closure
35+
*/
36+
public static function setCustomReporter(Closure $reporter)
37+
{
38+
self::$customReporter = $reporter;
39+
}
40+
41+
public static function clearCustomReporter()
42+
{
43+
self::$customReporter = null;
44+
}
45+
46+
/**
47+
* @param $use bool
48+
*/
49+
public static function useHeader($use)
50+
{
51+
self::$useHeader = $use;
52+
}
53+
54+
/**
55+
* @param $issue string
56+
*/
57+
public static function report($issue)
58+
{
59+
if (self::$useHeader) {
60+
$issue = "PHPUnit good practice has been abused.\n$issue";
61+
}
62+
63+
$reporter = self::$customReporter ? self::$customReporter : self::getDefaultReporter();
64+
$reporter($issue);
65+
}
66+
67+
/**
68+
* @return Closure
69+
*/
70+
private static function getDefaultReporter()
71+
{
72+
if (null === self::$defaultReporter) {
73+
self::$defaultReporter = function ($issue) { trigger_error($issue, E_USER_WARNING); };
74+
}
75+
76+
return self::$defaultReporter;
77+
}
78+
}

src/StrictAssertionTrait.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of PHPUnit Good Practices.
5+
*
6+
* (c) Dariusz Rumiński <dariusz.ruminski@gmail.com>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace PHPUnitGoodPractices;
13+
14+
trait StrictAssertionTrait
15+
{
16+
public static function assertEquals($expected, $actual, $message = '', $delta = 0.0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false)
17+
{
18+
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
19+
20+
// internally, PHPUnit calls `assertEquals` instead of `assertSame` internally, we allow that
21+
if ('assertSame' !== $trace[1]['function']) {
22+
Reporter::report('Use `->assertSame()` instead of `->assertEquals()`.');
23+
}
24+
25+
return call_user_func_array(array('parent', __FUNCTION__), func_get_args());
26+
}
27+
}

tests/ReporterTest.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/*
4+
* This file is part of PHPUnit Good Practices.
5+
*
6+
* (c) Dariusz Rumiński <dariusz.ruminski@gmail.com>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace PHPUnitGoodPractices\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use PHPUnitGoodPractices\Reporter;
16+
17+
/**
18+
* @covers \PHPUnitGoodPractices\Reporter
19+
*/
20+
final class ReporterTest extends TestCase
21+
{
22+
public function tearDown()
23+
{
24+
// reset global `Reporter` state
25+
Reporter::useHeader(true);
26+
Reporter::clearCustomReporter();
27+
}
28+
29+
public function testReportWithDefaults()
30+
{
31+
$expectedMessage = "PHPUnit good practice has been abused.\nFoo.";
32+
33+
if (is_callable(array($this, 'expectException'))) {
34+
$this->expectException(\PHPUnit_Framework_Error_Warning::class);
35+
$this->expectExceptionMessage($expectedMessage);
36+
} else {
37+
$this->setExpectedException(\PHPUnit_Framework_Error_Warning::class, $expectedMessage);
38+
}
39+
40+
Reporter::report('Foo.');
41+
}
42+
43+
public function testReportWithoutHeaders()
44+
{
45+
Reporter::useHeader(false);
46+
47+
$expectedMessage = 'Foo.';
48+
49+
if (is_callable(array($this, 'expectException'))) {
50+
$this->expectException(\PHPUnit_Framework_Error_Warning::class);
51+
$this->expectExceptionMessage($expectedMessage);
52+
} else {
53+
$this->setExpectedException(\PHPUnit_Framework_Error_Warning::class, $expectedMessage);
54+
}
55+
56+
Reporter::report('Foo.');
57+
}
58+
59+
public function testReportWithCustomReporter()
60+
{
61+
$counter = 0;
62+
$customReporter = function () use (&$counter) { ++$counter; };
63+
64+
Reporter::setCustomReporter($customReporter);
65+
Reporter::report('Foo.');
66+
$this->assertSame(1, $counter); // custom reporter shall be triggered
67+
}
68+
69+
public function testReportAfterClearingCustomReporter()
70+
{
71+
$customReporter = function () { };
72+
73+
Reporter::setCustomReporter($customReporter);
74+
Reporter::report('Foo.');
75+
76+
Reporter::clearCustomReporter();
77+
if (is_callable(array($this, 'expectException'))) {
78+
$this->expectException(\PHPUnit_Framework_Error_Warning::class);
79+
} else {
80+
$this->setExpectedException(\PHPUnit_Framework_Error_Warning::class);
81+
}
82+
Reporter::report('Foo.');
83+
}
84+
}

tests/StrictAssertionTraitTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* This file is part of PHPUnit Good Practices.
5+
*
6+
* (c) Dariusz Rumiński <dariusz.ruminski@gmail.com>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace PHPUnitGoodPractices\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use PHPUnitGoodPractices\StrictAssertionTrait;
16+
17+
/**
18+
* @covers \PHPUnitGoodPractices\StrictAssertionTrait
19+
*/
20+
final class StrictAssertionTraitTest extends TestCase
21+
{
22+
use StrictAssertionTrait;
23+
24+
public function testAssertSameWorks()
25+
{
26+
$data = 5;
27+
28+
$this->assertSame($data, $data);
29+
}
30+
31+
public function testAssertSameWorksForBooleans()
32+
{
33+
$data = true;
34+
35+
// assertSame has special handling when both params are booleans and it calls `assertEquals` under the hood
36+
$this->assertSame($data, $data);
37+
}
38+
39+
public function testAssertEqualsFails()
40+
{
41+
$data = 5;
42+
43+
if (is_callable(array($this, 'expectException'))) {
44+
$this->expectException(\PHPUnit_Framework_Error_Warning::class);
45+
} else {
46+
$this->setExpectedException(\PHPUnit_Framework_Error_Warning::class);
47+
}
48+
49+
$this->assertEquals($data, $data);
50+
}
51+
}

0 commit comments

Comments
 (0)