|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Ldap\Tests; |
| 13 | + |
| 14 | +use Symfony\Component\Ldap\Adapter\ExtLdap\Adapter; |
| 15 | +use Symfony\Component\Ldap\Adapter\ExtLdap\Collection; |
| 16 | +use Symfony\Component\Ldap\Entry; |
| 17 | +use Symfony\Component\Ldap\Exception\LdapException; |
| 18 | + |
| 19 | +/** |
| 20 | + * @requires extension ldap |
| 21 | + */ |
| 22 | +class LdapManagerTest extends \PHPUnit_Framework_TestCase |
| 23 | +{ |
| 24 | + /** @var Adapter */ |
| 25 | + private $adapter; |
| 26 | + |
| 27 | + protected function setUp() |
| 28 | + { |
| 29 | + $this->adapter = new Adapter(array('host' => 'localhost', 'port' => 3389)); |
| 30 | + $this->adapter->getConnection()->bind('cn=admin,dc=symfony,dc=com', 'symfony'); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @group functional |
| 35 | + */ |
| 36 | + public function testLdapAddAndRemove() |
| 37 | + { |
| 38 | + $this->executeSearchQuery(1); |
| 39 | + |
| 40 | + $entry = new Entry('cn=Charles Sarrazin,dc=symfony,dc=com', array( |
| 41 | + 'sn' => array('csarrazi'), |
| 42 | + 'objectclass' => array( |
| 43 | + 'inetOrgPerson', |
| 44 | + ), |
| 45 | + )); |
| 46 | + |
| 47 | + $em = $this->adapter->getEntryManager(); |
| 48 | + $em->add($entry); |
| 49 | + |
| 50 | + $this->executeSearchQuery(2); |
| 51 | + |
| 52 | + $em->remove($entry); |
| 53 | + $this->executeSearchQuery(1); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @group functional |
| 58 | + */ |
| 59 | + public function testLdapAddInvalidEntry() |
| 60 | + { |
| 61 | + $this->setExpectedException(LdapException::class); |
| 62 | + $this->executeSearchQuery(1); |
| 63 | + |
| 64 | + // The entry is missing a subject name |
| 65 | + $entry = new Entry('cn=Charles Sarrazin,dc=symfony,dc=com', array( |
| 66 | + 'objectclass' => array( |
| 67 | + 'inetOrgPerson', |
| 68 | + ), |
| 69 | + )); |
| 70 | + |
| 71 | + $em = $this->adapter->getEntryManager(); |
| 72 | + $em->add($entry); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @group functional |
| 77 | + */ |
| 78 | + public function testLdapUpdate() |
| 79 | + { |
| 80 | + $result = $this->executeSearchQuery(1); |
| 81 | + |
| 82 | + $entry = $result[0]; |
| 83 | + $this->assertNull($entry->getAttribute('email')); |
| 84 | + |
| 85 | + $em = $this->adapter->getEntryManager(); |
| 86 | + $em->update($entry); |
| 87 | + |
| 88 | + $result = $this->executeSearchQuery(1); |
| 89 | + |
| 90 | + $entry = $result[0]; |
| 91 | + $this->assertNull($entry->getAttribute('email')); |
| 92 | + |
| 93 | + $entry->removeAttribute('email'); |
| 94 | + $em->update($entry); |
| 95 | + |
| 96 | + $result = $this->executeSearchQuery(1); |
| 97 | + $entry = $result[0]; |
| 98 | + $this->assertNull($entry->getAttribute('email')); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @return Collection|Entry[] |
| 103 | + */ |
| 104 | + private function executeSearchQuery($expectedResults = 1) |
| 105 | + { |
| 106 | + $results = $this |
| 107 | + ->adapter |
| 108 | + ->createQuery('dc=symfony,dc=com', '(objectclass=person)') |
| 109 | + ->execute() |
| 110 | + ; |
| 111 | + |
| 112 | + $this->assertCount($expectedResults, $results); |
| 113 | + |
| 114 | + return $results; |
| 115 | + } |
| 116 | +} |
0 commit comments