LDAPListBaseTest.php 5.16 KB
<?php

/**
 * Copyright (c) 2016-2016} Andreas Heigl<andreas@heigl.org>
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @author    Andreas Heigl<andreas@heigl.org>
 * @copyright 2016-2016 Andreas Heigl
 * @license   http://www.opensource.org/licenses/mit-license.php MIT-License
 * @version   0.0
 * @since     07.06.2016
 * @link      http://github.com/heiglandreas/authLDAP
 */

namespace Org_Heigl\AuthLdapTest;

use Closure;
use Org_Heigl\AuthLdap\Exception\Error;
use Org_Heigl\AuthLdap\Exception\SearchUnsuccessfull;
use Org_Heigl\AuthLdap\LdapList;
use Org_Heigl\AuthLdap\Manager\Ldap;
use Org_Heigl\AuthLdap\LdapUri;
use Org_Heigl\AuthLdap\Wrapper\LdapFactory;
use Org_Heigl\AuthLdap\Wrapper\LdapInterface;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;

class LDAPListBaseTest extends TestCase
{
	private $ldapA;

	private $ldapB;

	public function setUp(): void
	{
		$this->ldapA = $this->getMockBuilder(Ldap::class)->disableOriginalConstructor()->getMock();
		$this->ldapB = $this->getMockBuilder(Ldap::class)->disableOriginalConstructor()->getMock();
		Assert::assertNotSame($this->ldapA, $this->ldapB);
		parent::setUp(); // TODO: Change the autogenerated stub
	}

	public function addingItemsWorks(): void
	{
		$list = new LdapList();

		$list->addLdap($this->ldapA);
		$list->addLdap($this->ldapB);

		Assert::assertSame([$this->ldapA, $this->ldapB], $this->getLdaps($list));
	}

	public function testFailingBindRemovesItemsFromList(): void
	{
		$list = new LdapList();

		$list->addLdap($this->ldapA);
		$list->addLdap($this->ldapB);

		$this->ldapA->method('bind')->willThrowException(new Error('throw'));
		$this->ldapB->method('bind')->willReturn($this->ldapB);

		$list->bind();

		Assert::assertCount(1, $this->getLdaps($list));
	}

	public function testFailingBindInAllConnectorsThrows(): void
	{
		$list = new LdapList();

		$list->addLdap($this->ldapA);
		$list->addLdap($this->ldapB);

		$this->ldapA->method('bind')->willThrowException(new Error('throw'));
		$this->ldapB->method('bind')->willThrowException(new Error('throw'));

		$this->expectException(Error::class);
		$this->expectExceptionMessage('No bind successfull');

		$list->bind();

		Assert::assertCount(0, $this->getLdaps($list));
	}

	public function testAuthenticatingViaListWorks(): void
	{
		$list = new LdapList();

		$list->addLdap($this->ldapA);
		$list->addLdap($this->ldapB);

		$this->ldapA->method('authenticate')->willReturn(false);
		$this->ldapB->method('authenticate')->willReturn(true);

		Assert::assertTrue($list->authenticate('foo', 'bar'));

		Assert::assertCount(1, $this->getLdaps($list));
	}

	public function testAuthenticatingViaListFailsWhenNoConnectorAuthenticates(): void
	{
		$list = new LdapList();

		$list->addLdap($this->ldapA);
		$list->addLdap($this->ldapB);

		$this->ldapA->method('authenticate')->willReturn(false);
		$this->ldapB->method('authenticate')->willReturn(false);

		Assert::assertFalse($list->authenticate('foo', 'bar'));

		Assert::assertCount(0, $this->getLdaps($list));
	}

	public function testSuccessfullSearchInOneConnectorReturnsResult(): void
	{
		$list = new LdapList();

		$list->addLdap($this->ldapA);
		$list->addLdap($this->ldapB);

		$this->ldapA->method('search')->willThrowException(new Error('Whoot'));
		$this->ldapB->method('search')->willReturn(['count' => 1, ['dn' => 'foo']]);

		Assert::assertEquals(['count' => 1, ['dn' => 'foo']], $list->search('uid=foo'));
	}

	public function testUnsuccessfullSearchWillThrow(): void
	{
		$list = new LdapList();

		$list->addLdap($this->ldapA);
		$list->addLdap($this->ldapB);

		$this->ldapA->method('search')->willThrowException(new Error('Whoot'));
		$this->ldapB->method('search')->willThrowException(new Error('Whoot2'));

		$this->expectException(SearchUnsuccessfull::class);

		$list->search('uid=foo');
	}


	private function getLdaps(LdapList $list): array
	{

		// Courtesy of Marco Pivetta
		//  https://ocramius.github.io/blog/accessing-private-php-class-members-without-reflection/
		$sweetsThief = function (LdapList $kitchen) {
			return $kitchen->items;
		};

		// Closure::bind() actually creates a new instance of the closure
		$sweetsThief = Closure::bind($sweetsThief, null, $list);

		return $sweetsThief($list);
	}
}