Skip to content

Commit

Permalink
refactor: Add response factories.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Apr 20, 2022
1 parent b741a2e commit 5b90e2f
Show file tree
Hide file tree
Showing 12 changed files with 308 additions and 61 deletions.
128 changes: 111 additions & 17 deletions spec/EcPhp/CasLib/CasSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
use EcPhp\CasLib\Exception\CasExceptionInterface;
use EcPhp\CasLib\Exception\CasHandlerException;
use EcPhp\CasLib\Response\CasResponseBuilder;
use EcPhp\CasLib\Response\Factory\AuthenticationFailureFactory;
use EcPhp\CasLib\Response\Factory\ProxyFactory;
use EcPhp\CasLib\Response\Factory\ProxyFailureFactory;
use EcPhp\CasLib\Response\Factory\ServiceValidateFactory;
use EcPhp\CasLib\Utils\Uri as UtilsUri;
use Ergebnis\Http\Method;
use Exception;
Expand Down Expand Up @@ -59,7 +63,12 @@ public function it_can_authenticate_a_request(CacheItemPoolInterface $cache)
new Psr18Client(CasSpecUtils::getHttpClientMock()),
$psr17,
$cache,
new CasResponseBuilder()
new CasResponseBuilder(
new AuthenticationFailureFactory(),
new ProxyFactory(),
new ProxyFailureFactory(),
new ServiceValidateFactory()
)
);

$request = new ServerRequest(
Expand Down Expand Up @@ -165,7 +174,12 @@ public function it_can_authenticate_a_request_in_proxy_mode(CacheItemPoolInterfa
new Psr18Client(CasSpecUtils::getHttpClientMock()),
$psr17,
$cache,
new CasResponseBuilder()
new CasResponseBuilder(
new AuthenticationFailureFactory(),
new ProxyFactory(),
new ProxyFailureFactory(),
new ServiceValidateFactory()
)
);

$request = new ServerRequest(
Expand Down Expand Up @@ -339,11 +353,21 @@ public function it_can_detect_wrong_url(CacheItemPoolInterface $cache)
],
]);

$client = new Psr18Client(CasSpecUtils::getHttpClientMock());
$psr17Factory = new Psr17Factory();
$psr17 = new Psr17($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory);

$this->beConstructedWith($properties, $client, $psr17, $cache, new CasResponseBuilder());
$this->beConstructedWith(
$properties,
new Psr18Client(CasSpecUtils::getHttpClientMock()),
$psr17,
$cache,
new CasResponseBuilder(
new AuthenticationFailureFactory(),
new ProxyFactory(),
new ProxyFailureFactory(),
new ServiceValidateFactory()
)
);

$parameters = [
'service' => 'service',
Expand Down Expand Up @@ -593,10 +617,21 @@ public function it_can_logout()

public function it_can_parse_a_bad_proxy_request_response(CacheItemPoolInterface $cache)
{
$client = new Psr18Client(CasSpecUtils::getHttpClientMock());
$psr17Factory = new Psr17Factory();
$psr17 = new Psr17($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory);
$this->beConstructedWith(CasSpecUtils::getTestProperties(), $client, $psr17, $cache, new CasResponseBuilder());

$this->beConstructedWith(
CasSpecUtils::getTestProperties(),
new Psr18Client(CasSpecUtils::getHttpClientMock()),
$psr17,
$cache,
new CasResponseBuilder(
new AuthenticationFailureFactory(),
new ProxyFactory(),
new ProxyFailureFactory(),
new ServiceValidateFactory()
)
);

$request = new ServerRequest(
Method::GET,
Expand All @@ -610,10 +645,21 @@ public function it_can_parse_a_bad_proxy_request_response(CacheItemPoolInterface

public function it_can_parse_a_good_proxy_request_response(CacheItemPoolInterface $cache)
{
$client = new Psr18Client(CasSpecUtils::getHttpClientMock());
$psr17Factory = new Psr17Factory();
$psr17 = new Psr17($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory);
$this->beConstructedWith(CasSpecUtils::getTestProperties(), $client, $psr17, $cache, new CasResponseBuilder());

$this->beConstructedWith(
CasSpecUtils::getTestProperties(),
new Psr18Client(CasSpecUtils::getHttpClientMock()),
$psr17,
$cache,
new CasResponseBuilder(
new AuthenticationFailureFactory(),
new ProxyFactory(),
new ProxyFailureFactory(),
new ServiceValidateFactory()
)
);

$request = new ServerRequest(
Method::GET,
Expand All @@ -639,10 +685,21 @@ public function it_can_parse_json_in_a_response(CacheItemPoolInterface $cache)
],
]);

$client = new Psr18Client(CasSpecUtils::getHttpClientMock());
$psr17Factory = new Psr17Factory();
$psr17 = new Psr17($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory);
$this->beConstructedWith($properties, $client, $psr17, $cache, new CasResponseBuilder());

$this->beConstructedWith(
$properties,
new Psr18Client(CasSpecUtils::getHttpClientMock()),
$psr17,
$cache,
new CasResponseBuilder(
new AuthenticationFailureFactory(),
new ProxyFactory(),
new ProxyFailureFactory(),
new ServiceValidateFactory()
)
);

$request = new ServerRequest(
Method::GET,
Expand Down Expand Up @@ -676,10 +733,21 @@ public function it_can_renew_login()

public function it_can_request_a_proxy_ticket(CacheItemPoolInterface $cache)
{
$client = new Psr18Client(CasSpecUtils::getHttpClientMock());
$psr17Factory = new Psr17Factory();
$psr17 = new Psr17($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory);
$this->beConstructedWith(CasSpecUtils::getTestProperties(), $client, $psr17, $cache, new CasResponseBuilder());

$this->beConstructedWith(
CasSpecUtils::getTestProperties(),
new Psr18Client(CasSpecUtils::getHttpClientMock()),
$psr17,
$cache,
new CasResponseBuilder(
new AuthenticationFailureFactory(),
new ProxyFactory(),
new ProxyFailureFactory(),
new ServiceValidateFactory()
)
);

$request = new ServerRequest(
Method::GET,
Expand Down Expand Up @@ -714,10 +782,21 @@ public function it_can_validate_a_bad_proxy_ticket(CacheItemPoolInterface $cache
],
]);

$client = new Psr18Client(CasSpecUtils::getHttpClientMock());
$psr17Factory = new Psr17Factory();
$psr17 = new Psr17($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory);
$this->beConstructedWith($properties, $client, $psr17, $cache, new CasResponseBuilder());

$this->beConstructedWith(
$properties,
new Psr18Client(CasSpecUtils::getHttpClientMock()),
$psr17,
$cache,
new CasResponseBuilder(
new AuthenticationFailureFactory(),
new ProxyFactory(),
new ProxyFailureFactory(),
new ServiceValidateFactory()
)
);

$request = new ServerRequest(
Method::POST,
Expand All @@ -739,7 +818,12 @@ public function it_can_validate_a_bad_service_validate_request(CacheItemPoolInte
new Psr18Client(CasSpecUtils::getHttpClientMock()),
$psr17,
$cache,
new CasResponseBuilder()
new CasResponseBuilder(
new AuthenticationFailureFactory(),
new ProxyFactory(),
new ProxyFailureFactory(),
new ServiceValidateFactory()
)
);

$request = new ServerRequest(
Expand Down Expand Up @@ -834,7 +918,12 @@ public function it_cannot_be_constructed_without_base_url(CacheItemPoolInterface
new Psr18Client(CasSpecUtils::getHttpClientMock()),
$psr17,
$cache,
new CasResponseBuilder()
new CasResponseBuilder(
new AuthenticationFailureFactory(),
new ProxyFactory(),
new ProxyFailureFactory(),
new ServiceValidateFactory()
)
);

$request = new ServerRequest(
Expand Down Expand Up @@ -917,7 +1006,12 @@ public function let(CacheItemPoolInterface $cache, CacheItemInterface $cacheItem
new Psr18Client(CasSpecUtils::getHttpClientMock()),
$psr17,
$cache,
new CasResponseBuilder()
new CasResponseBuilder(
new AuthenticationFailureFactory(),
new ProxyFactory(),
new ProxyFailureFactory(),
new ServiceValidateFactory()
)
);
}
}
11 changes: 10 additions & 1 deletion spec/EcPhp/CasLib/Handler/ProxySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

use EcPhp\CasLib\Handler\Proxy;
use EcPhp\CasLib\Response\CasResponseBuilder;
use EcPhp\CasLib\Response\Factory\AuthenticationFailureFactory;
use EcPhp\CasLib\Response\Factory\ProxyFactory;
use EcPhp\CasLib\Response\Factory\ProxyFailureFactory;
use EcPhp\CasLib\Response\Factory\ServiceValidateFactory;
use Ergebnis\Http\Method;
use Exception;
use loophp\psr17\Psr17Interface;
Expand Down Expand Up @@ -56,7 +60,12 @@ public function let(CacheItemPoolInterface $cache, Psr17Interface $psr17)
$this->beConstructedWith(
[],
$cache,
new CasResponseBuilder(),
new CasResponseBuilder(
new AuthenticationFailureFactory(),
new ProxyFactory(),
new ProxyFailureFactory(),
new ServiceValidateFactory()
),
new Psr18Client(CasSpecUtils::getHttpClientMock()),
Cas::getTestProperties(),
$psr17
Expand Down
18 changes: 16 additions & 2 deletions spec/EcPhp/CasLib/Handler/ServiceValidateSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

use EcPhp\CasLib\Handler\ServiceValidate;
use EcPhp\CasLib\Response\CasResponseBuilder;
use EcPhp\CasLib\Response\Factory\AuthenticationFailureFactory;
use EcPhp\CasLib\Response\Factory\ProxyFactory;
use EcPhp\CasLib\Response\Factory\ProxyFailureFactory;
use EcPhp\CasLib\Response\Factory\ServiceValidateFactory;
use Ergebnis\Http\Method;
use Exception;
use loophp\psr17\Psr17;
Expand Down Expand Up @@ -82,7 +86,12 @@ public function it_can_get_credentials_with_pgtUrl(CacheItemPoolInterface $cache
$this->beConstructedWith(
[],
$cache,
new CasResponseBuilder(),
new CasResponseBuilder(
new AuthenticationFailureFactory(),
new ProxyFactory(),
new ProxyFailureFactory(),
new ServiceValidateFactory()
),
new Psr18Client(CasSpecUtils::getHttpClientMock()),
Cas::getTestPropertiesWithPgtUrl(),
$psr17
Expand Down Expand Up @@ -132,7 +141,12 @@ public function let(CacheItemPoolInterface $cache)
$this->beConstructedWith(
[],
$cache,
new CasResponseBuilder(),
new CasResponseBuilder(
new AuthenticationFailureFactory(),
new ProxyFactory(),
new ProxyFailureFactory(),
new ServiceValidateFactory()
),
new Psr18Client(CasSpecUtils::getHttpClientMock()),
Cas::getTestProperties(),
$psr17
Expand Down
15 changes: 15 additions & 0 deletions spec/EcPhp/CasLib/Response/CasResponseBuilderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
use EcPhp\CasLib\Contract\Response\Type\Proxy;
use EcPhp\CasLib\Contract\Response\Type\ProxyFailure;
use EcPhp\CasLib\Contract\Response\Type\ServiceValidate;
use EcPhp\CasLib\Response\Factory\AuthenticationFailureFactory;
use EcPhp\CasLib\Response\Factory\ProxyFactory;
use EcPhp\CasLib\Response\Factory\ProxyFailureFactory;
use EcPhp\CasLib\Response\Factory\ServiceValidateFactory;
use Exception;
use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7\Response;
Expand Down Expand Up @@ -298,4 +302,15 @@ public function it_is_initializable()
{
$this->shouldHaveType(CasResponseBuilderInterface::class);
}

public function let()
{
$this
->beConstructedWith(
new AuthenticationFailureFactory(),
new ProxyFactory(),
new ProxyFailureFactory(),
new ServiceValidateFactory()
);
}
}
18 changes: 16 additions & 2 deletions spec/tests/EcPhp/CasLib/CasSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
use EcPhp\CasLib\Cas;
use EcPhp\CasLib\Contract\Response\Type\ServiceValidate;
use EcPhp\CasLib\Response\CasResponseBuilder;
use EcPhp\CasLib\Response\Factory\AuthenticationFailureFactory;
use EcPhp\CasLib\Response\Factory\ProxyFactory;
use EcPhp\CasLib\Response\Factory\ProxyFailureFactory;
use EcPhp\CasLib\Response\Factory\ServiceValidateFactory;
use Ergebnis\Http\Method;
use loophp\psr17\Psr17;
use Nyholm\Psr7\Factory\Psr17Factory;
Expand Down Expand Up @@ -43,7 +47,12 @@ public function it_can_test_the_proxy_mode_with_pgtUrl()
new Psr18Client(CasSpecUtils::getHttpClientMock()),
$psr17,
$cache,
new CasResponseBuilder()
new CasResponseBuilder(
new AuthenticationFailureFactory(),
new ProxyFactory(),
new ProxyFailureFactory(),
new ServiceValidateFactory()
)
));

$request = new ServerRequest(
Expand All @@ -66,7 +75,12 @@ public function it_can_test_the_proxy_mode_without_pgtUrl()
new Psr18Client(CasSpecUtils::getHttpClientMock()),
$psr17,
new ArrayAdapter(),
new CasResponseBuilder()
new CasResponseBuilder(
new AuthenticationFailureFactory(),
new ProxyFactory(),
new ProxyFailureFactory(),
new ServiceValidateFactory()
)
));

$request = new ServerRequest(
Expand Down
12 changes: 0 additions & 12 deletions src/Contract/Response/CasResponseBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,11 @@

namespace EcPhp\CasLib\Contract\Response;

use EcPhp\CasLib\Contract\Response\Type\AuthenticationFailure;
use EcPhp\CasLib\Contract\Response\Type\Proxy;
use EcPhp\CasLib\Contract\Response\Type\ProxyFailure;
use EcPhp\CasLib\Contract\Response\Type\ServiceValidate;
use EcPhp\CasLib\Exception\CasResponseBuilderException;
use Psr\Http\Message\ResponseInterface;

interface CasResponseBuilderInterface
{
public function createAuthenticationFailure(ResponseInterface $response): AuthenticationFailure;

public function createProxyFailure(ResponseInterface $response): ProxyFailure;

public function createProxySuccess(ResponseInterface $response): Proxy;

public function createServiceValidate(ResponseInterface $response): ServiceValidate;

/**
* @throws CasResponseBuilderException
*/
Expand Down
20 changes: 20 additions & 0 deletions src/Contract/Response/Factory/CasResponseFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/**
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/ecphp
*/

declare(strict_types=1);

namespace EcPhp\CasLib\Contract\Response\Factory;

use EcPhp\CasLib\Contract\Response\CasResponseInterface;
use Psr\Http\Message\ResponseInterface;

interface CasResponseFactoryInterface
{
public function decorate(ResponseInterface $response): CasResponseInterface;
}

0 comments on commit 5b90e2f

Please sign in to comment.