Skip to content

Commit

Permalink
Update test
Browse files Browse the repository at this point in the history
  • Loading branch information
chrieinv committed Nov 22, 2023
1 parent 6f8e9aa commit 691ead3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 21 deletions.
30 changes: 15 additions & 15 deletions sourcecode/apis/contentauthor/app/Http/Libraries/LtiTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ trait LtiTrait
{
public function ltiShow($id)
{
if (!method_exists($this, 'doShow')) {
abort(500, 'Requested action is not available');
}

$ltiRequest = $this->lti->getRequest(request());

if (!$ltiRequest) {
Expand All @@ -18,15 +22,15 @@ public function ltiShow($id)
);
}

if (method_exists($this, 'doShow')) {
return $this->doShow($id, $ltiRequest->generateContextKey(), $ltiRequest->isPreview());
}

abort(500, 'Requested action is not available');
return $this->doShow($id, $ltiRequest->generateContextKey(), $ltiRequest->isPreview());
}

public function ltiCreate(Request $request)
{
if (!method_exists($this, 'create')) {
abort(500, 'Requested action is not available');
}

$ltiRequest = $this->lti->getRequest($request);

if (!$ltiRequest) {
Expand All @@ -36,15 +40,15 @@ public function ltiCreate(Request $request)
);
}

if (method_exists($this, 'create')) {
return $this->create($request);
}

abort(500, 'Requested action is not available');
return $this->create($request);
}

public function ltiEdit(Request $request, $id)
{
if (!method_exists($this, 'edit')) {
abort(500, 'Requested action is not available');
}

$ltiRequest = $this->lti->getRequest($request);

if (!$ltiRequest) {
Expand All @@ -54,10 +58,6 @@ public function ltiEdit(Request $request, $id)
);
}

if (method_exists($this, 'edit')) {
return $this->edit($request, $id);
}

abort(500, 'Requested action is not available');
return $this->edit($request, $id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

namespace Tests\Integration\Http\Libraries;

use App\Http\Libraries\LtiTrait;
use App\Lti\LtiRequest;
use Cerpus\EdlibResourceKit\Oauth1\ValidatorInterface;
use Exception;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Tests\Integration\Http\Libraries\Stubs\LtiTraitStubClass;
use Tests\TestCase;

Expand All @@ -23,15 +25,25 @@ public function setupLti(): void
$this->instance(ValidatorInterface::class, $validator);
}

public function test_ltiShow_exception(): void
public function test_ltiShow_unauthorized_exception(): void
{
$this->expectException(Exception::class);
$this->expectException(UnauthorizedHttpException::class);
$this->expectExceptionMessage('No valid LTI request');

$class = app(LtiTraitStubClass::class);
$class->ltiShow(1);
}

public function test_ltiShow_unavailable_exception(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Requested action is not available');

$class = $this->getMockForTrait(LtiTrait::class);
/** @phpstan-ignore-next-line */
$class->ltiShow(1);
}

public function test_ltiShow(): void
{
$this->setupLti();
Expand All @@ -45,15 +57,25 @@ public function test_ltiShow(): void
$this->assertSame('doShow', $testClass->ltiShow(42));
}

public function test_ltiCreate_exception(): void
public function test_ltiCreate_unauthorized_exception(): void
{
$this->expectException(Exception::class);
$this->expectException(UnauthorizedHttpException::class);
$this->expectExceptionMessage('No valid LTI request');

$class = app(LtiTraitStubClass::class);
$class->ltiCreate(Request::create(''));
}

public function test_ltiCreate_unavailable_exception(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Requested action is not available');

$class = $this->getMockForTrait(LtiTrait::class);
/** @phpstan-ignore-next-line */
$class->ltiCreate(new Request());
}

public function test_ltiCreate(): void
{
$this->setupLti();
Expand All @@ -63,15 +85,25 @@ public function test_ltiCreate(): void
));
}

public function test_ltiEdit_exception(): void
public function test_ltiEdit_unauthorized_exception(): void
{
$this->expectException(Exception::class);
$this->expectException(UnauthorizedHttpException::class);
$this->expectExceptionMessage('No valid LTI request');

$class = app(LtiTraitStubClass::class);
$class->ltiEdit(Request::create(''), 1);
}

public function test_ltiEdit_unavailable_exception(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Requested action is not available');

$class = $this->getMockForTrait(LtiTrait::class);
/** @phpstan-ignore-next-line */
$class->ltiEdit(new Request(), 1);
}

public function test_ltiEdit(): void
{
$this->setupLti();
Expand Down

0 comments on commit 691ead3

Please sign in to comment.