Add fake backend

This commit is contained in:
philipp lang 2021-06-13 11:24:23 +02:00
parent 489b80ec12
commit 5da024f7ef
2 changed files with 50 additions and 2 deletions

View File

@ -2,8 +2,55 @@
namespace Zoomyboy\LaravelNami\Backend; namespace Zoomyboy\LaravelNami\Backend;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use GuzzleHttp\Psr7\Response as GuzzleResponse;
class FakeBackend { class FakeBackend {
private $members; 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
])));
}
}
} }

View File

@ -3,16 +3,17 @@
namespace Zoomyboy\LaravelNami; namespace Zoomyboy\LaravelNami;
use PHPUnit\Framework\Assert; use PHPUnit\Framework\Assert;
use Zoomyboy\LaravelNami\Backend\Backend;
trait FakesNami { trait FakesNami {
public function fakeNami() { public function fakeNami() {
Nami::fake(); Backend::fake();
} }
public function fakeNamiMembers($members) { public function fakeNamiMembers($members) {
foreach ($members as $member) { foreach ($members as $member) {
Nami::addMember($member); Backend::addMember($member);
} }
} }