Skip to content

Commit

Permalink
fix exposer nesting bug
Browse files Browse the repository at this point in the history
  • Loading branch information
jturbide committed Nov 19, 2023
1 parent 7b17f25 commit cf58f04
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/Support/Exposer/Exposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private static function checkExpose(Builder $builder): void
// Otherwise, check if a parent key exists
else {
$parentKey = $fullKey;
$parentIndex = false;
$parentIndex = strrpos($parentKey, '.');
do {
$parentKey = $parentIndex ? substr($parentKey, 0, $parentIndex) : '';
if (isset($columns[$parentKey])) {
Expand Down
83 changes: 58 additions & 25 deletions tests/Unit/ExposerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,67 +40,67 @@ public function testBuilder(): void
['test' => 'test'],
(object)['test' => 'test'],
];

foreach ($tests as $key => $value) {
$test = $key;
$expected = $value;
if (is_int($key)) {
$test = $value;
}

// value
$builder->setValue($test);
$this->assertEquals($test, $builder->getValue());

// parent
$builder->setParent($test);
$this->assertEquals($test, $builder->getParent());

if (is_string($test)) {
// key
$this->assertEquals($expected, Builder::processKey($test));

$builder->setKey($test);
$this->assertEquals($expected, $builder->getKey());

// context key
$builder->setContextKey($test);
$this->assertEquals($expected, $builder->getContextKey());

// full key
$this->assertEquals(trim($expected . '.' . $expected, '.'), $builder->getFullKey());
}

if (is_bool($test)) {
// expose
$builder->setExpose($test);
$this->assertEquals($test, $builder->getExpose());

// protected
$builder->setProtected($test);
$this->assertEquals($test, $builder->getProtected());
}

// columns
if (is_array($test) || is_null($test)) {
$builder->setColumns($test);
$this->assertEquals($test, $builder->getColumns());
}
}

// Expose
$builder->setExpose(true);
$this->assertTrue($builder->getExpose());
$builder->setExpose(false);
$this->assertFalse($builder->getExpose());

// Protected
$builder->setProtected(true);
$this->assertTrue($builder->getProtected());
$builder->setProtected(false);
$this->assertFalse($builder->getProtected());
}

public function testExposer(): void
{
$test = [
Expand All @@ -117,7 +117,8 @@ public function testExposer(): void
'test_object' => (object)['test' => 'test'],
'test_removed' => 'test_removed',
'test_removed_two' => 'test_removed_two',
'test_after_removed' => 'test_after_removed'
'test_after_removed' => 'test_after_removed',
'test_replace_value' => 'test_value_before',
];
$expected = $test;
$expected['test_empty_object'] = (array)$expected['test_empty_object'];
Expand All @@ -128,22 +129,41 @@ public function testExposer(): void
$this->assertEquals($expected, $actual);

// @todo
// unset($expected['test_removed']);
// unset($expected['test_removed_two']);
unset($expected['test_removed']);
unset($expected['test_removed_two']);
$expected['test_replace_value'] = 'test_value_after';
// $expected['new_value'] = 'test';
// $builder = Exposer::createBuilder($test, [
// true,
// 'test_removed' => false,
// 'test_removed_two',
// 'new_value' => 'test'
// ]);
// $actual = Exposer::expose($builder);
// $this->assertEquals($expected, $actual);
$builder = Exposer::createBuilder($test, [
true,
'test_removed' => false,
'test_removed_two' => false,
'test_replace_value' => 'test_value_after',
'new_value' => 'test',
]);
$actual = Exposer::expose($builder);
$this->assertEquals($expected, $actual);
}

public function testNestedExpose(): void
{
$array = [
'test' => 'test',
'test_hidden' => 'hidden',
'nested' => [
[
'id' => 1,
'test' => 'test',
'test_hidden' => 'hidden',
],
[
'id' => 2,
'test' => 'test',
'test_hidden' => 'hidden',
],
],
];

$result = [
'test' => 'test',
'nested' => [
[
Expand All @@ -166,6 +186,19 @@ public function testNestedExpose(): void
],
]);
$actual = Exposer::expose($builder);
$this->assertEquals($array, $actual);
$this->assertEquals($result, $actual);

$builder = Exposer::createBuilder($array, [
true,
'test' => true,
'test_hidden' => false,
'nested' => [
false,
'id',
'test'
],
]);
$actual = Exposer::expose($builder);
$this->assertEquals($result, $actual);
}
}

0 comments on commit cf58f04

Please sign in to comment.