Skip to content

Commit d85a3b8

Browse files
committed
tests: added cache trait tests
1 parent fa8bcdf commit d85a3b8

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

src/Resource/Util/CacheTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ trait CacheTrait
88
{
99
public function withCacheTtl(?int $ttl): static
1010
{
11-
$clone = deep_copy($this);
11+
$clone = deep_copy($this, true);
1212
$clone->api->getCacheBuilder()?->setTtl($ttl);
1313

1414
return $clone;

src/Resource/Util/LanguageTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function withLanguage(string $language): static
1616
{
1717
$this->validateLanguage($language);
1818

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

2222
return $clone;

src/Resource/Util/UnitSystemTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function withUnitSystem(string $unitSystem): static
1616
{
1717
$this->validateUnitSystem($unitSystem);
1818

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

2222
return $clone;

tests/Integration/CacheTraitTest.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\OpenWeatherMap\Test\Integration;
4+
5+
use ProgrammatorDev\Api\Builder\CacheBuilder;
6+
use ProgrammatorDev\OpenWeatherMap\Resource\Resource;
7+
use ProgrammatorDev\OpenWeatherMap\Resource\Util\CacheTrait;
8+
use ProgrammatorDev\OpenWeatherMap\Test\AbstractTest;
9+
use Psr\Cache\CacheItemPoolInterface;
10+
11+
class CacheTraitTest extends AbstractTest
12+
{
13+
private Resource $resource;
14+
15+
protected function setUp(): void
16+
{
17+
parent::setUp();
18+
19+
$pool = $this->createMock(CacheItemPoolInterface::class);
20+
$cacheBuilder = new CacheBuilder($pool);
21+
22+
$this->api->setCacheBuilder($cacheBuilder);
23+
24+
$this->resource = new class($this->api) extends Resource {
25+
use CacheTrait;
26+
27+
public function getCacheTtl(): ?int
28+
{
29+
return $this->api->getCacheBuilder()?->getTtl();
30+
}
31+
};
32+
}
33+
34+
public function testMethods(): void
35+
{
36+
$this->assertSame(600, $this->resource->withCacheTtl(600)->getCacheTtl());
37+
$this->assertSame(60, $this->resource->getCacheTtl()); // back to default value
38+
}
39+
}

0 commit comments

Comments
 (0)