| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\Tests\DebugInfo; |
| 4 |
|
| 5 |
use PHPUnit\Framework\TestCase; |
| 6 |
use stdClass; |
| 7 |
use Wikimedia\DebugInfo\DebugInfoTrait; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class DebugInfoTraitTest extends TestCase { |
| 14 |
public function setUp(): void { |
| 15 |
if ( extension_loaded( 'xdebug' ) ) { |
| 16 |
if ( version_compare( phpversion( 'xdebug' ), '3.0.0', '>=' ) ) { |
| 17 |
if ( str_contains( ini_get( 'xdebug.mode' ), 'develop' ) ) { |
| 18 |
$this->markTestSkipped( 'Can\'t run this test with xdebug.mode=develop. ' . |
| 19 |
'Use xdebug.mode=coverage to do test coverage without overloading var_dump.' ); |
| 20 |
} |
| 21 |
} else { |
| 22 |
$this->iniSet( 'xdebug.overload_var_dump', 0 ); |
| 23 |
} |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
public function testVarDump1() { |
| 28 |
$test1 = new Test1; |
| 29 |
ob_start(); |
| 30 |
var_dump( $test1 ); |
| 31 |
$result = ob_get_clean(); |
| 32 |
$format = <<<TEXT |
| 33 |
object(Wikimedia\Tests\DebugInfo\Test1)#%d (5) { |
| 34 |
["privateDump":"Wikimedia\Tests\DebugInfo\Test1":private]=> |
| 35 |
object(stdClass)#%d (0) { |
| 36 |
} |
| 37 |
["protectedDump":protected]=> |
| 38 |
object(stdClass)#%d (0) { |
| 39 |
} |
| 40 |
["privateNoDump":"Wikimedia\Tests\DebugInfo\Test1":private]=> |
| 41 |
object(Wikimedia\DebugInfo\Placeholder)#%d (1) { |
| 42 |
["desc"]=> |
| 43 |
string(%d) "stdClass#%d" |
| 44 |
} |
| 45 |
["protectedNoDump":protected]=> |
| 46 |
object(Wikimedia\DebugInfo\Placeholder)#%d (1) { |
| 47 |
["desc"]=> |
| 48 |
string(%d) "stdClass#%d" |
| 49 |
} |
| 50 |
["scalarDumpAllowed":"Wikimedia\Tests\DebugInfo\Test1":private]=> |
| 51 |
int(1) |
| 52 |
} |
| 53 |
TEXT; |
| 54 |
$this->assertStringMatchesFormat( trim( $format ), trim( $result ) ); |
| 55 |
} |
| 56 |
|
| 57 |
public function testVarDump2() { |
| 58 |
$test1 = new Test2; |
| 59 |
ob_start(); |
| 60 |
var_dump( $test1 ); |
| 61 |
$result = trim( ob_get_clean() ); |
| 62 |
|
| 63 |
$oldFormat = <<<TEXT |
| 64 |
object(Wikimedia\Tests\DebugInfo\Test2)#%d (9) { |
| 65 |
["privateDump":"Wikimedia\Tests\DebugInfo\Test2":private]=> |
| 66 |
object(stdClass)#%d (0) { |
| 67 |
} |
| 68 |
["protected2Dump":protected]=> |
| 69 |
object(stdClass)#%d (0) { |
| 70 |
} |
| 71 |
["privateNoDump":"Wikimedia\Tests\DebugInfo\Test2":private]=> |
| 72 |
object(Wikimedia\DebugInfo\Placeholder)#%d (1) { |
| 73 |
["desc"]=> |
| 74 |
string(%d) "stdClass#%d" |
| 75 |
} |
| 76 |
["protected2NoDump":protected]=> |
| 77 |
object(Wikimedia\DebugInfo\Placeholder)#%d (1) { |
| 78 |
["desc"]=> |
| 79 |
string(%d) "stdClass#%d" |
| 80 |
} |
| 81 |
["protectedDump":protected]=> |
| 82 |
object(stdClass)#%d (0) { |
| 83 |
} |
| 84 |
["protectedNoDump":protected]=> |
| 85 |
object(Wikimedia\DebugInfo\Placeholder)#%d (1) { |
| 86 |
["desc"]=> |
| 87 |
string(%d) "Wikimedia\DebugInfo\Placeholder#%d" |
| 88 |
} |
| 89 |
["scalarDumpAllowed":"Wikimedia\Tests\DebugInfo\Test1":private]=> |
| 90 |
int(1) |
| 91 |
["privateDump":"Wikimedia\Tests\DebugInfo\Test1":private]=> |
| 92 |
object(stdClass)#%d (0) { |
| 93 |
} |
| 94 |
["privateNoDump":"Wikimedia\Tests\DebugInfo\Test1":private]=> |
| 95 |
object(Wikimedia\DebugInfo\Placeholder)#%d (1) { |
| 96 |
["desc"]=> |
| 97 |
string(%d) "stdClass#%d" |
| 98 |
} |
| 99 |
} |
| 100 |
TEXT; |
| 101 |
|
| 102 |
|
| 103 |
$newFormat = <<<TEXT |
| 104 |
object(Wikimedia\Tests\DebugInfo\Test2)#%d (9) { |
| 105 |
["privateDump":"Wikimedia\Tests\DebugInfo\Test1":private]=> |
| 106 |
object(stdClass)#%d (0) { |
| 107 |
} |
| 108 |
["protectedDump":protected]=> |
| 109 |
object(stdClass)#%d (0) { |
| 110 |
} |
| 111 |
["privateNoDump":"Wikimedia\Tests\DebugInfo\Test1":private]=> |
| 112 |
object(Wikimedia\DebugInfo\Placeholder)#%d (1) { |
| 113 |
["desc"]=> |
| 114 |
string(%d) "stdClass#%d" |
| 115 |
} |
| 116 |
["protectedNoDump":protected]=> |
| 117 |
object(Wikimedia\DebugInfo\Placeholder)#%d (1) { |
| 118 |
["desc"]=> |
| 119 |
string(%d) "Wikimedia\DebugInfo\Placeholder#%d" |
| 120 |
} |
| 121 |
["scalarDumpAllowed":"Wikimedia\Tests\DebugInfo\Test1":private]=> |
| 122 |
int(1) |
| 123 |
["privateDump":"Wikimedia\Tests\DebugInfo\Test2":private]=> |
| 124 |
object(stdClass)#%d (0) { |
| 125 |
} |
| 126 |
["protected2Dump":protected]=> |
| 127 |
object(stdClass)#%d (0) { |
| 128 |
} |
| 129 |
["privateNoDump":"Wikimedia\Tests\DebugInfo\Test2":private]=> |
| 130 |
object(Wikimedia\DebugInfo\Placeholder)#%d (1) { |
| 131 |
["desc"]=> |
| 132 |
string(%d) "stdClass#%d" |
| 133 |
} |
| 134 |
["protected2NoDump":protected]=> |
| 135 |
object(Wikimedia\DebugInfo\Placeholder)#%d (1) { |
| 136 |
["desc"]=> |
| 137 |
string(%d) "stdClass#%d" |
| 138 |
} |
| 139 |
} |
| 140 |
TEXT; |
| 141 |
|
| 142 |
$this->assertThat( |
| 143 |
trim( $result ), |
| 144 |
$this->logicalOr( |
| 145 |
$this->matches( trim( $oldFormat ) ), |
| 146 |
$this->matches( trim( $newFormat ) ) |
| 147 |
) |
| 148 |
); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
class Test1 { |
| 154 |
use DebugInfoTrait; |
| 155 |
|
| 156 |
private $privateDump; |
| 157 |
protected $protectedDump; |
| 158 |
|
| 159 |
private $privateNoDump; |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
protected $protectedNoDump; |
| 164 |
|
| 165 |
private $scalarDumpAllowed = 1; |
| 166 |
|
| 167 |
public function __construct() { |
| 168 |
$this->privateDump = new stdClass; |
| 169 |
$this->protectedDump = new stdClass; |
| 170 |
$this->privateNoDump = new stdClass; |
| 171 |
$this->protectedNoDump = new stdClass; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
class Test2 extends Test1 { |
| 176 |
use DebugInfoTrait; |
| 177 |
|
| 178 |
private $privateDump; |
| 179 |
protected $protected2Dump; |
| 180 |
|
| 181 |
private $privateNoDump; |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
protected $protected2NoDump; |
| 186 |
|
| 187 |
public function __construct() { |
| 188 |
parent::__construct(); |
| 189 |
$this->privateDump = new stdClass; |
| 190 |
$this->protected2Dump = new stdClass; |
| 191 |
$this->privateNoDump = new stdClass; |
| 192 |
$this->protected2NoDump = new stdClass; |
| 193 |
} |
| 194 |
} |
| 195 |
|