Skip to content

Commit

Permalink
Fixing tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
diosmosis committed Jan 4, 2015
1 parent 5e6e0e1 commit 07add53
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions tests/Unit/LdapClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
namespace Piwik\Plugins\LoginLdap\tests\Unit;

use Piwik\ErrorHandler;
use Piwik\Exception\ErrorException;
use Piwik\Log;
use Piwik\Plugins\LoginLdap\Ldap\Client as LdapClient;
use Piwik\Plugins\LoginLdap\Ldap\LdapFunctions;
Expand Down Expand Up @@ -39,6 +38,8 @@ public function setUp()

public function tearDown()
{
restore_error_handler();

LdapFunctions::$phpUnitMock = null;

Log::unsetInstance();
Expand Down Expand Up @@ -78,19 +79,17 @@ public function test_connect_Closes_IfConnectionCurrentlyOpen()
}

/**
* @expectedException \ErrorException
* @expectedException \Piwik\Exception\ErrorException
* @expectedExceptionMessage triggered error
*/
public function test_connect_ThrowsPhpErrors()
{
ErrorHandler::registerErrorHandler();
$this->setPiwikErrorHandling();

$this->addLdapMethodThatTriggersPhpError('ldap_connect');

$ldapClient = new LdapClient();
$ldapClient->connect("hostname", 1234);

restore_error_handler();
}

public function test_close_Succeeds_IfConnectionAlreadyClosed()
Expand All @@ -100,20 +99,18 @@ public function test_close_Succeeds_IfConnectionAlreadyClosed()
}

/**
* @expectedException \ErrorException
* @expectedException \Piwik\Exception\ErrorException
* @expectedExceptionMessage triggered error
*/
public function test_close_ThrowsPhpErrors()
{
ErrorHandler::registerErrorHandler();
$this->setPiwikErrorHandling();

$this->addLdapConnectMethodMock();
$this->addLdapMethodThatTriggersPhpError('ldap_close');

$ldapClient = new LdapClient("hostname", 1234);
$ldapClient->close();

restore_error_handler();
}

public function test_bind_ForwardsLdapBindResult()
Expand All @@ -126,36 +123,32 @@ public function test_bind_ForwardsLdapBindResult()
}

/**
* @expectedException \ErrorException
* @expectedException \Piwik\Exception\ErrorException
* @expectedExceptionMessage triggered error
*/
public function test_bind_ThrowsPhpErrors()
{
ErrorHandler::registerErrorHandler();
$this->setPiwikErrorHandling();

$this->addLdapMethodThatTriggersPhpError('ldap_bind');

$ldapClient = new LdapClient();
$ldapClient->bind("resource", "password");

restore_error_handler();
}

/**
* @expectedException \ErrorException
* @expectedException \Piwik\Exception\ErrorException
* @expectedExceptionMessage triggered error
*/
public function test_fetchAll_ThrowsPhpErrors()
{
ErrorHandler::registerErrorHandler();
$this->setPiwikErrorHandling();

$this->addLdapMethodThatTriggersPhpError('ldap_search');
$this->addLdapMethodThatTriggersPhpError('ldap_get_entries');

$ldapClient = new LdapClient();
$ldapClient->fetchAll("base dn", "filter");

restore_error_handler();
}

public function test_fetchAll_ReturnsNull_IfLdapSearchFailsSilently()
Expand Down Expand Up @@ -277,11 +270,13 @@ public function test_fetchAll_CorrectlyProcessesLdapSearchResults($ldapData, $ex
}

/**
* @expectedException ErrorException
* @expectedException \Piwik\Exception\ErrorException
* @expectedExceptionMessage triggered error
*/
public function test_count_ThrowsPhpErrors()
{
$this->setPiwikErrorHandling();

LdapFunctions::$phpUnitMock->expects($this->any())->method('ldap_search')->will($this->returnValue("resource"));
$this->addLdapMethodThatTriggersPhpError('ldap_count_entries');

Expand All @@ -290,10 +285,12 @@ public function test_count_ThrowsPhpErrors()
}

/**
* @expectedException ErrorException
* @expectedException \Exception
*/
public function test_count_Throws_IfLdapSearchReturnsNull()
{
$this->setPiwikErrorHandling();

LdapFunctions::$phpUnitMock->expects($this->any())->method('ldap_search')->will($this->returnValue(null));

$ldapClient = new LdapClient();
Expand Down Expand Up @@ -328,8 +325,14 @@ private function addLdapConnectMethodMock($hostname = null, $port = null)
private function addLdapMethodThatTriggersPhpError($methodName, $returnValue = null)
{
LdapFunctions::$phpUnitMock->expects($this->any())->method($methodName)->will($this->returnCallback(function () use ($returnValue) {
trigger_error(LdapClientTest::ERROR_MESSAGE);
trigger_error(LdapClientTest::ERROR_MESSAGE, E_USER_ERROR);
return $returnValue;
}));
}

private function setPiwikErrorHandling()
{
ErrorHandler::registerErrorHandler();
error_reporting(E_ALL);
}
}

0 comments on commit 07add53

Please sign in to comment.