From 5da024f7efe8c1b4599b565c47d8cade1af43746 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Sun, 13 Jun 2021 11:24:23 +0200 Subject: [PATCH] Add fake backend --- src/Backend/FakeBackend.php | 47 +++++++++++++++++++++++++++++++++++++ src/FakesNami.php | 5 ++-- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/Backend/FakeBackend.php b/src/Backend/FakeBackend.php index c06f57b..94beef6 100644 --- a/src/Backend/FakeBackend.php +++ b/src/Backend/FakeBackend.php @@ -2,8 +2,55 @@ namespace Zoomyboy\LaravelNami\Backend; +use Illuminate\Http\Client\Response; +use Illuminate\Support\Facades\Http; +use GuzzleHttp\Psr7\Response as GuzzleResponse; + class FakeBackend { private $members; + public function __construct() { + $this->members = collect([]); + } + + public function addMember(array $member) { + $this->members->push($member); + } + + public function cookie($cookie) { + return $this; + } + + public function put($url, $data) { + if (preg_match('|/ica/rest/nami/mitglied/filtered-for-navigation/gruppierung/gruppierung/([0-9]+)/([0-9]+)|', $url, $matches)) { + list($url, $groupId, $memberId) = $matches; + $existing = $this->members->search(function($m) use ($groupId, $memberId) { + return $m['gruppierungId'] == $groupId && $m['id'] == $memberId; + }); + if ($existing !== false) { + $this->members[$existing] = $data; + } + + return; + } + + throw new \Exception('no handler found for URL '.$url); + } + + public function get($url) { + if (preg_match('|/ica/rest/nami/mitglied/filtered-for-navigation/gruppierung/gruppierung/([0-9]+)/([0-9]+)|', $url, $matches)) { + list($url, $groupId, $memberId) = $matches; + + $member = $this->members->first(function($m) use ($groupId, $memberId) { + return $m['gruppierungId'] == $groupId && $m['id'] == $memberId; + }); + + return new Response(new GuzzleResponse(200, [], json_encode([ + 'success' => true, + 'data' => $member + ]))); + } + } + } diff --git a/src/FakesNami.php b/src/FakesNami.php index eca07b7..97328e8 100644 --- a/src/FakesNami.php +++ b/src/FakesNami.php @@ -3,16 +3,17 @@ namespace Zoomyboy\LaravelNami; use PHPUnit\Framework\Assert; +use Zoomyboy\LaravelNami\Backend\Backend; trait FakesNami { public function fakeNami() { - Nami::fake(); + Backend::fake(); } public function fakeNamiMembers($members) { foreach ($members as $member) { - Nami::addMember($member); + Backend::addMember($member); } }