PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/ZfcUserTest/Authentication/Adapter/AdapterChainTest.php

https://gitlab.com/my-application.bjoernbartels.earth/ZfcUser
PHP | 311 lines | 175 code | 54 blank | 82 comment | 2 complexity | d931cdf16054c622e36746066d3c16ca MD5 | raw file
  1. <?php
  2. namespace ZfcUserTest\Authentication\Adapter;
  3. use Zend\EventManager\EventManagerInterface;
  4. use ZfcUser\Authentication\Adapter\AdapterChain;
  5. use ZfcUser\Authentication\Adapter\AdapterChainEvent;
  6. class AdapterChainTest extends \PHPUnit_Framework_TestCase
  7. {
  8. /**
  9. * The object to be tested.
  10. *
  11. * @var AdapterChain
  12. */
  13. protected $adapterChain;
  14. /**
  15. * Mock event manager.
  16. *
  17. * @var EventManagerInterface
  18. */
  19. protected $eventManager;
  20. /**
  21. * For tests where an event is required.
  22. *
  23. * @var \Zend\EventManager\EventInterface
  24. */
  25. protected $event;
  26. /**
  27. * Used when testing prepareForAuthentication.
  28. *
  29. * @var \Zend\Stdlib\RequestInterface
  30. */
  31. protected $request;
  32. /**
  33. * Prepare the objects to be tested.
  34. */
  35. protected function setUp()
  36. {
  37. $this->event = null;
  38. $this->request = null;
  39. $this->adapterChain = new AdapterChain();
  40. $this->eventManager = $this->getMock('Zend\EventManager\EventManagerInterface');
  41. $this->adapterChain->setEventManager($this->eventManager);
  42. }
  43. /**
  44. * @covers ZfcUser\Authentication\Adapter\AdapterChain::authenticate
  45. */
  46. public function testAuthenticate()
  47. {
  48. $event = $this->getMock('ZfcUser\Authentication\Adapter\AdapterChainEvent');
  49. $event->expects($this->once())
  50. ->method('getCode')
  51. ->will($this->returnValue(123));
  52. $event->expects($this->once())
  53. ->method('getIdentity')
  54. ->will($this->returnValue('identity'));
  55. $event->expects($this->once())
  56. ->method('getMessages')
  57. ->will($this->returnValue(array()));
  58. $this->eventManager->expects($this->once())
  59. ->method('getListeners')
  60. ->with($this->equalTo('authenticate'))
  61. ->will($this->returnValue(array()));
  62. $this->adapterChain->setEvent($event);
  63. $result = $this->adapterChain->authenticate();
  64. $this->assertInstanceOf('Zend\Authentication\Result', $result);
  65. $this->assertEquals($result->getIdentity(), 'identity');
  66. $this->assertEquals($result->getMessages(), array());
  67. }
  68. /**
  69. * @covers ZfcUser\Authentication\Adapter\AdapterChain::resetAdapters
  70. */
  71. public function testResetAdapters()
  72. {
  73. $listeners = array();
  74. for ($i=1; $i<=3; $i++) {
  75. $storage = $this->getMock('ZfcUser\Authentication\Storage\Db');
  76. $storage->expects($this->once())
  77. ->method('clear');
  78. $adapter = $this->getMock('ZfcUser\Authentication\Adapter\AbstractAdapter');
  79. $adapter->expects($this->once())
  80. ->method('getStorage')
  81. ->will($this->returnValue($storage));
  82. $callback = $this->getMockBuilder('Zend\Stdlib\CallbackHandler')->disableOriginalConstructor()->getMock();
  83. $callback->expects($this->once())
  84. ->method('getCallback')
  85. ->will($this->returnValue(array($adapter)));
  86. $listeners[] = $callback;
  87. }
  88. $this->eventManager->expects($this->once())
  89. ->method('getListeners')
  90. ->with($this->equalTo('authenticate'))
  91. ->will($this->returnValue($listeners));
  92. $result = $this->adapterChain->resetAdapters();
  93. $this->assertInstanceOf('ZfcUser\Authentication\Adapter\AdapterChain', $result);
  94. }
  95. /**
  96. * Get through the first part of SetUpPrepareForAuthentication
  97. */
  98. protected function setUpPrepareForAuthentication()
  99. {
  100. $this->request = $this->getMock('Zend\Stdlib\RequestInterface');
  101. $this->event = $this->getMock('ZfcUser\Authentication\Adapter\AdapterChainEvent');
  102. $this->event->expects($this->once())->method('setRequest')->with($this->request);
  103. $this->eventManager->expects($this->at(0))->method('trigger')->with('authenticate.pre');
  104. /**
  105. * @var $response Zend\EventManager\ResponseCollection
  106. */
  107. $responses = $this->getMock('Zend\EventManager\ResponseCollection');
  108. $this->eventManager->expects($this->at(1))
  109. ->method('trigger')
  110. ->with('authenticate', $this->event)
  111. ->will($this->returnCallback(function ($event, $target, $callback) use ($responses) {
  112. if (call_user_func($callback, $responses->last())) {
  113. $responses->setStopped(true);
  114. }
  115. return $responses;
  116. }));
  117. $this->adapterChain->setEvent($this->event);
  118. return $responses;
  119. }
  120. /**
  121. * Provider for testPrepareForAuthentication()
  122. *
  123. * @return array
  124. */
  125. public function identityProvider()
  126. {
  127. return array(
  128. array(true, true),
  129. array(false, false),
  130. );
  131. }
  132. /**
  133. * Tests prepareForAuthentication when falls through events.
  134. *
  135. * @param mixed $identity
  136. * @param bool $expected
  137. *
  138. * @dataProvider identityProvider
  139. * @covers ZfcUser\Authentication\Adapter\AdapterChain::prepareForAuthentication
  140. */
  141. public function testPrepareForAuthentication($identity, $expected)
  142. {
  143. $result = $this->setUpPrepareForAuthentication();
  144. $result->expects($this->once())->method('stopped')->will($this->returnValue(false));
  145. $this->event->expects($this->once())->method('getIdentity')->will($this->returnValue($identity));
  146. $this->assertEquals(
  147. $expected,
  148. $this->adapterChain->prepareForAuthentication($this->request),
  149. 'Asserting prepareForAuthentication() returns true'
  150. );
  151. }
  152. /**
  153. * Test prepareForAuthentication() when the returned collection contains stopped.
  154. *
  155. * @covers ZfcUser\Authentication\Adapter\AdapterChain::prepareForAuthentication
  156. */
  157. public function testPrepareForAuthenticationWithStoppedEvent()
  158. {
  159. $result = $this->setUpPrepareForAuthentication();
  160. $result->expects($this->once())->method('stopped')->will($this->returnValue(true));
  161. $lastResponse = $this->getMock('Zend\Stdlib\ResponseInterface');
  162. $result->expects($this->atLeastOnce())->method('last')->will($this->returnValue($lastResponse));
  163. $this->assertEquals(
  164. $lastResponse,
  165. $this->adapterChain->prepareForAuthentication($this->request),
  166. 'Asserting the Response returned from the event is returned'
  167. );
  168. }
  169. /**
  170. * Test prepareForAuthentication() when the returned collection contains stopped.
  171. *
  172. * @covers ZfcUser\Authentication\Adapter\AdapterChain::prepareForAuthentication
  173. * @expectedException ZfcUser\Exception\AuthenticationEventException
  174. */
  175. public function testPrepareForAuthenticationWithBadEventResult()
  176. {
  177. $result = $this->setUpPrepareForAuthentication();
  178. $result->expects($this->once())->method('stopped')->will($this->returnValue(true));
  179. $lastResponse = 'random-value';
  180. $result->expects($this->atLeastOnce())->method('last')->will($this->returnValue($lastResponse));
  181. $this->adapterChain->prepareForAuthentication($this->request);
  182. }
  183. /**
  184. * Test getEvent() when no event has previously been set.
  185. *
  186. * @covers ZfcUser\Authentication\Adapter\AdapterChain::getEvent
  187. */
  188. public function testGetEventWithNoEventSet()
  189. {
  190. $event = $this->adapterChain->getEvent();
  191. $this->assertInstanceOf(
  192. 'ZfcUser\Authentication\Adapter\AdapterChainEvent',
  193. $event,
  194. 'Asserting the adapter in an instance of ZfcUser\Authentication\Adapter\AdapterChainEvent'
  195. );
  196. $this->assertEquals(
  197. $this->adapterChain,
  198. $event->getTarget(),
  199. 'Asserting the Event target is the AdapterChain'
  200. );
  201. }
  202. /**
  203. * Test getEvent() when an event has previously been set.
  204. *
  205. * @covers ZfcUser\Authentication\Adapter\AdapterChain::setEvent
  206. * @covers ZfcUser\Authentication\Adapter\AdapterChain::getEvent
  207. */
  208. public function testGetEventWithEventSet()
  209. {
  210. $event = new \ZfcUser\Authentication\Adapter\AdapterChainEvent();
  211. $this->adapterChain->setEvent($event);
  212. $this->assertEquals(
  213. $event,
  214. $this->adapterChain->getEvent(),
  215. 'Asserting the event fetched is the same as the event set'
  216. );
  217. }
  218. /**
  219. * Tests the mechanism for casting one event type to AdapterChainEvent
  220. *
  221. * @covers ZfcUser\Authentication\Adapter\AdapterChain::setEvent
  222. */
  223. public function testSetEventWithDifferentEventType()
  224. {
  225. $testParams = array('testParam' => 'testValue');
  226. $event = new \Zend\EventManager\Event;
  227. $event->setParams($testParams);
  228. $this->adapterChain->setEvent($event);
  229. $returnEvent = $this->adapterChain->getEvent();
  230. $this->assertInstanceOf(
  231. 'ZfcUser\Authentication\Adapter\AdapterChainEvent',
  232. $returnEvent,
  233. 'Asserting the adapter in an instance of ZfcUser\Authentication\Adapter\AdapterChainEvent'
  234. );
  235. $this->assertEquals(
  236. $testParams,
  237. $returnEvent->getParams(),
  238. 'Asserting event parameters match'
  239. );
  240. }
  241. /**
  242. * Test the logoutAdapters method.
  243. *
  244. * @depends testGetEventWithEventSet
  245. * @covers ZfcUser\Authentication\Adapter\AdapterChain::logoutAdapters
  246. */
  247. public function testLogoutAdapters()
  248. {
  249. $event = new AdapterChainEvent();
  250. $this->eventManager
  251. ->expects($this->once())
  252. ->method('trigger')
  253. ->with('logout', $event);
  254. $this->adapterChain->setEvent($event);
  255. $this->adapterChain->logoutAdapters();
  256. }
  257. }