Skip to content

Commit

Permalink
feat: added select, include and select traits
Browse files Browse the repository at this point in the history
  • Loading branch information
andrepimpao committed May 16, 2024
1 parent d15978f commit ba224f0
Show file tree
Hide file tree
Showing 14 changed files with 308 additions and 102 deletions.
35 changes: 0 additions & 35 deletions src/Endpoint/Util/FilterTrait.php

This file was deleted.

33 changes: 0 additions & 33 deletions src/Endpoint/Util/IncludeTrait.php

This file was deleted.

33 changes: 0 additions & 33 deletions src/Endpoint/Util/SelectTrait.php

This file was deleted.

10 changes: 9 additions & 1 deletion src/Resource/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@
namespace ProgrammatorDev\SportMonksFootball\Resource;

use ProgrammatorDev\SportMonksFootball\Resource\Util\CacheTrait;
use ProgrammatorDev\SportMonksFootball\Resource\Util\FilterTrait;
use ProgrammatorDev\SportMonksFootball\Resource\Util\IncludeTrait;
use ProgrammatorDev\SportMonksFootball\Resource\Util\LanguageTrait;
use ProgrammatorDev\SportMonksFootball\Resource\Util\SelectTrait;
use ProgrammatorDev\SportMonksFootball\Resource\Util\TimezoneTrait;
use ProgrammatorDev\SportMonksFootball\Resource\Util\ValidationTrait;
use ProgrammatorDev\SportMonksFootball\SportMonksFootball;

class Resource
{
use CacheTrait;
use SelectTrait;
use IncludeTrait;
use FilterTrait;
use LanguageTrait;
use TimezoneTrait;
use CacheTrait;
use ValidationTrait;

public function __construct(protected SportMonksFootball $api) {}
Expand Down
16 changes: 16 additions & 0 deletions src/Resource/Util/FilterTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace ProgrammatorDev\SportMonksFootball\Resource\Util;

use function DeepCopy\deep_copy;

trait FilterTrait
{
public function withFilter(string $filter): static
{
$clone = deep_copy($this, true);
$clone->api->addQueryDefault('filters', $filter);

return $clone;
}
}
16 changes: 16 additions & 0 deletions src/Resource/Util/IncludeTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace ProgrammatorDev\SportMonksFootball\Resource\Util;

use function DeepCopy\deep_copy;

trait IncludeTrait
{
public function withInclude(string $include): static
{
$clone = deep_copy($this, true);
$clone->api->addQueryDefault('include', $include);

return $clone;
}
}
16 changes: 16 additions & 0 deletions src/Resource/Util/SelectTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace ProgrammatorDev\SportMonksFootball\Resource\Util;

use function DeepCopy\deep_copy;

trait SelectTrait
{
public function withSelect(string $select): static
{
$clone = deep_copy($this, true);
$clone->api->addQueryDefault('select', $select);

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

namespace ProgrammatorDev\SportMonksFootball\Test\Integration;

use ProgrammatorDev\Api\Builder\CacheBuilder;
use ProgrammatorDev\SportMonksFootball\Resource\Resource;
use ProgrammatorDev\SportMonksFootball\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 {
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
}
}
29 changes: 29 additions & 0 deletions tests/Integration/FilterTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace ProgrammatorDev\SportMonksFootball\Test\Integration;

use ProgrammatorDev\SportMonksFootball\Resource\Resource;
use ProgrammatorDev\SportMonksFootball\Test\AbstractTest;

class FilterTraitTest extends AbstractTest
{
private Resource $resource;

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

$this->resource = new class($this->api) extends Resource {
public function getFilter(): ?string
{
return $this->api->getQueryDefault('filters');
}
};
}

public function testMethods(): void
{
$this->assertSame('eventTypes:18,17', $this->resource->withFilter('eventTypes:18,17')->getFilter());
$this->assertSame(null, $this->resource->getFilter()); // back to default value
}
}
29 changes: 29 additions & 0 deletions tests/Integration/IncludeTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace ProgrammatorDev\SportMonksFootball\Test\Integration;

use ProgrammatorDev\SportMonksFootball\Resource\Resource;
use ProgrammatorDev\SportMonksFootball\Test\AbstractTest;

class IncludeTraitTest extends AbstractTest
{
private Resource $resource;

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

$this->resource = new class($this->api) extends Resource {
public function getInclude(): ?string
{
return $this->api->getQueryDefault('include');
}
};
}

public function testMethods(): void
{
$this->assertSame('fixtures;teams', $this->resource->withInclude('fixtures;teams')->getInclude());
$this->assertSame(null, $this->resource->getInclude()); // back to default value
}
}
29 changes: 29 additions & 0 deletions tests/Integration/LanguageTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace ProgrammatorDev\SportMonksFootball\Test\Integration;

use ProgrammatorDev\SportMonksFootball\Resource\Resource;
use ProgrammatorDev\SportMonksFootball\Test\AbstractTest;

class LanguageTraitTest extends AbstractTest
{
private Resource $resource;

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

$this->resource = new class($this->api) extends Resource {
public function getLanguage(): string
{
return $this->api->getQueryDefault('locale');
}
};
}

public function testMethods(): void
{
$this->assertSame('pt', $this->resource->withLanguage('pt')->getLanguage());
$this->assertSame('en', $this->resource->getLanguage()); // back to default value
}
}
61 changes: 61 additions & 0 deletions tests/Integration/PaginationTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace ProgrammatorDev\SportMonksFootball\Test\Integration;

use ProgrammatorDev\SportMonksFootball\Resource\Resource;
use ProgrammatorDev\SportMonksFootball\Resource\Util\PaginationTrait;
use ProgrammatorDev\SportMonksFootball\Test\AbstractTest;

class PaginationTraitTest extends AbstractTest
{
private Resource $resource;

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

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

public function getPage(): ?int
{
return $this->api->getQueryDefault('page');
}

public function getPerPage(): ?int
{
return $this->api->getQueryDefault('per_page');
}

public function getSortBy(): ?string
{
return $this->api->getQueryDefault('sortBy');
}

public function getOrder(): ?string
{
return $this->api->getQueryDefault('order');
}
};
}

public function testMethods(): void
{
$this->assertSame(1, $this->resource->withPage(1)->getPage());
$this->assertSame(null, $this->resource->getPage());

$this->assertSame(25, $this->resource->withPerPage(25)->getPerPage());
$this->assertSame(null, $this->resource->getPerPage());

$this->assertSame('name', $this->resource->withSortBy('name')->getSortBy());
$this->assertSame(null, $this->resource->getSortBy());

$this->assertSame('asc', $this->resource->withOrder('asc')->getOrder());
$this->assertSame(null, $this->resource->getOrder());

$this->assertSame(1, $this->resource->withPagination(page: 1)->getPage());
$this->assertSame(25, $this->resource->withPagination(page: 1, perPage: 25)->getPerPage());
$this->assertSame('name', $this->resource->withPagination(page: 1, sortBy: 'name')->getSortBy());
$this->assertSame('asc', $this->resource->withPagination(page: 1, order: 'asc')->getOrder());
}
}
Loading

0 comments on commit ba224f0

Please sign in to comment.