/** * @expectedException \Itkg\Consumer\Exception\DisabledServiceException */ public function testAccessServiceDisabled() { $eventDispatcher = new EventDispatcher(); $eventDispatcher->addSubscriber(new AccessListener()); $clientMock = $this->getMockBuilder('Itkg\\Consumer\\Client\\RestClient')->getMock(); $clientMock->expects($this->never())->method('sendRequest'); $service = new Service($eventDispatcher, $clientMock, array('identifier' => 'authenticable service', 'disabled' => true)); $service->sendRequest(Request::create('/')); }
public function testDeserializerAndDefaultDeserialization() { $eventDispatcher = new EventDispatcher(); $deserializerListener = new DeserializerListener(SerializerBuilder::create()->build()); $eventDispatcher->addSubscriber($deserializerListener); $clientMock = $this->getMockBuilder('Itkg\\Consumer\\Client\\RestClient')->getMock(); $service = new Service($eventDispatcher, $clientMock, array('response_type' => 'array')); $service->sendRequest(Request::create('/'), new Response('[{"title":"value"}]')); $this->assertEquals($deserializerListener, $deserializerListener->setSerializer(SerializerBuilder::create()->build())); $this->assertEquals(array(0 => array('title' => 'value')), $service->getResponse()->getDeserializedContent()); }
public function testAuthenticate() { $eventDispatcher = new EventDispatcher(); $eventDispatcher->addSubscriber(new AuthenticationListener()); $clientMock = $this->getMockBuilder('Itkg\\Consumer\\Client\\RestClient')->getMock(); $authenticatorMock = $this->getMockBuilder('Itkg\\Consumer\\Authentication\\AuthenticationProviderInterface')->getMock(); $authenticatorMock->expects($this->once())->method('authenticate'); $authenticatorMock->expects($this->exactly(2))->method('hydrate'); $service = new Service($eventDispatcher, $clientMock, array('identifier' => 'authenticable service', 'authentication_provider' => $authenticatorMock)); $service->sendRequest(Request::create('/')); $this->assertFalse($service->isAuthenticated()); $authenticatorMock->expects($this->any())->method('getToken')->will($this->returnValue('MY_TOKEN')); $service->sendRequest(Request::create('/')); $this->assertTrue($service->isAuthenticated()); }
public function testGetSetCache() { $registry = new Registry(); $eventDispatcher = new EventDispatcher(); $eventDispatcher->addSubscriber(new CacheListener($eventDispatcher)); $clientMock = $this->getMockBuilder('Itkg\\Consumer\\Client\\RestClient')->getMock(); $clientMock->expects($this->once())->method('sendRequest'); $cacheableService = new Service($eventDispatcher, $clientMock, array('identifier' => 'cacheable service', 'cache_serializer' => function (Response $response) { $response->setContent('My content'); return serialize($response); }, 'cache_adapter' => $registry)); $this->assertFalse($cacheableService->isLoaded()); $cacheableService->sendRequest(Request::create('/')); $this->assertNotNull($registry->get($cacheableService)); $cacheableService->sendRequest(Request::create('/')); $this->assertTrue($cacheableService->isLoaded()); }
public function testServiceSuccessAndFail() { $eventDispatcher = new EventDispatcher(); $eventDispatcher->addSubscriber(new LoggerListener()); $clientMock = $this->getMockBuilder('Itkg\\Consumer\\Client\\RestClient')->getMock(); $loggerMock = $this->getMockBuilder('Psr\\Log\\AbstractLogger')->disableOriginalConstructor()->getMock(); $loggerMock->expects($this->exactly(3))->method('info'); $loggerMock->expects($this->once())->method('error'); $loggableService = new Service($eventDispatcher, $clientMock, array('identifier' => 'loggable service', 'logger' => $loggerMock)); $loggableService->sendRequest(Request::create('/')); $clientMock->expects($this->once())->method('sendRequest')->will($this->throwException(new \Exception('KO'))); $loggableService->setClient($clientMock); try { $loggableService->sendRequest(Request::create('/')); } catch (\Exception $e) { $this->assertEquals($e, $loggableService->getException()); } }
public function testHeaderWithSecurity() { $optionsTotest = array('connection_timeout' => 2, 'trace' => false, 'encoding' => 'UTF8', 'soap_version' => SOAP_1_1, 'features' => SOAP_SINGLE_ELEMENT_ARRAYS, 'namespace' => 'http://my_namespace.com', 'uri' => '/my_ws', 'location' => 'http://location', 'must_understand' => true, 'signature' => 'my_signature', 'signature_namespace' => 'http://host/for/my/signature', 'login' => 'my_auth_login', 'password' => 'my_auth_password'); $headerXML = <<<EOF <wsse:Security SOAP-ENV:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"> <wsse:UsernameToken wsu:Id="UsernameToken-6868426" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"> <wsse:Username>my_login</wsse:Username> <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">xxxx</wsse:Password> </wsse:UsernameToken> </wsse:Security><Signature xmlns="http://host/for/my/signature">my_signature</Signature> EOF; $header = new \SoapHeader('http://my_namespace.com', 'Security', new \SoapVar($headerXML, XSD_ANYXML, null, 'http://my_namespace.com')); $options = array('login' => 'my_login', 'password' => 'xxxx', 'namespace' => 'http://my_namespace.com', 'uri' => '/my_ws', 'location' => 'http://location', 'must_understand' => true, 'signature' => 'my_signature', 'signature_namespace' => 'http://host/for/my/signature', 'http_auth_login' => 'my_auth_login', 'http_auth_password' => 'my_auth_password'); $client = $this->getMock('Itkg\\Consumer\\Client\\SoapClient', array('__soapCall', '__getLastResponse'), array(null, $options)); $client->expects($this->once())->method('__getLastResponse')->will($this->returnValue('My SOAP Response')); $request = $this->getMockBuilder('Symfony\\Component\\HttpFoundation\\Request')->disableOriginalConstructor()->setMethods(array('getContent', 'getPathInfo'))->getMock(); $request->expects($this->once())->method('getContent')->will($this->returnValue('My SOAP Response')); $request->expects($this->once())->method('getPathInfo')->will($this->returnValue('/')); $client->expects($this->once())->method('__soapCall')->with('/', array(new \SoapVar('My SOAP Response', XSD_ANYXML)), $optionsTotest, $header); $service = new Service(new EventDispatcher(), $client, array('identifier' => 'identifier')); $service->sendRequest($request)->getResponse(); }
public function testDefaultOptions() { $service = new Service(new EventDispatcher(), new RestClient()); $this->assertEquals('json', $service->getOption('response_format')); $this->assertEquals(null, $service->getOption('response_type')); $this->assertEquals('UNDEFINED', $service->getOption('identifier')); }