laravel-nami-api/tests/Unit/GroupsTest.php

91 lines
2.5 KiB
PHP
Raw Normal View History

2022-02-19 17:08:43 +01:00
<?php
namespace Zoomyboy\LaravelNami\Tests\Unit;
use Zoomyboy\LaravelNami\Authentication\Auth;
use Zoomyboy\LaravelNami\Exceptions\NotAuthenticatedException;
use Zoomyboy\LaravelNami\Fakes\GroupFake;
2022-03-11 20:13:19 +01:00
use Zoomyboy\LaravelNami\Group;
2022-02-19 17:08:43 +01:00
use Zoomyboy\LaravelNami\Nami;
2022-02-20 01:41:21 +01:00
use Zoomyboy\LaravelNami\NamiException;
2022-02-19 17:08:43 +01:00
use Zoomyboy\LaravelNami\Tests\TestCase;
class GroupsTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
2022-03-11 20:13:19 +01:00
2022-02-19 17:08:43 +01:00
Auth::fake();
}
2022-03-11 20:13:19 +01:00
public function testGetGroups(): void
2022-02-19 17:08:43 +01:00
{
Auth::success(12345, 'secret');
2022-02-20 01:41:21 +01:00
app(GroupFake::class)->fetches(null, [
2022-03-11 20:13:19 +01:00
1234 => ['name' => 'testgroup'],
2022-02-19 17:08:43 +01:00
]);
$group = Nami::login(12345, 'secret')->group(1234);
2022-03-11 20:13:19 +01:00
$this->assertInstanceOf(Group::class, $group);
2022-02-19 17:08:43 +01:00
$this->assertEquals('testgroup', $group->name);
$this->assertEquals(1234, $group->id);
app(GroupFake::class)->assertRootFetched();
}
2022-03-11 20:13:19 +01:00
public function testGetSubgroups(): void
2022-02-19 17:08:43 +01:00
{
Auth::success(12345, 'secret');
2022-02-20 01:41:21 +01:00
app(GroupFake::class)->fetches(null, [
2022-03-11 20:13:19 +01:00
1234 => ['name' => 'testgroup'],
2022-02-20 01:41:21 +01:00
])->fetches(1234, [
2022-03-11 20:13:19 +01:00
555 => ['name' => 'ABC'],
2022-02-19 17:08:43 +01:00
]);
2022-02-20 01:41:21 +01:00
$group = Nami::login(12345, 'secret')->groups(1234)->first();
2022-02-19 17:08:43 +01:00
$this->assertEquals('ABC', $group->name);
$this->assertEquals(555, $group->id);
app(GroupFake::class)->assertFetched(1234);
}
2022-03-11 20:13:19 +01:00
public function testNeedsAuthentication(): void
2022-02-20 01:41:21 +01:00
{
$this->expectException(NotAuthenticatedException::class);
$group = Nami::group(1234);
}
2022-03-11 20:13:19 +01:00
public function testThrowsExceptionWhenGroupFetchFailed(): void
2022-02-20 01:41:21 +01:00
{
$this->expectException(NamiException::class);
Auth::success(12345, 'secret');
app(GroupFake::class)->failsToFetch(null);
Nami::login(12345, 'secret')->group(1234);
}
2022-03-11 20:13:19 +01:00
public function testThrowsExceptionWhenSubgroupFetchFailed(): void
2022-02-20 01:41:21 +01:00
{
$this->expectException(NamiException::class);
Auth::success(12345, 'secret');
app(GroupFake::class)->fetches(null, [
2022-03-11 20:13:19 +01:00
1234 => ['name' => 'testgroup'],
2022-02-20 01:41:21 +01:00
]);
app(GroupFake::class)->failsToFetch(1234);
Nami::login(12345, 'secret')->groups(1234);
}
2022-03-11 20:13:19 +01:00
public function testItDoesntReturnGroupWhenNoJsonIsReturned(): void
2022-02-20 01:41:21 +01:00
{
Auth::success(12345, 'secret');
app(GroupFake::class)->failsToFetchWithoutJson(null);
$group = Nami::login(12345, 'secret')->group(1234);
$this->assertNull($group);
}
2022-02-19 17:08:43 +01:00
}