Skip to content

Commit

Permalink
Copy response in profilerItem not to store a reference to the origina…
Browse files Browse the repository at this point in the history
…l object
  • Loading branch information
MortalFlesh committed Jul 25, 2021
1 parent c1fbeb2 commit 7c928f9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<!-- There should always be "Unreleased" section at the beginning. -->

## Unreleased
- Copy `$response` in `ProfilerItem` not to store a reference to the original object

## 1.0.0 - 2021-05-12
- Initial implementation
17 changes: 14 additions & 3 deletions src/ValueObject/ProfilerItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public function __construct(
$this->itemType = $this->matchItemType($itemType);

$this->type = $type;
$this->response = $response;
$this->error = $error;
$this->setResponse($response);
$this->setError($error);
$this->cacheKey = $cacheKey;
$this->isLoadedFromCache = $isLoadedFromCache;
$this->isStoredInCache = $isStoredInCache;
Expand Down Expand Up @@ -112,7 +112,18 @@ public function getResponse()
/** @param mixed|FormattedValue<mixed, mixed> $response */
public function setResponse($response): void
{
$this->response = $response;
$this->response = $this->copy($response);
}

/**
* @param mixed $value
* @return mixed
*/
private function copy($value)
{
return is_object($value)
? clone $value
: $value;
}

/** @return \Throwable|FormattedValue<mixed, mixed>|null */
Expand Down
39 changes: 39 additions & 0 deletions tests/ValueObject/ProfilerItemTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php declare(strict_types=1);

namespace Lmc\Cqrs\Types\ValueObject;

use PHPUnit\Framework\TestCase;

class ProfilerItemTest extends TestCase
{
/**
* @test
*/
public function shouldProfileResponseAsItIsInThatMoment(): void
{
$response = new \stdClass();
$response->value = 'foo';

$profilerItem = new ProfilerItem('id', null, 'test', null, $response);

$response->value = 'fooBar';

$this->assertSame('foo', $profilerItem->getResponse()->value);
}

/**
* @test
*/
public function shouldProfileResponseAsItIsInThatMomentViaSetter(): void
{
$response = new \stdClass();
$response->value = 'foo';

$profilerItem = new ProfilerItem('id', null, 'test');
$profilerItem->setResponse($response);

$response->value = 'fooBar';

$this->assertSame('foo', $profilerItem->getResponse()->value);
}
}

0 comments on commit 7c928f9

Please sign in to comment.