|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace spec\FreeDSx\Ldap\Protocol\ClientProtocolHandler; |
| 6 | + |
| 7 | +use FreeDSx\Ldap\ClientOptions; |
| 8 | +use FreeDSx\Ldap\Control\Sync\SyncDoneControl; |
| 9 | +use FreeDSx\Ldap\Control\Sync\SyncRequestControl; |
| 10 | +use FreeDSx\Ldap\Entry\Entry; |
| 11 | +use FreeDSx\Ldap\Exception\RuntimeException; |
| 12 | +use FreeDSx\Ldap\LdapUrl; |
| 13 | +use FreeDSx\Ldap\Operation\Request\SyncRequest; |
| 14 | +use FreeDSx\Ldap\Operation\Response\SearchResultDone; |
| 15 | +use FreeDSx\Ldap\Operation\Response\SearchResultEntry; |
| 16 | +use FreeDSx\Ldap\Operation\Response\SearchResultReference; |
| 17 | +use FreeDSx\Ldap\Protocol\ClientProtocolHandler\ClientProtocolContext; |
| 18 | +use FreeDSx\Ldap\Protocol\ClientProtocolHandler\ClientSyncHandler; |
| 19 | +use FreeDSx\Ldap\Protocol\ClientProtocolHandler\RequestHandlerInterface; |
| 20 | +use FreeDSx\Ldap\Protocol\ClientProtocolHandler\ResponseHandlerInterface; |
| 21 | +use FreeDSx\Ldap\Protocol\LdapMessageRequest; |
| 22 | +use FreeDSx\Ldap\Protocol\LdapMessageResponse; |
| 23 | +use FreeDSx\Ldap\Protocol\Queue\ClientQueue; |
| 24 | +use PhpSpec\ObjectBehavior; |
| 25 | +use spec\FreeDSx\Ldap\TestFactoryTrait; |
| 26 | + |
| 27 | +class ClientSyncHandlerSpec extends ObjectBehavior |
| 28 | +{ |
| 29 | + use TestFactoryTrait; |
| 30 | + |
| 31 | + public function it_is_initializable(): void |
| 32 | + { |
| 33 | + $this->shouldHaveType(ClientSyncHandler::class); |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | + public function it_should_implement_ResponseHandlerInterface(): void |
| 38 | + { |
| 39 | + $this->shouldBeAnInstanceOf(ResponseHandlerInterface::class); |
| 40 | + } |
| 41 | + |
| 42 | + public function it_should_implement_RequestHandlerInterface(): void |
| 43 | + { |
| 44 | + $this->shouldBeAnInstanceOf(RequestHandlerInterface::class); |
| 45 | + } |
| 46 | + |
| 47 | + public function it_should_set_a_default_DN_for_a_request_that_has_none( |
| 48 | + ClientProtocolContext $context, |
| 49 | + LdapMessageResponse $response, |
| 50 | + ClientQueue $queue, |
| 51 | + LdapMessageRequest $message, |
| 52 | + SyncRequest $request |
| 53 | + ): void { |
| 54 | + $queue->getMessage(1)->shouldBeCalled()->willReturn($response); |
| 55 | + $queue->sendMessage($message)->shouldBeCalledOnce(); |
| 56 | + |
| 57 | + $message->getMessageId()->willReturn(1); |
| 58 | + $message->getRequest()->willReturn($request); |
| 59 | + $request->getBaseDn()->willReturn(null); |
| 60 | + |
| 61 | + $context->messageToSend()->willReturn($message); |
| 62 | + $context->getRequest()->willReturn($request); |
| 63 | + $context->getQueue()->willReturn($queue); |
| 64 | + $context->getOptions()->willReturn( |
| 65 | + (new ClientOptions()) |
| 66 | + ->setBaseDn('cn=foo') |
| 67 | + ); |
| 68 | + |
| 69 | + $request->setBaseDn('cn=foo') |
| 70 | + ->shouldBeCalledOnce(); |
| 71 | + |
| 72 | + $this->handleRequest($context); |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + public function it_should_retrieve_results_until_it_receives_a_search_done_with_a_sync_done_control( |
| 77 | + ClientQueue $queue, |
| 78 | + ): void { |
| 79 | + $messageTo = new LdapMessageRequest( |
| 80 | + 1, |
| 81 | + new SyncRequest(), |
| 82 | + new SyncRequestControl(), |
| 83 | + ); |
| 84 | + $response = new LdapMessageResponse( |
| 85 | + 1, |
| 86 | + new SearchResultEntry(new Entry('bar')) |
| 87 | + ); |
| 88 | + |
| 89 | + $entries = [ |
| 90 | + new SearchResultEntry(new Entry('foo')), |
| 91 | + new SearchResultEntry(new Entry('foo')), |
| 92 | + new SearchResultEntry(new Entry('foo')), |
| 93 | + ]; |
| 94 | + $referrals = [ |
| 95 | + new SearchResultReference(new LdapUrl('ldap://foo')), |
| 96 | + ]; |
| 97 | + |
| 98 | + $queue->getMessage(1)->willReturn( |
| 99 | + new LdapMessageResponse(1, $entries[0]), |
| 100 | + new LdapMessageResponse(1, $entries[1]), |
| 101 | + new LdapMessageResponse(1, $referrals[0]), |
| 102 | + new LdapMessageResponse(1, $entries[2]), |
| 103 | + new LdapMessageResponse( |
| 104 | + 1, |
| 105 | + new SearchResultDone( |
| 106 | + 0, |
| 107 | + 'cn=foo', |
| 108 | + 'bar' |
| 109 | + ), |
| 110 | + new SyncDoneControl('foo') |
| 111 | + ) |
| 112 | + ); |
| 113 | + |
| 114 | + $queue->getMessage(1) |
| 115 | + ->shouldBeCalledTimes(5); |
| 116 | + |
| 117 | + $this->handleResponse( |
| 118 | + $messageTo, |
| 119 | + $response, |
| 120 | + $queue, |
| 121 | + new ClientOptions() |
| 122 | + )->shouldBeLike( |
| 123 | + $this::makeSearchResponseFromEntries( |
| 124 | + dn: 'cn=foo', |
| 125 | + diagnostic: 'bar', |
| 126 | + controls: [ |
| 127 | + new SyncDoneControl('foo'), |
| 128 | + ] |
| 129 | + ) |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + public function it_should_throw_an_exception_if_a_sync_request_control_was_not_provided(ClientQueue $queue,): void |
| 134 | + { |
| 135 | + $this->shouldThrow(new RuntimeException(sprintf( |
| 136 | + 'Expected a "%s", but there is none.', |
| 137 | + SyncRequestControl::class, |
| 138 | + )))->during( |
| 139 | + 'handleResponse', |
| 140 | + [ |
| 141 | + new LdapMessageRequest( |
| 142 | + 1, |
| 143 | + new SyncRequest(), |
| 144 | + ), |
| 145 | + new LdapMessageResponse( |
| 146 | + 1, |
| 147 | + new SearchResultEntry(new Entry('bar')) |
| 148 | + ), |
| 149 | + $queue, |
| 150 | + new ClientOptions(), |
| 151 | + ] |
| 152 | + ); |
| 153 | + } |
| 154 | +} |
0 commit comments