From 286add563cd5f2c0fa3aa5db04ab37e9483e6b7c Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Tue, 14 Jul 2020 10:34:39 +0200 Subject: [PATCH 1/3] Issue #11: Inject the Introspector object. --- spec/EcPhp/CasLib/CasSpec.php | 35 +++++----- .../CasLib/Introspection/IntrospectorSpec.php | 10 +-- spec/EcPhp/CasLib/Introspection/ProxySpec.php | 11 ++- .../Introspection/ServiceValidateSpec.php | 54 +++++++++----- spec/EcPhp/CasLib/Service/ProxySpec.php | 7 +- .../CasLib/Service/ProxyValidateSpec.php | 7 +- spec/tests/EcPhp/CasLib/CasSpec.php | 5 +- .../CasLib/Service/ProxyValidateSpec.php | 14 ++-- src/AbstractCas.php | 70 ++++--------------- src/Cas.php | 12 ++-- .../Contract/IntrospectorInterface.php | 10 +-- src/Introspection/Introspector.php | 6 +- src/Service/Proxy.php | 3 +- src/Service/Service.php | 31 ++++---- tests/Cas.php | 10 ++- tests/Service/ProxyValidate.php | 6 ++ 16 files changed, 148 insertions(+), 143 deletions(-) diff --git a/spec/EcPhp/CasLib/CasSpec.php b/spec/EcPhp/CasLib/CasSpec.php index 1edc808..799eb24 100644 --- a/spec/EcPhp/CasLib/CasSpec.php +++ b/spec/EcPhp/CasLib/CasSpec.php @@ -6,6 +6,7 @@ use EcPhp\CasLib\Cas; use EcPhp\CasLib\Configuration\Properties as CasProperties; +use EcPhp\CasLib\Introspection\Introspector; use EcPhp\CasLib\Utils\SimpleXml; use Exception; use InvalidArgumentException; @@ -113,7 +114,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() @@ -221,7 +222,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() @@ -321,7 +322,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'); @@ -352,7 +353,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() @@ -399,7 +400,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() @@ -430,7 +431,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() @@ -461,7 +462,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() @@ -539,7 +540,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', @@ -862,7 +863,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'; @@ -887,7 +888,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'; @@ -928,7 +929,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'); @@ -980,7 +981,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'; @@ -1032,7 +1033,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'); @@ -1063,7 +1064,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/'; @@ -1150,7 +1151,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'); @@ -1249,7 +1250,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/'; @@ -1444,6 +1445,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()); } } diff --git a/spec/EcPhp/CasLib/Introspection/IntrospectorSpec.php b/spec/EcPhp/CasLib/Introspection/IntrospectorSpec.php index 5fe6d7a..9e05bc7 100644 --- a/spec/EcPhp/CasLib/Introspection/IntrospectorSpec.php +++ b/spec/EcPhp/CasLib/Introspection/IntrospectorSpec.php @@ -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); } @@ -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' @@ -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); } @@ -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); } @@ -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); } diff --git a/spec/EcPhp/CasLib/Introspection/ProxySpec.php b/spec/EcPhp/CasLib/Introspection/ProxySpec.php index 983557a..76d7dbf 100644 --- a/spec/EcPhp/CasLib/Introspection/ProxySpec.php +++ b/spec/EcPhp/CasLib/Introspection/ProxySpec.php @@ -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; @@ -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() diff --git a/spec/EcPhp/CasLib/Introspection/ServiceValidateSpec.php b/spec/EcPhp/CasLib/Introspection/ServiceValidateSpec.php index 341a3a4..e87b261 100644 --- a/spec/EcPhp/CasLib/Introspection/ServiceValidateSpec.php +++ b/spec/EcPhp/CasLib/Introspection/ServiceValidateSpec.php @@ -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; @@ -32,22 +31,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 @@ -85,17 +92,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() diff --git a/spec/EcPhp/CasLib/Service/ProxySpec.php b/spec/EcPhp/CasLib/Service/ProxySpec.php index 4ffe3e5..78e69cb 100644 --- a/spec/EcPhp/CasLib/Service/ProxySpec.php +++ b/spec/EcPhp/CasLib/Service/ProxySpec.php @@ -4,6 +4,7 @@ namespace spec\EcPhp\CasLib\Service; +use EcPhp\CasLib\Introspection\Introspector; use EcPhp\CasLib\Service\Proxy; use Nyholm\Psr7\Factory\Psr17Factory; use Nyholm\Psr7\Response; @@ -16,7 +17,7 @@ class ProxySpec extends ObjectBehavior { - public function it_can_detect_a_wrong_proxy_response(ServerRequestInterface $serverRequest, ClientInterface $client, CacheItemPoolInterface $cache, LoggerInterface $logger) + public function it_can_detect_a_wrong_proxy_response() { $body = <<< 'EOF' @@ -46,7 +47,7 @@ public function it_can_detect_when_no_credentials() ->shouldBeNull(); } - public function it_is_initializable(ServerRequestInterface $serverRequest, ClientInterface $client, CacheItemPoolInterface $cache, LoggerInterface $logger) + public function it_is_initializable() { $this->shouldHaveType(Proxy::class); } @@ -55,6 +56,6 @@ public function let(ServerRequestInterface $serverRequest, ClientInterface $clie { $psr17Factory = new Psr17Factory(); - $this->beConstructedWith($serverRequest, [], Cas::getTestProperties(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger); + $this->beConstructedWith($serverRequest, [], Cas::getTestProperties(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger, new Introspector()); } } diff --git a/spec/EcPhp/CasLib/Service/ProxyValidateSpec.php b/spec/EcPhp/CasLib/Service/ProxyValidateSpec.php index bc5bdb8..16ecc91 100644 --- a/spec/EcPhp/CasLib/Service/ProxyValidateSpec.php +++ b/spec/EcPhp/CasLib/Service/ProxyValidateSpec.php @@ -4,6 +4,7 @@ namespace spec\EcPhp\CasLib\Service; +use EcPhp\CasLib\Introspection\Introspector; use EcPhp\CasLib\Service\ProxyValidate; use Nyholm\Psr7\Factory\Psr17Factory; use Nyholm\Psr7\Response; @@ -61,7 +62,7 @@ public function it_can_get_credentials_with_pgtUrl(ServerRequestInterface $serve ->getItem('pgtIou') ->willReturn($cacheItem); - $this->beConstructedWith($serverRequest, ['service' => 'service', 'ticket' => 'ST-ticket-pgt'], Cas::getTestPropertiesWithPgtUrl(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger); + $this->beConstructedWith($serverRequest, ['service' => 'service', 'ticket' => 'ST-ticket-pgt'], Cas::getTestPropertiesWithPgtUrl(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger, new Introspector()); $response = $this->handle(); @@ -84,7 +85,7 @@ public function it_can_get_credentials_without_pgtUrl(ServerRequestInterface $se $serverRequest = new ServerRequest('GET', 'http://from'); $client = new Psr18Client(CasSpecUtils::getHttpClientMock()); - $this->beConstructedWith($serverRequest, ['service' => 'service', 'ticket' => 'ticket'], Cas::getTestProperties(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger); + $this->beConstructedWith($serverRequest, ['service' => 'service', 'ticket' => 'ticket'], Cas::getTestProperties(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger, new Introspector()); $response = $this->handle(); @@ -109,6 +110,6 @@ public function let(ServerRequestInterface $serverRequest, ClientInterface $clie { $psr17Factory = new Psr17Factory(); - $this->beConstructedWith($serverRequest, [], Cas::getTestProperties(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger); + $this->beConstructedWith($serverRequest, [], Cas::getTestProperties(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger, new Introspector()); } } diff --git a/spec/tests/EcPhp/CasLib/CasSpec.php b/spec/tests/EcPhp/CasLib/CasSpec.php index 5872ea0..fdda411 100644 --- a/spec/tests/EcPhp/CasLib/CasSpec.php +++ b/spec/tests/EcPhp/CasLib/CasSpec.php @@ -4,6 +4,7 @@ namespace spec\tests\EcPhp\CasLib; +use EcPhp\CasLib\Introspection\Introspector; use Nyholm\Psr7\Factory\Psr17Factory; use Nyholm\Psr7Server\ServerRequestCreator; use PhpSpec\ObjectBehavior; @@ -28,7 +29,7 @@ public function it_can_test_the_proxy_mode_with_pgtUrl(CacheItemPoolInterface $c ); $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()); $this ->proxyMode() @@ -49,7 +50,7 @@ public function it_can_test_the_proxy_mode_without_pgtUrl(CacheItemPoolInterface ); $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()); $this ->proxyMode() diff --git a/spec/tests/EcPhp/CasLib/Service/ProxyValidateSpec.php b/spec/tests/EcPhp/CasLib/Service/ProxyValidateSpec.php index 49c802a..4e45ca7 100644 --- a/spec/tests/EcPhp/CasLib/Service/ProxyValidateSpec.php +++ b/spec/tests/EcPhp/CasLib/Service/ProxyValidateSpec.php @@ -4,6 +4,8 @@ namespace spec\tests\EcPhp\CasLib\Service; +use EcPhp\CasLib\Introspection\Contract\IntrospectorInterface; +use EcPhp\CasLib\Introspection\Introspector; use Nyholm\Psr7\Factory\Psr17Factory; use Nyholm\Psr7\Response; use Nyholm\Psr7\ServerRequest; @@ -55,7 +57,7 @@ public function it_can_check_the_visibility_of_some_methods(CacheItemPoolInterfa ->getItem('pgtIou') ->willReturn($cacheItem); - $this->beConstructedWith($serverRequest, [], Cas::getTestProperties(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger); + $this->beConstructedWith($serverRequest, [], Cas::getTestProperties(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger, new Introspector()); $this ->getClient() @@ -93,6 +95,10 @@ public function it_can_check_the_visibility_of_some_methods(CacheItemPoolInterfa ->getRequest() ->shouldBeAnInstanceOf(RequestInterface::class); + $this + ->getIntrospector() + ->shouldBeAnInstanceOf(IntrospectorInterface::class); + $response = [ 'serviceResponse' => [ 'authenticationSuccess' => [ @@ -134,7 +140,7 @@ public function it_can_log_debugging_information_when_trying_to_get_unexisting_p ->hasItem('pgtIou') ->willReturn(false); - $this->beConstructedWith($serverRequest, [], Cas::getTestProperties(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger); + $this->beConstructedWith($serverRequest, [], Cas::getTestProperties(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger, new Introspector()); $response = [ 'serviceResponse' => [ @@ -160,7 +166,7 @@ public function it_can_parse_a_response(CacheItemPoolInterface $cache, LoggerInt $serverRequest = new ServerRequest('GET', 'http://from'); $client = new Psr18Client(Cas::getHttpClientMock()); - $this->beConstructedWith($serverRequest, [], Cas::getTestProperties(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger); + $this->beConstructedWith($serverRequest, [], Cas::getTestProperties(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger, new Introspector()); $response = new Response(200, [], 'foo'); @@ -182,6 +188,6 @@ public function let(ServerRequestInterface $serverRequest, ClientInterface $clie { $psr17Factory = new Psr17Factory(); - $this->beConstructedWith($serverRequest, [], Cas::getTestProperties(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger); + $this->beConstructedWith($serverRequest, [], Cas::getTestProperties(), $client, $psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory, $cache, $logger, new Introspector()); } } diff --git a/src/AbstractCas.php b/src/AbstractCas.php index 269ba12..99c67ec 100644 --- a/src/AbstractCas.php +++ b/src/AbstractCas.php @@ -5,7 +5,7 @@ namespace EcPhp\CasLib; use EcPhp\CasLib\Configuration\PropertiesInterface; -use EcPhp\CasLib\Introspection\Introspector; +use EcPhp\CasLib\Introspection\Contract\IntrospectorInterface; use EcPhp\CasLib\Utils\Uri; use Psr\Cache\CacheItemPoolInterface; use Psr\Http\Client\ClientInterface; @@ -38,6 +38,11 @@ abstract class AbstractCas implements CasInterface */ private $client; + /** + * @var \EcPhp\CasLib\Introspection\Contract\IntrospectorInterface + */ + private $introspector; + /** * The logger. * @@ -87,9 +92,6 @@ abstract class AbstractCas implements CasInterface */ private $uriFactory; - /** - * AbstractCas constructor. - */ public function __construct( ServerRequestInterface $serverRequest, PropertiesInterface $properties, @@ -99,7 +101,8 @@ public function __construct( RequestFactoryInterface $requestFactory, StreamFactoryInterface $streamFactory, CacheItemPoolInterface $cache, - LoggerInterface $logger + LoggerInterface $logger, + IntrospectorInterface $introspector ) { $this->serverRequest = $serverRequest; $this->properties = $properties; @@ -110,6 +113,7 @@ public function __construct( $this->streamFactory = $streamFactory; $this->cache = $cache; $this->logger = $logger; + $this->introspector = $introspector; } /** @@ -125,8 +129,7 @@ public function authenticate(array $parameters = []): ?array return null; } - return Introspector::detect($response) - ->getParsedResponse(); + return $this->getIntrospector()->detect($response)->getParsedResponse(); } /** @@ -181,89 +184,46 @@ public function withServerRequest(ServerRequestInterface $serverRequest): CasInt return $clone; } - /** - * Get the cache. - * - * @return \Psr\Cache\CacheItemPoolInterface - * The cache. - */ protected function getCache(): CacheItemPoolInterface { return $this->cache; } - /** - * Get the HTTP client. - * - * @return \Psr\Http\Client\ClientInterface - * The HTTP client. - */ protected function getHttpClient(): ClientInterface { return $this->client; } - /** - * Get the logger. - * - * @return \Psr\Log\LoggerInterface - * The logger. - */ + protected function getIntrospector(): IntrospectorInterface + { + return $this->introspector; + } + protected function getLogger(): LoggerInterface { return $this->logger; } - /** - * Get the request factory. - * - * @return \Psr\Http\Message\RequestFactoryInterface - * The request factory. - */ protected function getRequestFactory(): RequestFactoryInterface { return $this->requestFactory; } - /** - * Get the response factory. - * - * @return \Psr\Http\Message\ResponseFactoryInterface - * The response factory. - */ protected function getResponseFactory(): ResponseFactoryInterface { return $this->responseFactory; } - /** - * Get the server request. - * - * @return \Psr\Http\Message\ServerRequestInterface - * The server request. - */ protected function getServerRequest(): ServerRequestInterface { return $this->serverRequest; } - /** - * Get the stream factory. - * - * @return \Psr\Http\Message\StreamFactoryInterface - * The stream factory. - */ protected function getStreamFactory(): StreamFactoryInterface { return $this->streamFactory; } - /** - * Get the URI factory. - * - * @return \Psr\Http\Message\UriFactoryInterface - * The URI factory. - */ protected function getUriFactory(): UriFactoryInterface { return $this->uriFactory; diff --git a/src/Cas.php b/src/Cas.php index 16d7ee8..733f18b 100644 --- a/src/Cas.php +++ b/src/Cas.php @@ -12,9 +12,6 @@ use EcPhp\CasLib\Service\ServiceValidate; use Psr\Http\Message\ResponseInterface; -/** - * Class Cas. - */ final class Cas extends AbstractCas { /** @@ -91,7 +88,8 @@ public function requestProxyTicket(array $parameters = [], ?ResponseInterface $r $this->getRequestFactory(), $this->getStreamFactory(), $this->getCache(), - $this->getLogger() + $this->getLogger(), + $this->getIntrospector() ); if (null === $response) { @@ -132,7 +130,8 @@ public function requestProxyValidate( $this->getRequestFactory(), $this->getStreamFactory(), $this->getCache(), - $this->getLogger() + $this->getLogger(), + $this->getIntrospector() ); if (null === $response) { @@ -173,7 +172,8 @@ public function requestServiceValidate( $this->getRequestFactory(), $this->getStreamFactory(), $this->getCache(), - $this->getLogger() + $this->getLogger(), + $this->getIntrospector() ); if (null === $response) { diff --git a/src/Introspection/Contract/IntrospectorInterface.php b/src/Introspection/Contract/IntrospectorInterface.php index cd5e8cb..00a97ba 100644 --- a/src/Introspection/Contract/IntrospectorInterface.php +++ b/src/Introspection/Contract/IntrospectorInterface.php @@ -6,10 +6,12 @@ use Psr\Http\Message\ResponseInterface; -/** - * Interface IntrospectorInterface. - */ interface IntrospectorInterface { - public static function detect(ResponseInterface $response): IntrospectionInterface; + public function detect(ResponseInterface $response): IntrospectionInterface; + + /** + * @return array + */ + public function parse(ResponseInterface $response, string $format = 'XML'): array; } diff --git a/src/Introspection/Introspector.php b/src/Introspection/Introspector.php index 811fdb2..5b69e4a 100644 --- a/src/Introspection/Introspector.php +++ b/src/Introspection/Introspector.php @@ -17,7 +17,7 @@ */ final class Introspector implements IntrospectorInterface { - public static function detect(ResponseInterface $response): IntrospectionInterface + public function detect(ResponseInterface $response): IntrospectionInterface { $format = null; @@ -45,7 +45,7 @@ public static function detect(ResponseInterface $response): IntrospectionInterfa } try { - $data = self::parse($response, $format); + $data = $this->parse($response, $format); } catch (InvalidArgumentException $exception) { throw new InvalidArgumentException($exception->getMessage()); } @@ -70,7 +70,7 @@ public static function detect(ResponseInterface $response): IntrospectionInterfa * * @return mixed[] */ - public static function parse(ResponseInterface $response, string $format = 'XML'): array + public function parse(ResponseInterface $response, string $format = 'XML'): array { if ('XML' === $format) { $xml = SimpleXml::fromString((string) $response->getBody()); diff --git a/src/Service/Proxy.php b/src/Service/Proxy.php index 969e5b5..0652954 100644 --- a/src/Service/Proxy.php +++ b/src/Service/Proxy.php @@ -4,7 +4,6 @@ namespace EcPhp\CasLib\Service; -use EcPhp\CasLib\Introspection\Introspector; use InvalidArgumentException; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; @@ -20,7 +19,7 @@ final class Proxy extends Service implements ServiceInterface public function getCredentials(ResponseInterface $response): ?ResponseInterface { try { - $introspect = Introspector::detect($response); + $introspect = $this->getIntrospector()->detect($response); } catch (InvalidArgumentException $exception) { $this ->getLogger() diff --git a/src/Service/Service.php b/src/Service/Service.php index 293a313..505c21c 100644 --- a/src/Service/Service.php +++ b/src/Service/Service.php @@ -6,8 +6,8 @@ use EcPhp\CasLib\Configuration\PropertiesInterface; use EcPhp\CasLib\Handler\Handler; +use EcPhp\CasLib\Introspection\Contract\IntrospectorInterface; use EcPhp\CasLib\Introspection\Contract\ServiceValidate; -use EcPhp\CasLib\Introspection\Introspector; use InvalidArgumentException; use Psr\Cache\CacheItemPoolInterface; use Psr\Http\Client\ClientExceptionInterface; @@ -26,9 +26,6 @@ use const JSON_ERROR_NONE; -/** - * Class Service. - */ abstract class Service extends Handler { /** @@ -37,15 +34,15 @@ abstract class Service extends Handler private $client; /** - * @var \Psr\Http\Message\RequestFactoryInterface + * @var \EcPhp\CasLib\Introspection\Contract\IntrospectorInterface */ - private $requestFactory; + private $introspector; /** - * Service constructor. - * - * @param array[]|string[] $parameters + * @var \Psr\Http\Message\RequestFactoryInterface */ + private $requestFactory; + public function __construct( ServerRequestInterface $serverRequest, array $parameters, @@ -56,7 +53,8 @@ public function __construct( RequestFactoryInterface $requestFactory, StreamFactoryInterface $streamFactory, CacheItemPoolInterface $cache, - LoggerInterface $logger + LoggerInterface $logger, + IntrospectorInterface $introspector ) { parent::__construct( $serverRequest, @@ -71,6 +69,7 @@ public function __construct( $this->client = $client; $this->requestFactory = $requestFactory; + $this->introspector = $introspector; } /** @@ -79,7 +78,7 @@ public function __construct( public function getCredentials(ResponseInterface $response): ?ResponseInterface { try { - $introspect = Introspector::detect($response); + $introspect = $this->getIntrospector()->detect($response); } catch (InvalidArgumentException $exception) { $this ->getLogger() @@ -159,9 +158,11 @@ protected function getClient(): ClientInterface return $this->client; } - /** - * Get the request. - */ + protected function getIntrospector(): IntrospectorInterface + { + return $this->introspector; + } + protected function getRequest(): RequestInterface { return $this->getRequestFactory()->createRequest('GET', $this->getUri()); @@ -188,7 +189,7 @@ protected function parse(ResponseInterface $response): array $format = $this->getProtocolProperties()['default_parameters']['format'] ?? 'XML'; try { - $array = Introspector::parse($response, $format); + $array = $this->getIntrospector()->parse($response, $format); } catch (InvalidArgumentException $exception) { $this ->getLogger() diff --git a/tests/Cas.php b/tests/Cas.php index 2d3647e..fcb134f 100644 --- a/tests/Cas.php +++ b/tests/Cas.php @@ -6,6 +6,7 @@ use EcPhp\CasLib\AbstractCas; use EcPhp\CasLib\Configuration\PropertiesInterface; +use EcPhp\CasLib\Introspection\Contract\IntrospectorInterface; use Psr\Cache\CacheItemPoolInterface; use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestFactoryInterface; @@ -32,7 +33,8 @@ public function __construct( RequestFactoryInterface $requestFactory, StreamFactoryInterface $streamFactory, CacheItemPoolInterface $cache, - LoggerInterface $logger + LoggerInterface $logger, + IntrospectorInterface $introspector ) { parent::__construct( $serverRequest, @@ -43,7 +45,8 @@ public function __construct( $requestFactory, $streamFactory, $cache, - $logger + $logger, + $introspector ); $this->cas = new \EcPhp\CasLib\Cas( @@ -55,7 +58,8 @@ public function __construct( $requestFactory, $streamFactory, $cache, - $logger + $logger, + $introspector ); } diff --git a/tests/Service/ProxyValidate.php b/tests/Service/ProxyValidate.php index 541f26c..8041ec5 100644 --- a/tests/Service/ProxyValidate.php +++ b/tests/Service/ProxyValidate.php @@ -4,6 +4,7 @@ namespace tests\EcPhp\CasLib\Service; +use EcPhp\CasLib\Introspection\Contract\IntrospectorInterface; use EcPhp\CasLib\Service\Service; use EcPhp\CasLib\Utils\Uri; use Psr\Cache\CacheItemPoolInterface; @@ -30,6 +31,11 @@ public function getClient(): ClientInterface return parent::getClient(); } + public function getIntrospector(): IntrospectorInterface + { + return parent::getIntrospector(); + } + public function getLogger(): LoggerInterface { return parent::getLogger(); From f43c64174e6af6622f90dc1d4d55f9328471057e Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Tue, 14 Jul 2020 10:55:31 +0200 Subject: [PATCH 2/3] Issue #11: Add new IntrospectionInterface::withParsedResponse() method. --- .../Introspection/ServiceValidateSpec.php | 22 +++++++++++++++++++ .../Contract/IntrospectionInterface.php | 8 ++----- src/Introspection/Introspection.php | 14 ++++++++---- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/spec/EcPhp/CasLib/Introspection/ServiceValidateSpec.php b/spec/EcPhp/CasLib/Introspection/ServiceValidateSpec.php index e87b261..a4c00dd 100644 --- a/spec/EcPhp/CasLib/Introspection/ServiceValidateSpec.php +++ b/spec/EcPhp/CasLib/Introspection/ServiceValidateSpec.php @@ -4,6 +4,7 @@ namespace spec\EcPhp\CasLib\Introspection; +use EcPhp\CasLib\Introspection\Contract\IntrospectionInterface; use Nyholm\Psr7\Factory\Psr17Factory; use Nyholm\Psr7\Response; use PhpSpec\ObjectBehavior; @@ -125,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); + } } diff --git a/src/Introspection/Contract/IntrospectionInterface.php b/src/Introspection/Contract/IntrospectionInterface.php index a2becf0..d6b7c16 100644 --- a/src/Introspection/Contract/IntrospectionInterface.php +++ b/src/Introspection/Contract/IntrospectionInterface.php @@ -6,17 +6,13 @@ use Psr\Http\Message\ResponseInterface; -/** - * Interface IntrospectionInterface. - */ interface IntrospectionInterface { public function getFormat(): string; - /** - * @return array[] - */ public function getParsedResponse(): array; public function getResponse(): ResponseInterface; + + public function withParsedResponse(array $parsedResponse): IntrospectionInterface; } diff --git a/src/Introspection/Introspection.php b/src/Introspection/Introspection.php index 1756258..9eb0c83 100644 --- a/src/Introspection/Introspection.php +++ b/src/Introspection/Introspection.php @@ -4,6 +4,7 @@ namespace EcPhp\CasLib\Introspection; +use EcPhp\CasLib\Introspection\Contract\IntrospectionInterface; use Psr\Http\Message\ResponseInterface; abstract class Introspection @@ -19,7 +20,7 @@ abstract class Introspection private $parsedResponse; /** - * @var \Psr\Http\Message\ResponseInterface + * @var \Psr\Hstaticttp\Message\ResponseInterface */ private $response; @@ -40,9 +41,6 @@ public function getFormat(): string return $this->format; } - /** - * @return array[] - */ public function getParsedResponse(): array { return $this->parsedResponse; @@ -52,4 +50,12 @@ public function getResponse(): ResponseInterface { return $this->response; } + + public function withParsedResponse(array $parsedResponse): IntrospectionInterface + { + $clone = clone $this; + $clone->parsedResponse = $parsedResponse; + + return $clone; + } } From 0dc346ff0401b0a412ab8386f4fc08ed35153f5f Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Tue, 14 Jul 2020 12:00:28 +0200 Subject: [PATCH 3/3] Issue #11: Add CasInterface::detect() method. --- spec/EcPhp/CasLib/CasSpec.php | 50 +++++++++++++++++++ src/AbstractCas.php | 6 +++ src/CasInterface.php | 3 ++ .../Contract/ServiceValidate.php | 9 ---- src/Introspection/Introspector.php | 3 -- src/Introspection/ServiceValidate.php | 9 ---- 6 files changed, 59 insertions(+), 21 deletions(-) diff --git a/spec/EcPhp/CasLib/CasSpec.php b/spec/EcPhp/CasLib/CasSpec.php index 799eb24..e110596 100644 --- a/spec/EcPhp/CasLib/CasSpec.php +++ b/spec/EcPhp/CasLib/CasSpec.php @@ -7,6 +7,7 @@ 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; @@ -492,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' + + + username + + +EOF; + + $headers = [ + 'Content-Type' => 'application/xml', + ]; + + $response = new Response( + 200, + $headers, + $body + ); + + $this + ->detect( + $response + ) + ->shouldReturnAnInstanceOf(ServiceValidate::class); + + $body = <<< 'EOF' + + + username + + +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/'; diff --git a/src/AbstractCas.php b/src/AbstractCas.php index 99c67ec..b16ad22 100644 --- a/src/AbstractCas.php +++ b/src/AbstractCas.php @@ -5,6 +5,7 @@ namespace EcPhp\CasLib; use EcPhp\CasLib\Configuration\PropertiesInterface; +use EcPhp\CasLib\Introspection\Contract\IntrospectionInterface; use EcPhp\CasLib\Introspection\Contract\IntrospectorInterface; use EcPhp\CasLib\Utils\Uri; use Psr\Cache\CacheItemPoolInterface; @@ -132,6 +133,11 @@ public function authenticate(array $parameters = []): ?array return $this->getIntrospector()->detect($response)->getParsedResponse(); } + public function detect(ResponseInterface $response): IntrospectionInterface + { + return $this->getIntrospector()->detect($response); + } + /** * {@inheritdoc} */ diff --git a/src/CasInterface.php b/src/CasInterface.php index ea4ab8b..336a654 100644 --- a/src/CasInterface.php +++ b/src/CasInterface.php @@ -5,6 +5,7 @@ namespace EcPhp\CasLib; use EcPhp\CasLib\Configuration\PropertiesInterface; +use EcPhp\CasLib\Introspection\Contract\IntrospectionInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; @@ -24,6 +25,8 @@ interface CasInterface */ public function authenticate(array $parameters = []): ?array; + public function detect(ResponseInterface $response): IntrospectionInterface; + /** * Get the CAS properties. * diff --git a/src/Introspection/Contract/ServiceValidate.php b/src/Introspection/Contract/ServiceValidate.php index a7a5de6..cff7e92 100644 --- a/src/Introspection/Contract/ServiceValidate.php +++ b/src/Introspection/Contract/ServiceValidate.php @@ -4,18 +4,9 @@ namespace EcPhp\CasLib\Introspection\Contract; -/** - * Interface ServiceValidate. - */ interface ServiceValidate extends IntrospectionInterface { - /** - * @return array[] - */ public function getCredentials(): array; - /** - * @return array[] - */ public function getProxies(): array; } diff --git a/src/Introspection/Introspector.php b/src/Introspection/Introspector.php index 5b69e4a..0c80f14 100644 --- a/src/Introspection/Introspector.php +++ b/src/Introspection/Introspector.php @@ -12,9 +12,6 @@ use const JSON_ERROR_NONE; -/** - * Class Introspector. - */ final class Introspector implements IntrospectorInterface { public function detect(ResponseInterface $response): IntrospectionInterface diff --git a/src/Introspection/ServiceValidate.php b/src/Introspection/ServiceValidate.php index f509523..c389da9 100644 --- a/src/Introspection/ServiceValidate.php +++ b/src/Introspection/ServiceValidate.php @@ -6,22 +6,13 @@ use EcPhp\CasLib\Introspection\Contract\ServiceValidate as ServiceValidateInterface; -/** - * Class ServiceValidate. - */ final class ServiceValidate extends Introspection implements ServiceValidateInterface { - /** - * {@inheritdoc} - */ public function getCredentials(): array { return $this->getParsedResponse()['serviceResponse']['authenticationSuccess']; } - /** - * {@inheritdoc} - */ public function getProxies(): array { $hasProxy = isset($this->getParsedResponse()['serviceResponse']['authenticationSuccess']['proxies']);