Skip to content

Commit

Permalink
Adjust tests to Persistence injection refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Jun 17, 2022
1 parent c50bf20 commit 038a384
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 21 deletions.
16 changes: 8 additions & 8 deletions src/test/php/de/thekid/cas/unittest/LoginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
class LoginTest extends HandlerTest {
public const SERVICE = 'https://example.org/';

private $templates, $signed, $flow;
private $persistence, $templates, $signed, $flow;

#[Before]
public function initialize() {
$this->tickets= new TestingTickets();
$this->persistence= new TestingPersistence(users: new TestingUsers(['root' => 'secret']));
$this->signed= new Signed('secret');
$this->flow= new Flow([
new UseService(new class() implements Services {
public fn validate($url) => LoginTest::SERVICE === $url;
}),
new EnterCredentials(new TestingUsers(['root' => 'secret'])),
new RedirectToService($this->tickets, $this->signed),
new EnterCredentials($this->persistence),
new RedirectToService($this->persistence, $this->signed),
new DisplaySuccess(),
]);
}
Expand Down Expand Up @@ -201,10 +201,10 @@ public function issues_ticket_and_redirect_to_service() {
'attributes' => null,
],
],
$this->tickets->validate(0),
$this->persistence->tickets()->validate(0),
);
Assert::equals(
self::SERVICE.'?ticket='.$this->signed->id(0, $this->tickets->prefix()),
self::SERVICE.'?ticket='.$this->signed->id(0, $this->persistence->tickets()->prefix()),
$res->headers()['Location']
);
}
Expand All @@ -229,10 +229,10 @@ public function issues_ticket_and_redirect_to_service_directly_when_user_in_sess
'attributes' => null,
],
],
$this->tickets->validate(1),
$this->persistence->tickets()->validate(1),
);
Assert::equals(
self::SERVICE.'?ticket='.$this->signed->id(1, $this->tickets->prefix()),
self::SERVICE.'?ticket='.$this->signed->id(1, $this->persistence->tickets()->prefix()),
$res->headers()['Location']
);
}
Expand Down
19 changes: 19 additions & 0 deletions src/test/php/de/thekid/cas/unittest/TestingPersistence.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php namespace de\thekid\cas\unittest;

use de\thekid\cas\Persistence;
use de\thekid\cas\tickets\Tickets;
use de\thekid\cas\users\Users;

class TestingPersistence implements Persistence {

public function __construct(
private Users $users= new TestingUsers(),
private Tickets $tickets= new TestingTickets(),
) { }

/* Users accessor */
public fn users(): Users => $this->users;

/* Tickets accessor */
public fn tickets(): Tickets => $this->tickets;
}
30 changes: 17 additions & 13 deletions src/test/php/de/thekid/cas/unittest/ValidateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
use web\{Request, Response};

class ValidateTest {
private $tickets, $signed;
private $persistence, $signed;

#[Before]
public function initialize() {
$this->tickets= new TestingTickets();
$this->persistence= new TestingPersistence();
$this->signed= new Signed('testing-secret');
}

Expand All @@ -33,7 +33,7 @@ private function assertResponse($expected, $actual) {
* @return string
*/
private function handle($uri) {
$fixture= new Validate($this->tickets, $this->signed);
$fixture= new Validate($this->persistence, $this->signed);

$req= new Request(new TestInput('GET', $uri));
$res= new Response(TestOutput::buffered());
Expand All @@ -44,14 +44,15 @@ private function handle($uri) {

#[Test]
public function can_create() {
new Validate($this->tickets, $this->signed);
new Validate($this->persistence, $this->signed);
}

#[Test]
public function validation_success() {
$tickets= $this->persistence->tickets();
$ticket= $this->signed->id(
$this->tickets->create(['user' => ['username' => 'test'], 'service' => 'http://example.org']),
$this->tickets->prefix(),
$tickets->create(['user' => ['username' => 'test'], 'service' => 'http://example.org']),
$tickets->prefix(),
);
$this->assertResponse(
'<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">
Expand Down Expand Up @@ -113,7 +114,7 @@ public function invalid_ticket_parameter() {

#[Test]
public function missing_ticket() {
$ticket= $this->signed->id(1, $this->tickets->prefix());
$ticket= $this->signed->id(1, $this->persistence->tickets()->prefix());
$response= sprintf('
<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">
<cas:authenticationFailure code="INVALID_TICKET">
Expand All @@ -127,9 +128,10 @@ public function missing_ticket() {

#[Test]
public function invalid_service() {
$tickets= $this->persistence->tickets();
$ticket= $this->signed->id(
$this->tickets->create(['user' => ['username' => 'test'], 'service' => 'http://example.org']),
$this->tickets->prefix(),
$tickets->create(['user' => ['username' => 'test'], 'service' => 'http://example.org']),
$tickets->prefix(),
);
$this->assertResponse(
'<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">
Expand All @@ -143,9 +145,10 @@ public function invalid_service() {

#[Test]
public function success_using_explicit_xml_format() {
$tickets= $this->persistence->tickets();
$ticket= $this->signed->id(
$this->tickets->create(['user' => ['username' => 'test'], 'service' => 'http://example.org']),
$this->tickets->prefix(),
$tickets->create(['user' => ['username' => 'test'], 'service' => 'http://example.org']),
$tickets->prefix(),
);
$this->assertResponse(
'<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">
Expand All @@ -159,9 +162,10 @@ public function success_using_explicit_xml_format() {

#[Test]
public function success_using_json_format() {
$tickets= $this->persistence->tickets();
$ticket= $this->signed->id(
$this->tickets->create(['user' => ['username' => 'test'], 'service' => 'http://example.org']),
$this->tickets->prefix(),
$tickets->create(['user' => ['username' => 'test'], 'service' => 'http://example.org']),
$tickets->prefix(),
);
$this->assertResponse(
'{
Expand Down

0 comments on commit 038a384

Please sign in to comment.