Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v 1.0.3 #5

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading