Skip to content

Commit

Permalink
tests: added cache trait tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrepimpao committed May 13, 2024
1 parent fa8bcdf commit d85a3b8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Resource/Util/CacheTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ trait CacheTrait
{
public function withCacheTtl(?int $ttl): static
{
$clone = deep_copy($this);
$clone = deep_copy($this, true);
$clone->api->getCacheBuilder()?->setTtl($ttl);

return $clone;
Expand Down
2 changes: 1 addition & 1 deletion src/Resource/Util/LanguageTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function withLanguage(string $language): static
{
$this->validateLanguage($language);

$clone = deep_copy($this);
$clone = deep_copy($this, true);
$clone->api->addQueryDefault('lang', $language);

return $clone;
Expand Down
2 changes: 1 addition & 1 deletion src/Resource/Util/UnitSystemTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function withUnitSystem(string $unitSystem): static
{
$this->validateUnitSystem($unitSystem);

$clone = deep_copy($this);
$clone = deep_copy($this, true);
$clone->api->addQueryDefault('units', $unitSystem);

return $clone;
Expand Down
39 changes: 39 additions & 0 deletions tests/Integration/CacheTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace ProgrammatorDev\OpenWeatherMap\Test\Integration;

use ProgrammatorDev\Api\Builder\CacheBuilder;
use ProgrammatorDev\OpenWeatherMap\Resource\Resource;
use ProgrammatorDev\OpenWeatherMap\Resource\Util\CacheTrait;
use ProgrammatorDev\OpenWeatherMap\Test\AbstractTest;
use Psr\Cache\CacheItemPoolInterface;

class CacheTraitTest extends AbstractTest
{
private Resource $resource;

protected function setUp(): void
{
parent::setUp();

$pool = $this->createMock(CacheItemPoolInterface::class);
$cacheBuilder = new CacheBuilder($pool);

$this->api->setCacheBuilder($cacheBuilder);

$this->resource = new class($this->api) extends Resource {
use CacheTrait;

public function getCacheTtl(): ?int
{
return $this->api->getCacheBuilder()?->getTtl();
}
};
}

public function testMethods(): void
{
$this->assertSame(600, $this->resource->withCacheTtl(600)->getCacheTtl());
$this->assertSame(60, $this->resource->getCacheTtl()); // back to default value
}
}

0 comments on commit d85a3b8

Please sign in to comment.