Complete tests on Entry and improve php2ldap() method
This commit is contained in:
parent
489dd7af3a
commit
8dad96ee32
3 changed files with 409 additions and 8 deletions
|
@ -20,5 +20,5 @@ parameters:
|
|||
message: "#Access to an undefined property Mockery\\\\LegacyMockInterface::\\$[a-zA-Z0-9_]+\\.#"
|
||||
path: tests/*
|
||||
-
|
||||
message: "#Call to an undefined method Mockery\\\\Mock::[a-zA-Z0-9_]+\\(\\)\\.#"
|
||||
message: "#Call to an undefined method Mockery\\\\(Mock|LegacyMockInterface)::[a-zA-Z0-9_]+\\(\\)\\.#"
|
||||
path: tests/*
|
||||
|
|
|
@ -228,14 +228,9 @@ class Entry {
|
|||
public function php2ldap($attr, $value, $one_value=false) {
|
||||
if (!$this -> ldap || !$this -> ldap -> schema) {
|
||||
// No LDAP schema available
|
||||
if ($one_value)
|
||||
return is_null($value)?null:strval($value);
|
||||
if ($one_value) return is_null($value)?null:strval($value);
|
||||
if (is_null($value)) return array();
|
||||
$value = is_array($value)?$value:array($value);
|
||||
$ldap_values = array();
|
||||
foreach($value as $v)
|
||||
$ldap_values[] = strval($v);
|
||||
return $ldap_values;
|
||||
return array_map('strval', is_array($value)?$value:array($value));
|
||||
}
|
||||
|
||||
// Convert using LDAP schema
|
||||
|
|
|
@ -855,4 +855,410 @@ final class EntryTest extends TestCase {
|
|||
$entry = new Entry($dn);
|
||||
$this->assertEquals("<LDAP Entry \"$dn\">", $entry->__toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \EesyLDAP\Entry::has_attribute
|
||||
*/
|
||||
public function testHasAttribute() {
|
||||
$attr = 'test';
|
||||
$ocs = array('oc1', 'oc2');
|
||||
$schema = Mockery::mock('EesyLDAP\Schema');
|
||||
$schema->shouldReceive('has_attribute')
|
||||
->once()
|
||||
->with($attr, $ocs)
|
||||
->andReturn(true);
|
||||
|
||||
$ldap = $this->createMock(\EesyLDAP\Ldap::class);
|
||||
$ldap->expects($this->atLeastOnce())
|
||||
->method('__get')
|
||||
->with($this->equalTo('schema'))
|
||||
->will($this->returnValue($schema));
|
||||
|
||||
$entry = Mockery::mock('EesyLDAP\Entry');
|
||||
$entry_reflection = new ReflectionClass($entry);
|
||||
$ldap_property = $entry_reflection->getProperty('ldap');
|
||||
$ldap_property->setAccessible(true);
|
||||
$ldap_property->setValue($entry, $ldap);
|
||||
|
||||
$entry->shouldReceive('get_raw_values')
|
||||
->once()
|
||||
->with('objectclass')
|
||||
->andReturn($ocs);
|
||||
$entry->shouldReceive('has_attribute')->passthru();
|
||||
$this->assertTrue($entry->has_attribute($attr));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \EesyLDAP\Entry::has_attribute
|
||||
*/
|
||||
public function testHasAttributeNoObjectclass() {
|
||||
$attr = 'test';
|
||||
$schema = Mockery::mock('EesyLDAP\Schema');
|
||||
$ldap = $this->createMock(\EesyLDAP\Ldap::class);
|
||||
$ldap->expects($this->atLeastOnce())
|
||||
->method('__get')
|
||||
->with($this->equalTo('schema'))
|
||||
->will($this->returnValue($schema));
|
||||
|
||||
$entry = Mockery::mock('EesyLDAP\Entry');
|
||||
$entry_reflection = new ReflectionClass($entry);
|
||||
$ldap_property = $entry_reflection->getProperty('ldap');
|
||||
$ldap_property->setAccessible(true);
|
||||
$ldap_property->setValue($entry, $ldap);
|
||||
|
||||
$entry->shouldReceive('get_raw_values')
|
||||
->once()
|
||||
->with('objectclass')
|
||||
->andReturn(array());
|
||||
$entry->shouldReceive('has_attribute')->passthru();
|
||||
$this->assertFalse($entry->has_attribute($attr));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \EesyLDAP\Entry::get_value
|
||||
* @
|
||||
*/
|
||||
public function testGetValueNoLdap() {
|
||||
$attr_name = 'test';
|
||||
$value = 'TRUE';
|
||||
$default = false;
|
||||
$first = true;
|
||||
$entry = $this->getMockBuilder(Entry::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['get_raw_values'])
|
||||
->getMock();
|
||||
$entry->expects($this->once())
|
||||
->method('get_raw_values')
|
||||
// @phpstan-ignore-next-line
|
||||
->with($this->equalTo($attr_name), $this->equalTo($default), $this->equalTo(!$first))
|
||||
->will($this->returnValue(true));
|
||||
$this->assertTrue($entry->get_value($attr_name, $default, $first));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \EesyLDAP\Entry::get_value
|
||||
*/
|
||||
public function testGetValueWithSchema() {
|
||||
$attr_name = 'test';
|
||||
$value = 'TRUE';
|
||||
$attr = $this->createMock(\EesyLDAP\Schema\Attribute\BooleanAttribute::class);
|
||||
$attr->expects($this->once())
|
||||
->method('ldap2php')
|
||||
->with($this->equalTo(array($value)))
|
||||
->will($this->returnValue(true));
|
||||
$schema = Mockery::mock('EesyLDAP\Schema');
|
||||
$schema->shouldReceive('attribute')
|
||||
->once()
|
||||
->with($attr_name)
|
||||
->andReturn($attr);
|
||||
|
||||
$ldap = $this->createMock(\EesyLDAP\Ldap::class);
|
||||
$ldap->expects($this->atLeastOnce())
|
||||
->method('__get')
|
||||
->with($this->equalTo('schema'))
|
||||
->will($this->returnValue($schema));
|
||||
|
||||
$entry = Mockery::mock('EesyLDAP\Entry')
|
||||
->shouldAllowMockingProtectedMethods();
|
||||
$entry_reflection = new ReflectionClass($entry);
|
||||
$ldap_property = $entry_reflection->getProperty('ldap');
|
||||
$ldap_property->setAccessible(true);
|
||||
$ldap_property->setValue($entry, $ldap);
|
||||
|
||||
$entry->shouldReceive('_get_raw_values')
|
||||
->once()
|
||||
->with($attr_name)
|
||||
->andReturn(array($value));
|
||||
$entry->shouldReceive('get_value')->passthru();
|
||||
|
||||
$this->assertTrue($entry->get_value($attr_name));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \EesyLDAP\Entry::get_value
|
||||
*/
|
||||
public function testGetValueWithSchemaNoValue() {
|
||||
$attr_name = 'test';
|
||||
$attr = $this->createMock(\EesyLDAP\Schema\Attribute\BooleanAttribute::class);
|
||||
$attr->expects($this->atLeastOnce())
|
||||
->method('__get')
|
||||
->with($this->equalTo('single'))
|
||||
->will($this->returnValue(true));
|
||||
$schema = Mockery::mock('EesyLDAP\Schema');
|
||||
$schema->shouldReceive('attribute')
|
||||
->once()
|
||||
->with($attr_name)
|
||||
->andReturn($attr);
|
||||
|
||||
$ldap = $this->createMock(\EesyLDAP\Ldap::class);
|
||||
$ldap->expects($this->atLeastOnce())
|
||||
->method('__get')
|
||||
->with($this->equalTo('schema'))
|
||||
->will($this->returnValue($schema));
|
||||
|
||||
$entry = Mockery::mock('EesyLDAP\Entry')
|
||||
->shouldAllowMockingProtectedMethods();
|
||||
$entry_reflection = new ReflectionClass($entry);
|
||||
$ldap_property = $entry_reflection->getProperty('ldap');
|
||||
$ldap_property->setAccessible(true);
|
||||
$ldap_property->setValue($entry, $ldap);
|
||||
|
||||
$entry->shouldReceive('_get_raw_values')
|
||||
->once()
|
||||
->with($attr_name)
|
||||
->andReturn(array());
|
||||
$entry->shouldReceive('get_value')->passthru();
|
||||
|
||||
$this->assertNull($entry->get_value($attr_name));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \EesyLDAP\Entry::get_value
|
||||
*/
|
||||
public function testGetValueWithSchemaNoValueMultiple() {
|
||||
$attr_name = 'test';
|
||||
$attr = $this->createMock(\EesyLDAP\Schema\Attribute::class);
|
||||
$attr->expects($this->atLeastOnce())
|
||||
->method('__get')
|
||||
->with($this->equalTo('single'))
|
||||
->will($this->returnValue(false));
|
||||
$schema = Mockery::mock('EesyLDAP\Schema');
|
||||
$schema->shouldReceive('attribute')
|
||||
->once()
|
||||
->with($attr_name)
|
||||
->andReturn($attr);
|
||||
|
||||
$ldap = $this->createMock(\EesyLDAP\Ldap::class);
|
||||
$ldap->expects($this->atLeastOnce())
|
||||
->method('__get')
|
||||
->with($this->equalTo('schema'))
|
||||
->will($this->returnValue($schema));
|
||||
|
||||
$entry = Mockery::mock('EesyLDAP\Entry')
|
||||
->shouldAllowMockingProtectedMethods();
|
||||
$entry_reflection = new ReflectionClass($entry);
|
||||
$ldap_property = $entry_reflection->getProperty('ldap');
|
||||
$ldap_property->setAccessible(true);
|
||||
$ldap_property->setValue($entry, $ldap);
|
||||
|
||||
$entry->shouldReceive('_get_raw_values')
|
||||
->once()
|
||||
->with($attr_name)
|
||||
->andReturn(array());
|
||||
$entry->shouldReceive('get_value')->passthru();
|
||||
|
||||
$this->assertEquals(array(), $entry->get_value($attr_name));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \EesyLDAP\Entry::get_value
|
||||
*/
|
||||
public function testGetValueWithSchemaUndefinedAttribute() {
|
||||
$attr_name = 'test';
|
||||
$schema = Mockery::mock('EesyLDAP\Schema');
|
||||
$schema->shouldReceive('attribute')
|
||||
->once()
|
||||
->with($attr_name)
|
||||
->andReturn(false);
|
||||
|
||||
$ldap = $this->createMock(\EesyLDAP\Ldap::class);
|
||||
$ldap->expects($this->atLeastOnce())
|
||||
->method('__get')
|
||||
->with($this->equalTo('schema'))
|
||||
->will($this->returnValue($schema));
|
||||
|
||||
$entry = Mockery::mock('EesyLDAP\Entry')
|
||||
->shouldAllowMockingProtectedMethods();
|
||||
$entry_reflection = new ReflectionClass($entry);
|
||||
$ldap_property = $entry_reflection->getProperty('ldap');
|
||||
$ldap_property->setAccessible(true);
|
||||
$ldap_property->setValue($entry, $ldap);
|
||||
|
||||
$entry->shouldReceive('_get_raw_values')
|
||||
->once()
|
||||
->with($attr_name)
|
||||
->andReturn(array());
|
||||
$entry->shouldReceive('error')
|
||||
->once()
|
||||
->with('Unkown attribute %s in schema', null, $attr_name)
|
||||
->andThrow(LdapException::class);
|
||||
$entry->shouldReceive('get_value')->passthru();
|
||||
|
||||
$this->expectException(LdapException::class);
|
||||
$entry->get_value($attr_name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \EesyLDAP\Entry::php2ldap
|
||||
*/
|
||||
public function testPhp2LdapNoLdap() {
|
||||
$attr_name = 'test';
|
||||
$values = array(1, 2);
|
||||
$entry = Mockery::mock('EesyLDAP\Entry');
|
||||
$entry->shouldReceive('php2ldap')->passthru();
|
||||
$this->assertEquals(array_map('strval', $values), $entry->php2ldap($attr_name, $values));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \EesyLDAP\Entry::php2ldap
|
||||
*/
|
||||
public function testPhp2LdapNoLdapNoValue() {
|
||||
$attr_name = 'test';
|
||||
$entry = Mockery::mock('EesyLDAP\Entry');
|
||||
$entry->shouldReceive('php2ldap')->passthru();
|
||||
$this->assertEquals(array(), $entry->php2ldap($attr_name, null));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \EesyLDAP\Entry::php2ldap
|
||||
*/
|
||||
public function testPhp2LdapNoLdapNotArray() {
|
||||
$attr_name = 'test';
|
||||
$value = 'test';
|
||||
$entry = Mockery::mock('EesyLDAP\Entry');
|
||||
$entry->shouldReceive('php2ldap')->passthru();
|
||||
$this->assertEquals(array($value), $entry->php2ldap($attr_name, $value));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \EesyLDAP\Entry::php2ldap
|
||||
*/
|
||||
public function testPhp2LdapNoLdapOneValue() {
|
||||
$attr_name = 'test';
|
||||
$value = 1;
|
||||
$entry = Mockery::mock('EesyLDAP\Entry');
|
||||
$entry->shouldReceive('php2ldap')->passthru();
|
||||
$this->assertEquals(strval($value), $entry->php2ldap($attr_name, $value, true));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \EesyLDAP\Entry::php2ldap
|
||||
*/
|
||||
public function testPhp2LdapNoLdapOneValueNull() {
|
||||
$attr_name = 'test';
|
||||
$entry = Mockery::mock('EesyLDAP\Entry');
|
||||
$entry->shouldReceive('php2ldap')->passthru();
|
||||
$this->assertNull($entry->php2ldap($attr_name, null, true));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \EesyLDAP\Entry::php2ldap
|
||||
*/
|
||||
public function testPhp2LdapWithSchema() {
|
||||
$attr_name = 'test';
|
||||
$value = array('TRUE');
|
||||
$one_value = false;
|
||||
$attr = $this->createMock(\EesyLDAP\Schema\Attribute\BooleanAttribute::class);
|
||||
$attr->expects($this->once())
|
||||
->method('php2ldap')
|
||||
->with($this->equalTo($value), $this->equalTo($one_value))
|
||||
->will($this->returnValue(true));
|
||||
$schema = Mockery::mock('EesyLDAP\Schema');
|
||||
$schema->shouldReceive('attribute')
|
||||
->once()
|
||||
->with($attr_name)
|
||||
->andReturn($attr);
|
||||
|
||||
$ldap = $this->createMock(\EesyLDAP\Ldap::class);
|
||||
$ldap->expects($this->atLeastOnce())
|
||||
->method('__get')
|
||||
->with($this->equalTo('schema'))
|
||||
->will($this->returnValue($schema));
|
||||
|
||||
$entry = Mockery::mock('EesyLDAP\Entry')
|
||||
->shouldAllowMockingProtectedMethods();
|
||||
$entry_reflection = new ReflectionClass($entry);
|
||||
$ldap_property = $entry_reflection->getProperty('ldap');
|
||||
$ldap_property->setAccessible(true);
|
||||
$ldap_property->setValue($entry, $ldap);
|
||||
|
||||
$entry->shouldReceive('php2ldap')->passthru();
|
||||
|
||||
$this->assertTrue($entry->php2ldap($attr_name, $value, $one_value));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \EesyLDAP\Entry::php2ldap
|
||||
*/
|
||||
public function testPhp2LdapWithSchemaUndefinedAttribute() {
|
||||
$attr_name = 'test';
|
||||
$value = array('TRUE');
|
||||
$schema = Mockery::mock('EesyLDAP\Schema');
|
||||
$schema->shouldReceive('attribute')
|
||||
->once()
|
||||
->with($attr_name)
|
||||
->andReturn(false);
|
||||
|
||||
$ldap = $this->createMock(\EesyLDAP\Ldap::class);
|
||||
$ldap->expects($this->atLeastOnce())
|
||||
->method('__get')
|
||||
->with($this->equalTo('schema'))
|
||||
->will($this->returnValue($schema));
|
||||
|
||||
$entry = Mockery::mock('EesyLDAP\Entry')
|
||||
->shouldAllowMockingProtectedMethods();
|
||||
$entry_reflection = new ReflectionClass($entry);
|
||||
$ldap_property = $entry_reflection->getProperty('ldap');
|
||||
$ldap_property->setAccessible(true);
|
||||
$ldap_property->setValue($entry, $ldap);
|
||||
|
||||
$entry->shouldReceive('error')
|
||||
->once()
|
||||
->with('Unkown attribute %s in schema', null, $attr_name)
|
||||
->andThrow(LdapException::class);
|
||||
$entry->shouldReceive('php2ldap')->passthru();
|
||||
|
||||
$this->expectException(LdapException::class);
|
||||
$entry->php2ldap($attr_name, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \EesyLDAP\Entry::error
|
||||
*/
|
||||
public function testError() {
|
||||
$entry = Mockery::mock('EesyLDAP\Entry')
|
||||
->shouldAllowMockingProtectedMethods();
|
||||
$entry->shouldReceive('error')->passthru();
|
||||
$this->expectException(LdapException::class);
|
||||
$entry->error('test');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \EesyLDAP\Entry::error
|
||||
*/
|
||||
public function testErrorWithArgs() {
|
||||
$error = 'test %s:%s';
|
||||
$arg1 = 'arg1';
|
||||
$arg2 = 'arg2';
|
||||
$entry = Mockery::mock('EesyLDAP\Entry')
|
||||
->shouldAllowMockingProtectedMethods();
|
||||
$entry->shouldReceive('error')->passthru();
|
||||
$this->expectException(LdapException::class);
|
||||
$this->expectExceptionMessage(sprintf($error, $arg1, $arg2));
|
||||
$entry->error($error, $arg1, $arg2);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \EesyLDAP\Entry::error
|
||||
*/
|
||||
public function testErrorWithLdap() {
|
||||
$error = 'test';
|
||||
$ldap = $this->createMock(\EesyLDAP\Ldap::class);
|
||||
$ldap->expects($this->once())
|
||||
->method('error')
|
||||
->with($this->equalTo($error))
|
||||
->will($this->throwException(new LdapException));
|
||||
|
||||
$entry = Mockery::mock('EesyLDAP\Entry')
|
||||
->shouldAllowMockingProtectedMethods();
|
||||
$entry_reflection = new ReflectionClass($entry);
|
||||
$ldap_property = $entry_reflection->getProperty('ldap');
|
||||
$ldap_property->setAccessible(true);
|
||||
$ldap_property->setValue($entry, $ldap);
|
||||
|
||||
$entry->shouldReceive('error')->passthru();
|
||||
|
||||
$this->expectException(LdapException::class);
|
||||
$entry->error($error);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue