Skip to content

Commit

Permalink
Merge pull request #12 from ecphp/11-inject-introspector
Browse files Browse the repository at this point in the history
Issue #11: Inject the Introspector object.
  • Loading branch information
drupol authored Jul 23, 2020
2 parents c975348 + 0dc346f commit a9e6d58
Show file tree
Hide file tree
Showing 21 changed files with 241 additions and 174 deletions.
85 changes: 68 additions & 17 deletions spec/EcPhp/CasLib/CasSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use EcPhp\CasLib\Cas;
use EcPhp\CasLib\Configuration\Properties as CasProperties;
use EcPhp\CasLib\Introspection\Introspector;
use EcPhp\CasLib\Introspection\ServiceValidate;
use EcPhp\CasLib\Utils\SimpleXml;
use Exception;
use InvalidArgumentException;
Expand Down Expand Up @@ -113,7 +115,7 @@ public function it_can_authenticate_a_request(ServerRequestInterface $serverRequ
$request = (new ServerRequest('GET', $uri))
->withQueryParams(['ticket' => 'ST-ticket']);

$this->beConstructedWith($request, CasSpecUtils::getTestProperties(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger);
$this->beConstructedWith($request, CasSpecUtils::getTestProperties(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger, new Introspector());

$this
->authenticate()
Expand Down Expand Up @@ -221,7 +223,7 @@ public function it_can_authenticate_a_request_in_proxy_mode(ServerRequestInterfa
$request = (new ServerRequest('GET', $uri))
->withQueryParams(['ticket' => 'ST-ticket']);

$this->beConstructedWith($request, CasSpecUtils::getTestPropertiesWithPgtUrl(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger);
$this->beConstructedWith($request, CasSpecUtils::getTestPropertiesWithPgtUrl(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger, new Introspector());

$this
->authenticate()
Expand Down Expand Up @@ -321,7 +323,7 @@ public function it_can_be_constructed_without_base_url(LoggerInterface $logger,
$psr17Factory // StreamFactory
);
$serverRequest = $creator->fromGlobals();
$this->beConstructedWith($serverRequest, $properties, $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger);
$this->beConstructedWith($serverRequest, $properties, $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger, new Introspector());

$request = new ServerRequest('GET', 'http://foo');

Expand Down Expand Up @@ -352,7 +354,7 @@ public function it_can_check_if_the_logger_works_during_a_failed_authentication_
$request = (new ServerRequest('GET', $uri))
->withQueryParams(['ticket' => 'BAD-ticket']);

$this->beConstructedWith($request, CasSpecUtils::getTestProperties(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger);
$this->beConstructedWith($request, CasSpecUtils::getTestProperties(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger, new Introspector());

$this
->authenticate()
Expand Down Expand Up @@ -399,7 +401,7 @@ public function it_can_check_if_the_logger_works_during_a_failed_proxy_validate_
$request = (new ServerRequest('GET', $uri))
->withQueryParams(['ticket' => 'BAD-ticket']);

$this->beConstructedWith($request, CasSpecUtils::getTestPropertiesWithPgtUrl(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger);
$this->beConstructedWith($request, CasSpecUtils::getTestPropertiesWithPgtUrl(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger, new Introspector());

$this
->requestProxyValidate()
Expand Down Expand Up @@ -430,7 +432,7 @@ public function it_can_check_if_the_logger_works_during_a_failed_service_validat
$request = (new ServerRequest('GET', $uri))
->withQueryParams(['ticket' => 'BAD-ticket']);

$this->beConstructedWith($request, CasSpecUtils::getTestProperties(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger);
$this->beConstructedWith($request, CasSpecUtils::getTestProperties(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger, new Introspector());

$this
->requestServiceValidate()
Expand Down Expand Up @@ -461,7 +463,7 @@ public function it_can_check_if_the_logger_works_during_a_successful_authenticat
$request = (new ServerRequest('GET', $uri))
->withQueryParams(['ticket' => 'ST-ticket']);

$this->beConstructedWith($request, CasSpecUtils::getTestProperties(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger);
$this->beConstructedWith($request, CasSpecUtils::getTestProperties(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger, new Introspector());

$response = $this
->getWrappedObject()
Expand Down Expand Up @@ -491,6 +493,55 @@ public function it_can_check_if_the_request_needs_authentication()
->shouldReturn(true);
}

public function it_can_detect_the_type_of_a_response(CacheItemPoolInterface $cache, LoggerInterface $logger)
{
$body = <<< 'EOF'
<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">
<cas:authenticationSuccess>
<cas:user>username</cas:user>
</cas:authenticationSuccess>
</cas:serviceResponse>
EOF;

$headers = [
'Content-Type' => 'application/xml',
];

$response = new Response(
200,
$headers,
$body
);

$this
->detect(
$response
)
->shouldReturnAnInstanceOf(ServiceValidate::class);

$body = <<< 'EOF'
<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">
<cas:authenticationSuccess>
<cas:user>username</cas:user>
</cas:authenticationSuccess>
</cas:serviceResponse>
EOF;

$headers = [
'Content-Type' => 'application/foo',
];

$response = new Response(
200,
$headers,
$body
);

$this
->shouldThrow(InvalidArgumentException::class)
->during('detect', [$response]);
}

public function it_can_detect_when_gateway_and_renew_are_set_together()
{
$from = 'http://local/';
Expand Down Expand Up @@ -539,7 +590,7 @@ public function it_can_detect_wrong_url(LoggerInterface $logger, CacheItemPoolIn
);
$serverRequest = $creator->fromGlobals();

$this->beConstructedWith($serverRequest, $properties, $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger);
$this->beConstructedWith($serverRequest, $properties, $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger, new Introspector());

$parameters = [
'service' => 'service',
Expand Down Expand Up @@ -862,7 +913,7 @@ public function it_can_parse_a_bad_proxy_request_response(CacheItemPoolInterface
$psr17Factory // StreamFactory
);
$serverRequest = $creator->fromGlobals();
$this->beConstructedWith($serverRequest, CasSpecUtils::getTestProperties(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger);
$this->beConstructedWith($serverRequest, CasSpecUtils::getTestProperties(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger, new Introspector());

$url = 'http://from';

Expand All @@ -887,7 +938,7 @@ public function it_can_parse_a_good_proxy_request_response(CacheItemPoolInterfac
$psr17Factory // StreamFactory
);
$serverRequest = $creator->fromGlobals();
$this->beConstructedWith($serverRequest, CasSpecUtils::getTestProperties(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger);
$this->beConstructedWith($serverRequest, CasSpecUtils::getTestProperties(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger, new Introspector());

$url = 'http://from';

Expand Down Expand Up @@ -928,7 +979,7 @@ public function it_can_parse_json_in_a_response(LoggerInterface $logger, CacheIt
$psr17Factory // StreamFactory
);
$serverRequest = $creator->fromGlobals();
$this->beConstructedWith($serverRequest, $properties, $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger);
$this->beConstructedWith($serverRequest, $properties, $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger, new Introspector());

$request = new ServerRequest('GET', 'http://local/cas/serviceValidate?service=service&ticket=ticket&format=JSON');

Expand Down Expand Up @@ -980,7 +1031,7 @@ public function it_can_request_a_proxy_ticket(LoggerInterface $logger, CacheItem

//$logger = new Logger('psrcas', [new StreamHandler('php://stderr')]);

$this->beConstructedWith($serverRequest, CasSpecUtils::getTestProperties(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger);
$this->beConstructedWith($serverRequest, CasSpecUtils::getTestProperties(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger, new Introspector());

$url = 'http://from';

Expand Down Expand Up @@ -1032,7 +1083,7 @@ public function it_can_validate_a_bad_proxy_ticket(LoggerInterface $logger, Cach
);
$serverRequest = $creator->fromGlobals();

$this->beConstructedWith($serverRequest, $properties, $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger);
$this->beConstructedWith($serverRequest, $properties, $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger, new Introspector());

$request = new ServerRequest('POST', 'foo');

Expand Down Expand Up @@ -1063,7 +1114,7 @@ public function it_can_validate_a_bad_service_validate_request(LoggerInterface $
$psr17Factory, // UploadedFileFactory
$psr17Factory // StreamFactory
);
$this->beConstructedWith($creator->fromGlobals(), CasSpecUtils::getTestProperties(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger);
$this->beConstructedWith($creator->fromGlobals(), CasSpecUtils::getTestProperties(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger, new Introspector());

$from = 'http://from/';

Expand Down Expand Up @@ -1150,7 +1201,7 @@ public function it_can_validate_a_good_proxy_ticket(LoggerInterface $logger, Cac

//$logger = new Logger('psrcas', [new StreamHandler('php://stderr')]);

$this->beConstructedWith($serverRequest, $properties, $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger);
$this->beConstructedWith($serverRequest, $properties, $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger, new Introspector());

$request = new ServerRequest('GET', 'http://local/cas/proxyValidate?service=service&ticket=ticket');

Expand Down Expand Up @@ -1249,7 +1300,7 @@ public function it_can_validate_a_good_service_validate_request(LoggerInterface
$psr17Factory, // UploadedFileFactory
$psr17Factory // StreamFactory
);
$this->beConstructedWith($creator->fromGlobals(), $properties, $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger);
$this->beConstructedWith($creator->fromGlobals(), $properties, $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger, new Introspector());

$from = 'http://local/';

Expand Down Expand Up @@ -1444,6 +1495,6 @@ public function let(LoggerInterface $logger, CacheItemPoolInterface $cache, Cach
->getItem('pgtIouWithPgtIdNull')
->willReturn($cacheItemPgtIdNull);

$this->beConstructedWith($serverRequest, $properties, $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger);
$this->beConstructedWith($serverRequest, $properties, $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger, new Introspector());
}
}
10 changes: 5 additions & 5 deletions spec/EcPhp/CasLib/Introspection/IntrospectorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function it_can_detect_a_proxy_response()
->withHeader('Content-Type', 'application/xml')
->withBody($psr17Factory->createStream($body));

$this::detect($response)
$this->detect($response)
->shouldBeAnInstanceOf(Proxy::class);
}

Expand All @@ -55,7 +55,7 @@ public function it_can_detect_a_service_validate_response()
->withHeader('Content-Type', 'application/xml')
->withBody($psr17Factory->createStream($body));

$this::detect($response)
$this->detect($response)
->shouldBeAnInstanceOf(ServiceValidate::class);

$body = <<< 'EOF'
Expand All @@ -72,7 +72,7 @@ public function it_can_detect_a_service_validate_response()
->withHeader('Content-Type', 'application/json')
->withBody($psr17Factory->createStream($body));

$this::detect($response)
$this->detect($response)
->shouldBeAnInstanceOf(ServiceValidate::class);
}

Expand All @@ -96,7 +96,7 @@ public function it_can_detect_a_service_validate_response_with_proxy()
->withHeader('Content-Type', 'application/xml')
->withBody($psr17Factory->createStream($body));

$this::detect($response)
$this->detect($response)
->shouldBeAnInstanceOf(ServiceValidate::class);
}

Expand Down Expand Up @@ -145,7 +145,7 @@ public function it_can_detect_an_authentication_failure_response()
->withHeader('Content-Type', 'application/xml')
->withBody($psr17Factory->createStream($body));

$this::detect($response)
$this->detect($response)
->shouldBeAnInstanceOf(AuthenticationFailure::class);
}

Expand Down
11 changes: 9 additions & 2 deletions spec/EcPhp/CasLib/Introspection/ProxySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace spec\EcPhp\CasLib\Introspection;

use EcPhp\CasLib\Introspection\Introspector;
use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7\Response;
use PhpSpec\ObjectBehavior;
Expand All @@ -30,8 +29,16 @@ public function it_can_detect_a_proxy_response()
->withHeader('Content-Type', 'application/xml')
->withBody($psr17Factory->createStream($body));

$parsed = [
'serviceResponse' => [
'proxySuccess' => [
'proxyTicket' => 'PT-214-A3OoEPNr4Q9kNNuYzmfN8azU31aDUsuW8nk380k7wDExT5PFJpxR1TrNI3q3VGzyDdi0DpZ1LKb8IhPKZKQvavW-8hnfexYjmLCx7qWNsLib1W-DCzzoLVTosAUFzP3XDn5dNzoNtxIXV9KSztF9fYhwHvU0',
],
],
];

$this
->beConstructedWith(Introspector::parse($response), 'XML', $response);
->beConstructedWith($parsed, 'XML', $response);

$this
->getProxyTicket()
Expand Down
76 changes: 57 additions & 19 deletions spec/EcPhp/CasLib/Introspection/ServiceValidateSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace spec\EcPhp\CasLib\Introspection;

use EcPhp\CasLib\Introspection\Introspector;
use EcPhp\CasLib\Introspection\Contract\IntrospectionInterface;
use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7\Response;
use PhpSpec\ObjectBehavior;
Expand Down Expand Up @@ -32,22 +32,30 @@ public function it_can_detect_a_proxy_service_validate_response()
->withHeader('Content-Type', 'application/xml')
->withBody($psr17Factory->createStream($body));

$credentials = [
'user' => 'user',
'proxyGrantingTicket' => 'proxyGrantingTicket',
'proxies' => [
'proxy' => [
'http://proxy1',
'http://proxy2',
],
],
];

$parsed = [
'serviceResponse' => [
'authenticationSuccess' => $credentials,
],
];

$this
->beConstructedWith(Introspector::parse($response), 'XML', $response);
->beConstructedWith($parsed, 'XML', $response);

$this
->getCredentials()
->shouldReturn(
[
'user' => 'user',
'proxyGrantingTicket' => 'proxyGrantingTicket',
'proxies' => [
'proxy' => [
'http://proxy1',
'http://proxy2',
],
],
]
$credentials
);

$this
Expand Down Expand Up @@ -85,17 +93,26 @@ public function it_can_detect_a_service_validate_response()
->withHeader('Content-Type', 'application/xml')
->withBody($psr17Factory->createStream($body));

$credentials = [
'user' => 'user',
'proxyGrantingTicket' => 'proxyGrantingTicket',
];

$parsed = [
'serviceResponse' => [
'authenticationSuccess' => [
'user' => 'user',
'proxyGrantingTicket' => 'proxyGrantingTicket',
],
],
];

$this
->beConstructedWith(Introspector::parse($response), 'XML', $response);
->beConstructedWith($parsed, 'XML', $response);

$this
->getCredentials()
->shouldReturn(
[
'user' => 'user',
'proxyGrantingTicket' => 'proxyGrantingTicket',
]
);
->shouldReturn($credentials);

$this
->getFormat()
Expand All @@ -109,4 +126,25 @@ public function it_can_detect_a_service_validate_response()
->getResponse()
->shouldReturn($response);
}

public function it_can_use_the_withParsedResponse_wither_method()
{
$psr17Factory = new Psr17Factory();
$body = 'body';

$response = (new Response(200))
->withHeader('Content-Type', 'application/xml')
->withBody($psr17Factory->createStream($body));

$this
->beConstructedWith([], 'XML', $response);

$this
->withParsedResponse(['foo'])
->shouldNotReturn($this);

$this
->withParsedResponse(['foo'])
->shouldReturnAnInstanceOf(IntrospectionInterface::class);
}
}
Loading

0 comments on commit a9e6d58

Please sign in to comment.