Skip to content

Commit 2035d6c

Browse files
committed
Troubleshoot unit tests
1 parent 5449e1f commit 2035d6c

5 files changed

Lines changed: 56 additions & 14 deletions

File tree

src/Backtrace/Backtrace.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public static function getCallerInfo($offset = 0, $options = 0)
112112
we need to collect object... we'll remove object at end if undesired
113113
*/
114114
$phpOptions = static::translateOptions($options | self::INCL_OBJECT);
115-
$backtrace = \debug_backtrace($phpOptions, 50);
115+
$backtrace = \debug_backtrace($phpOptions, 60);
116116
$backtrace = self::normalize($backtrace);
117117
$index = SkipInternal::getFirstIndex($backtrace, $offset);
118118
$index = \max($index, 1); // ensure we're >= 1

src/Debug/Plugin/Method/Table.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ class Table implements SubscriberInterface
5757
*/
5858
public function __construct()
5959
{
60-
$this->tableFactory = new TableFactory(array(
61-
'getValInfo' => [$this, 'getValInfo'],
62-
));
6360
}
6461

6562
/**
@@ -174,7 +171,7 @@ private function doTableLogEntry(LogEntry $logEntry)
174171
{
175172
$this->initLogEntry($logEntry);
176173

177-
$table = $this->tableFactory->create(
174+
$table = $this->getTableFactory()->create(
178175
isset($logEntry['args'][0])
179176
? $logEntry['args'][0]
180177
: null,
@@ -198,6 +195,21 @@ private function doTableLogEntry(LogEntry $logEntry)
198195
$this->removeTableMetaFromLogEntry($logEntry);
199196
}
200197

198+
/**
199+
* Get or create TableFactory instance
200+
*
201+
* @return TableFactory
202+
*/
203+
private function getTableFactory()
204+
{
205+
if ($this->tableFactory === null) {
206+
$this->tableFactory = new TableFactory(array(
207+
'getValInfo' => [$this, 'getValInfo'],
208+
));
209+
}
210+
return $this->tableFactory;
211+
}
212+
201213
/**
202214
* Find the data, caption, & columns in logEntry arguments
203215
*

src/Table/Element.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function __serialize()
7575
{
7676
$data = array(
7777
'attribs' => $this->attribs,
78-
'children' => \array_map(static function (self $child) {
78+
'children' => \array_map(static function ($child) {
7979
$data = $child->__serialize();
8080
if (\count($data) === 1) {
8181
return \reset($data);
@@ -138,7 +138,7 @@ public function getHtml()
138138
{
139139
$children = $this->getChildren();
140140
if ($children) {
141-
$innerHtml = "\n" . \implode('', \array_map(static function (self $child) {
141+
$innerHtml = "\n" . \implode('', \array_map(static function ($child) {
142142
return $child->getOuterHtml() . "\n";
143143
}, $children));
144144
if ($this->cfg['indent']) {
@@ -255,6 +255,9 @@ public function addClass($class)
255255
*/
256256
public function appendChild(self $child)
257257
{
258+
if (!($child instanceof self)) {
259+
throw new InvalidArgumentException('Child must be an instance of ' . __CLASS__);
260+
}
258261
$child->setParent($this);
259262
$this->children[] = $child;
260263
return $this;

src/Table/Factory.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use bdk\Debug\Utility\Php;
66
use bdk\Debug\Utility\PhpType;
77
use bdk\Table\TableRow;
8+
use Exception;
9+
use Throwable;
810

911
/**
1012
* Create Table structure via iterable (array or object)
@@ -307,15 +309,17 @@ private function getObjectValuesKeys($obj, $keys = [])
307309
{
308310
$valsAll = $this->getObjectValues($obj);
309311
\set_error_handler(static function ($type, $message) {
310-
throw new \Exception($message);
312+
throw new Exception($message);
311313
});
312314
$vals = array();
313315
foreach ($keys as $key) {
314316
try {
315317
$vals[$key] = \array_key_exists($key, $valsAll)
316318
? $valsAll[$key]
317319
: $obj->{$key};
318-
} catch (\Throwable $e) {
320+
} catch (Exception $e) {
321+
$vals[$key] = self::VAL_UNDEFINED;
322+
} catch (Throwable $e) {
319323
$vals[$key] = self::VAL_UNDEFINED;
320324
}
321325
}

tests/Table/ElementTest.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
use bdk\Table\Element;
66
use bdk\PhpUnitPolyfill\AssertionTrait;
7+
use ErrorNotice;
8+
use InvalidArgumentException;
79
use PHPUnit\Framework\TestCase;
10+
use RuntimeException;
11+
use TypeError;
812

913
/**
1014
* PHPUnit tests for bdk\Table\Element
@@ -302,11 +306,10 @@ public function testSetChildren()
302306
*/
303307
public function testSetChildrenInvalid()
304308
{
305-
$this->expectException('TypeError');
306-
// $this->expectExceptionMessage('Children must be instances of');
307-
308-
$parent = new Element('div');
309-
$parent->setChildren(['not an Element object']);
309+
self::assertExceptionOrTypeError(function () {
310+
$parent = new Element('div');
311+
$parent->setChildren(['not an Element object']);
312+
});
310313
}
311314

312315
/**
@@ -883,4 +886,24 @@ public function testGetHtmlPrioritizesChildren()
883886
$html = $parent->getHtml();
884887
self::assertStringContainsString('<span>Child</span>', $html);
885888
}
889+
890+
protected static function assertExceptionOrTypeError($callable)
891+
{
892+
try {
893+
$callable();
894+
} catch (ErrorNotice $e) {
895+
self::assertSame('A non well formed numeric value encountered', $e->getMessage());
896+
return;
897+
} catch (RuntimeException $e) {
898+
self::assertSame('A non well formed numeric value encountered', $e->getMessage());
899+
return;
900+
} catch (InvalidArgumentException $e) {
901+
self::assertTrue(true);
902+
return;
903+
} catch (TypeError $e) {
904+
self::assertSame(\get_class($e), 'TypeError');
905+
return;
906+
}
907+
throw new AssertionFailedError('Exception not thrown');
908+
}
886909
}

0 commit comments

Comments
 (0)