Skip to content

Commit

Permalink
Merge pull request #5 from prosopo/main
Browse files Browse the repository at this point in the history
v 1.0.3
  • Loading branch information
light-source authored Dec 19, 2024
2 parents 4315d2a + 0f21796 commit 7fbc426
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
6 changes: 5 additions & 1 deletion private-classes/Object/ObjectReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ protected function getPropertyValues(object $instance, array $reflectionProperti
return array_reduce(
$reflectionProperties,
function (array $variableValues, ReflectionProperty $reflection_property) use ($instance) {
$variableValues[ $reflection_property->getName() ] = $reflection_property->getValue($instance);
// make sure the property is initialized.
// Otherwise, we'll get "must not be accessed before initialization" error.
if (true === $reflection_property->isInitialized($instance)) {
$variableValues[ $reflection_property->getName() ] = $reflection_property->getValue($instance);
}

return $variableValues;
},
Expand Down
30 changes: 30 additions & 0 deletions tests/pest/Feature/ViewsManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,36 @@ public function testMakeModelNotSetDefaultsForModelsWithoutDefaultsInterface():
$this->assertFalse(isset($model->message));
}

public function testMakeModelNotSetDefaultsForInnerInterfacedModel(): void
{
// given
$bladeRenderer = new ViewTemplateRenderer();
$namespaceConfig = (new ViewNamespaceConfig($bladeRenderer));
$views = new ViewsManager();

$modelNamespace = $this->defineRealModelClass(
__METHOD__,
'FirstModel',
[
[
'name' => 'inner',
'type' => '\\' . TemplateModelInterface::class,
'visibility' => 'public',
]
],
true
);

// when
$views->registerNamespace($modelNamespace, $namespaceConfig);

$modelClass = $modelNamespace . '\\FirstModel';
$model = $views->createModel($modelClass);

// then
$this->assertFalse(isset($model->inner));
}

public function testMakeModelSupportsDifferentNamespaces(): void
{
// given
Expand Down
18 changes: 18 additions & 0 deletions tests/pest/Unit/Object/ObjectReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,22 @@ public function someMethod(): string
'someMethod' => [$testInstance, 'someMethod'],
], $variables);
}

public function testExcludesNotInitializedProperties(): void
{
// given
$objectReader = new ObjectReader();
$testInstance = new class {
public int $typedProperty = 1;
public int $typedButNotInitializedProperty;
};

// when
$variables = $objectReader->extractObjectVariables($testInstance);

// then
$this->assertEquals([
'typedProperty' => 1,
], $variables);
}
}

0 comments on commit 7fbc426

Please sign in to comment.