public function decryption_throws_exception_if_creation_has_failed() { $called = false; Secret::setBacking(function ($value) { throw new XPException('Something went wrong - intentionally.'); }, function ($value) { return null; }); // Creation may never throw exception try { $s = new Secret('foo'); } catch (\Throwable $t) { $this->fail('Exception thrown where no exception may be thrown', $t, null); } // Buf if creation failed, an exception must be raised here: $s->reveal(); }
/** * Connect to the LDAP server. Optionally takes DN and password which * overwrite any credentials given in the connection DSN. * * @param string $dn * @param util.Secret $password * @return self $this * @throws peer.ConnectException */ public function connect($dn = null, Secret $password = null) { static $ports = ['ldap' => 389, 'ldaps' => 636]; if ($this->isConnected()) { return true; } $uri = sprintf('%s://%s:%d', $this->url->getScheme(), $this->url->getHost(), $this->url->getPort($ports[$this->url->getScheme()])); if (false === ($this->handle = ldap_connect($uri))) { throw new ConnectException('Cannot connect to ' . $uri); } foreach (array_merge(['protocol_version' => 3], $this->url->getParams()) as $option => $value) { $set = self::$options[$option]; if (!$set($this->handle, $value)) { ldap_unbind($this->handle); $this->handle = null; throw new LDAPException('Cannot set option "' . $option . '"', ldap_errno($this->handle)); } } if (null === $dn) { $result = ldap_bind($this->handle, $this->url->getUser(null), $this->url->getPassword(null)); } else { $result = ldap_bind($this->handle, $dn, $password->reveal()); } if (false === $result) { $error = ldap_errno($this->handle); ldap_unbind($this->handle); $this->handle = null; if (LDAP_SERVER_DOWN === $error || -1 === $error) { throw new ConnectException('Cannot connect to ' . $uri); } else { throw new LDAPException('Cannot bind for "' . ($user ?: $this->url->getUser(null)) . '"', $error); } } return $this; }