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

[TASK] Use RenderingContext->setAttribute() #922

Merged
merged 1 commit into from
Jul 21, 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: 2 additions & 4 deletions src/Core/Rendering/RenderingContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,14 +365,12 @@ public function buildParserConfiguration()
return $parserConfiguration;
}

public function withAttribute(string $className, object $value): static
public function setAttribute(string $className, object $value): void
{
if (!$value instanceof $className) {
throw new \RuntimeException('$value is not an instance of ' . $className, 1719410580);
}
$clonedObject = clone $this;
$clonedObject->attributes[$className] = $value;
return $clonedObject;
$this->attributes[$className] = $value;
}

public function hasAttribute(string $className): bool
Expand Down
8 changes: 5 additions & 3 deletions src/Core/Rendering/RenderingContextInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,18 @@ public function getControllerAction();
public function setControllerAction($action);

/**
* Return an instance with the specified attribute being attached.
* Add an object to this instance.
*
* This method allows you to attach arbitrary objects to the
* rendering context to be used later e. g. in ViewHelpers.
* rendering context to be used later e.g. in ViewHelpers.
*
* A typical use case is to attach a ServerRequestInterface here.
*
* @template T of object
* @param class-string<T> $className
* @param T $value
*/
public function withAttribute(string $className, object $value): RenderingContextInterface;
public function setAttribute(string $className, object $value): void;

/**
* Return true if an attribute object of that type exists.
Expand Down
13 changes: 7 additions & 6 deletions tests/Unit/Core/Rendering/RenderingContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ public function isCacheEnabledReturnsTrueIfCacheIsEnabled(): void
}

#[Test]
public function withAttributeThrowsIfValueIsNotInstanceofClassName(): void
public function setAttributeThrowsIfValueIsNotInstanceofClassName(): void
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionCode(1719410580);
(new RenderingContext())->withAttribute(RenderingContext::class, new stdClass());
(new RenderingContext())->setAttribute(RenderingContext::class, new stdClass());
}

#[Test]
Expand All @@ -114,7 +114,8 @@ public function hasAttributeReturnsFalseIfNotSet(): void
#[Test]
public function hasAttributeReturnsTrueIfSet(): void
{
$subject = (new RenderingContext())->withAttribute(stdClass::class, new stdClass());
$subject = new RenderingContext();
$subject->setAttribute(stdClass::class, new stdClass());
self::assertTrue($subject->hasAttribute(stdClass::class));
}

Expand All @@ -127,11 +128,11 @@ public function getAttributeThrowsWithNoSuchAttribute(): void
}

#[Test]
public function getAttributeReturnsInstanceSetUsingWithAttribute(): void
public function getAttributeReturnsInstanceSetUsingSetAttribute(): void
{
$object = new stdClass();
$subject = new RenderingContext();
$clonedSubject = $subject->withAttribute(stdClass::class, $object);
self::assertEquals($object, $clonedSubject->getAttribute(stdClass::class));
$subject->setAttribute(stdClass::class, $object);
self::assertEquals($object, $subject->getAttribute(stdClass::class));
}
}