Skip to content

Commit

Permalink
Merge pull request #6 from DeBoerTool/eps
Browse files Browse the repository at this point in the history
Invokable Endpoints
  • Loading branch information
danielsdeboer authored Mar 28, 2023
2 parents 1e676f4 + c279b60 commit 4591142
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 12 deletions.
34 changes: 33 additions & 1 deletion Source/ClientFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Dbt\ClientFake\Traits\AsData;
use Exception;
use Faker\Factory;
use Faker\Generator;
use Illuminate\Contracts\Foundation\Application;
Expand Down Expand Up @@ -140,6 +141,11 @@ public function fake(
return $this;
}

public function fakes(): array
{
return $this->fakes;
}

/**
* Generate the full URL using the ClientFakeOptions URL generator.
*/
Expand All @@ -148,12 +154,38 @@ protected function url(string|array $url): string
return $this->options->url($url);
}

/**
* @throws \Exception
*/
public function __get(string $name)
{
if ($this->endpoints->has($name)) {
return $this->endpoints->get($name, $this);
}

return $this->$name;
throw new Exception(sprintf(
'Undefined property: %s::$%s',
static::class,
$name,
));
}

/**
* @throws \Exception
*/
public function __call(string $name, array $args)
{
if ($this->endpoints->has($name)) {
return call_user_func(
$this->endpoints->get($name, $this),
...$args,
);
}

throw new Exception(sprintf(
'Undefined method: %s::%s()',
static::class,
$name,
));
}
}
19 changes: 12 additions & 7 deletions Source/ClientFakeEndpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ public function __construct(protected ClientFake $clientFake)
{
}

public function __invoke(Closure $closure): ClientFake
{
return $this->with($closure);
}

public function with(Closure $closure): ClientFake
{
$closure($this);

return $this->clientFake;
}

public function fake(
string|array $url,
Closure|array $data,
Expand All @@ -23,13 +35,6 @@ public function fake(
return $this;
}

public function with(Closure $closure): ClientFake
{
$closure($this);

return $this->clientFake;
}

public function done(): ClientFake
{
return $this->clientFake;
Expand Down
62 changes: 58 additions & 4 deletions Tests/ClientFakeEndpointsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
namespace Dbt\ClientFake\Tests;

use Dbt\ClientFake\Tests\Fakes\BreedEps;
use Dbt\ClientFake\Tests\Fakes\CatFactsFake;
use Throwable;

class ClientFakeEndpointsTest extends TestCase
{
/** @test */
public function failing_to_get_the_endpoints_object(): void
public function failing_to_get_the_endpoints(): void
{
try {
$this->fake()->somethingThatDoesntExist;
Expand All @@ -27,23 +28,54 @@ public function failing_to_get_the_endpoints_object(): void
}

/** @test */
public function getting_the_endpoints_object(): void
public function getting_the_endpoints(): void
{
$this->assertInstanceOf(
BreedEps::class,
$this->fake()->breeds,
);
}

/** @test */
public function failing_to_call_the_endpoints(): void
{
try {
$this->fake()->aMethodThatDoesNotExist();
} catch (Throwable $e) {
$this->assertStringContainsString(
'Undefined method',
$e->getMessage(),
);

return;
}

$this->fail('No exception was thrown.');
}

/** @test */
public function calling_the_endpoints(): void
{
$fake = $this->fake()->breeds(
fn (BreedEps $breeds) => $breeds->index([]),
);

$this->assertArrayHasKey(
'https://catfact.ninja/breeds',
$fake->fakes(),
);
$this->assertInstanceOf(CatFactsFake::class, $fake);
}

/** @test */
public function using_the_endpoints(): void
{
$breeds = $this->breeds();
$fact = $this->fact();

$this->fake()->breeds
->index($breeds)
->done()
->index($breeds)
->done()
->getFact($fact)
->commit();

Expand Down Expand Up @@ -79,4 +111,26 @@ public function using_a_closure(): void
$this->service()->getFact()->json('fact'),
);
}

/** @test */
public function using_with_invoke(): void
{
$breeds = $this->breeds();
$fact = $this->fact();

$this->fake()
->breeds(fn (BreedEps $eps) => $eps->index($breeds))
->getFact($fact)
->commit();

$this->assertSame(
$breeds,
$this->service()->getBreeds()->json('data'),
);

$this->assertSame(
$fact,
$this->service()->getFact()->json('fact'),
);
}
}

0 comments on commit 4591142

Please sign in to comment.