Skip to content

Commit

Permalink
NestedAccessor::set(): bug fixed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed Jun 16, 2023
1 parent b8db069 commit 107bf9b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Components/NestedAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ public function set($path, $value): self
$source = &$this->getRef($this->getPathStack($path));

if ($source instanceof ProxyInterface) {
$source->setValue($value);
try {
$source->setValue($value);
} catch (\BadMethodCallException $e) {
[$key, $path] = $this->cutPathTail($path);
throw new PathNotWritableException($key, $path, $this->pathDelimiter);
}
} else {
$source = $value;
}
Expand Down
43 changes: 43 additions & 0 deletions tests/unit/NestedAccessor/NestedAccessorSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Smoren\Schemator\Tests\Unit\NestedAccessor;

use Smoren\Schemator\Components\NestedAccessor;
use Smoren\Schemator\Exceptions\PathNotWritableException;
use Smoren\Schemator\Tests\Unit\Fixtures\ClassWithAccessibleProperties;

class NestedAccessorSetTest extends \Codeception\Test\Unit
Expand Down Expand Up @@ -90,6 +91,12 @@ public function dataProviderForArray(): array
[1],
['a' => ['b' => ['c' => [0], 'd' => [1]]]],
],
[
['a' => ['b' => 1]],
['a', 'b', 'd'],
[1],
['a' => ['b' => ['d' => [1]]]],
],
];
}

Expand Down Expand Up @@ -260,4 +267,40 @@ public function dataProviderForObject(): array
],
];
}

/**
* @dataProvider dataProviderForPathNotWritable
*/
public function testPathNotWritable($source, $path, $value)
{
// Given
$accessor = new NestedAccessor($source);

// Then
$this->expectException(PathNotWritableException::class);

// When
$accessor->set($path, $value);
}

public function dataProviderForPathNotWritable(): array
{
return [
[
new ClassWithAccessibleProperties(),
'protectedProperty',
22,
],
[
new ClassWithAccessibleProperties(),
'privateProperty',
22,
],
[
1,
'test',
22,
],
];
}
}

0 comments on commit 107bf9b

Please sign in to comment.