Add fake backend

This commit is contained in:
philipp lang 2021-05-13 23:25:00 +02:00
parent cb97b8f1a9
commit 60465625b7
8 changed files with 75 additions and 17 deletions

View File

@ -8,9 +8,9 @@ use Illuminate\Support\Str;
use App\Nami\Exceptions\TooManyLoginAttemptsException;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Collection;
use Zoomyboy\LaravelNami\Concerns\NamiData;
use Zoomyboy\LaravelNami\Backend\Backend;
class Api implements NamiData {
class Api {
public $cookie;
public $loggedIn = null;
@ -20,8 +20,8 @@ class Api implements NamiData {
$this->cookie = new \GuzzleHttp\Cookie\CookieJar();
}
private function http() {
return Http::withOptions(['cookies' => $this->cookie]);
public function http() {
return Backend::cookie($this->cookie);
}
public function setUser(NamiUser $user) {

15
src/Backend/Backend.php Normal file
View File

@ -0,0 +1,15 @@
<?php
namespace Zoomyboy\LaravelNami\Backend;
use Illuminate\Support\Facades\Facade;
class Backend extends Facade {
protected static function getFacadeAccessor() { return 'nami.backend'; }
public static function fake() {
static::swap($api = new FakeBackend());
return $api;
}
}

View File

@ -0,0 +1,9 @@
<?php
namespace Zoomyboy\LaravelNami\Backend;
class FakeBackend {
private $members;
}

View File

@ -0,0 +1,13 @@
<?php
namespace Zoomyboy\LaravelNami\Backend;
use Illuminate\Support\Facades\Http;
class LiveBackend {
public static function cookie($cookie) {
return Http::withOptions(['cookies' => $cookie]);
}
}

View File

@ -1,11 +0,0 @@
<?php
namespace Zoomyboy\LaravelNami\Concerns;
use Illuminate\Support\Collection;
interface NamiData {
public function nationalities(): Collection;
}

27
src/FakesNami.php Normal file
View File

@ -0,0 +1,27 @@
<?php
namespace Zoomyboy\LaravelNami;
use PHPUnit\Framework\Assert;
trait FakesNami {
public function fakeNami() {
Nami::fake();
}
public function fakeNamiMembers($members) {
foreach ($members as $member) {
Nami::addMember($member);
}
}
public function assertMemberExists($groupId, $data) {
Assert::assertNotNull($existing = Nami::member($groupId, $data['id']));
foreach ($data as $key => $value) {
Assert::assertEquals($value, $existing[$key]);
}
}
}

View File

@ -1,10 +1,12 @@
<?php
namespace Zoomyboy\LaravelNami;
namespace Zoomyboy\LaravelNami\Providers;
use Illuminate\Support\Facades\Auth;
use GuzzleHttp\Client as GuzzleClient;
use Illuminate\Support\ServiceProvider;
use Zoomyboy\LaravelNami\Backend\LiveBackend;
use Zoomyboy\LaravelNami\Api;
class NamiServiceProvider extends ServiceProvider
{
@ -19,5 +21,8 @@ class NamiServiceProvider extends ServiceProvider
$this->app->singleton('nami.api', function() {
return new Api();
});
$this->app->bind('nami.backend', function() {
return new LiveBackend();
});
}
}

View File

@ -3,9 +3,9 @@
namespace Zoomyboy\LaravelNami\Tests;
use Illuminate\Support\Facades\Config;
use Zoomyboy\LaravelNami\NamiServiceProvider;
use Illuminate\Support\Facades\Http;
use Zoomyboy\LaravelNami\Tests\Stub\Member;
use Zoomyboy\LaravelNami\Providers\NamiServiceProvider;
class TestCase extends \Orchestra\Testbench\TestCase
{