php - How to override var_export in PHPUnit? -


in phpunit, there times when assertion fails, , when reporting assertion, phpunit automatically calls var_export() on variable. let's called chunk of code:

$foo = new stdclass(); $foo->bar = 123; $foo->baz = "hi there"; $this->asserttrue($foo); 

the output of is:

failed asserting  stdclass object (    [bar] => 123    [baz] => hi there ) true. 

if variable exception object, can cause unit test print out megabytes of text when walking object tree, including stack traces , other info. phpunit dies 'cause runs out of memory trying output all.

i know 1 solution add additional test, checking if variable object before doing asserttrue, or assertequals. team has lot of unit tests.

so wondering if there's way override phpunit's default behavior of calling var_export on variable when generating error report.

thanks in advance.

after digging around didn't find flag or achieve tried work backwards code:

the code in question (the print_r's) live in: phpunit_framework_comparisonfailure_object's tostring() method , don't see way of providing implementation class without changeing code in phpunit (since object gets created in static call)

so sake of debugging change in place ofc. leads usual problems when changing 3rd party code , since said you've got lot of code might not want since rely on phpunit working expects.

a way not rely on changeing phpunit code , might less hassle going through tests , changeing asserttrue() calls might along lines:

if tests use 1 base class it's easier put in:

<?php  class errortest extends phpunit_framework_testcase {      public function test1() {         $foo = new stdclass();         $foo->bar = 123;         $foo->baz = "hi there";         $this->asserttrue($foo);     }      public static function asserttrue($x) {         if(is_object($x)) {             self::fail("expecting true, got object of type: ".get_class($x));         }         parent::asserttrue($x);     } } 

->

phpunit errortest.php phpunit 3.4.15 sebastian bergmann.  f  time: 0 seconds, memory: 4.25mb  there 1 failure:  1) errortest::test1 expecting true, got object of type: stdclass  /home/mcsvnls/errortest.php:15 /home/mcsvnls/errortest.php:9  failures! tests: 1, assertions: 0, failures: 1. 

while i'm not sure thats "good" solution it's best come right now, maybe helps :)


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -