diff --git a/.styleci.yml b/.styleci.yml index 9f5e38cf..c9970367 100644 --- a/.styleci.yml +++ b/.styleci.yml @@ -1,8 +1,3 @@ preset: laravel enabled: - - phpdoc_align - - phpdoc_separation - - unalign_double_arrow -disabled: - - laravel_phpdoc_alignment - - laravel_phpdoc_separation + - no_superfluous_phpdoc_tags diff --git a/composer.json b/composer.json index e3f80314..e95d3c68 100644 --- a/composer.json +++ b/composer.json @@ -10,17 +10,17 @@ "license": "MIT", "type": "project", "require": { - "php": ">=7.3", + "php": ">=8.1", "ext-ldap": "*", "ext-json": "*", "ramsey/uuid": "*", - "directorytree/ldaprecord": "^2.4.4", - "illuminate/support": "^5.6|^6.0|^7.0|^8.0|^9.0|^10.0" + "directorytree/ldaprecord": "v3.0.x-dev", + "illuminate/support": "^8.0|^9.0|^10.0" }, "require-dev": { "mockery/mockery": "^1.0", - "phpunit/phpunit": "^7.0|^8.0|^9.0", - "orchestra/testbench": "^3.7|^4.0|^5.0|^6.0|^7.0", + "phpunit/phpunit": "^8.0|^9.0", + "orchestra/testbench": "^6.0|^7.0|^8.0", "spatie/ray": "^1.28", "laravel/sanctum": "*" }, diff --git a/src/Auth/ListensForLdapBindFailure.php b/src/Auth/ListensForLdapBindFailure.php index 3b0f4190..a09edbf7 100644 --- a/src/Auth/ListensForLdapBindFailure.php +++ b/src/Auth/ListensForLdapBindFailure.php @@ -39,7 +39,7 @@ public static function setErrorHandler(Closure $callback) */ public function listenForLdapBindFailure() { - $dispatcher = Container::getInstance()->getEventDispatcher(); + $dispatcher = Container::getDispatcher(); $isOnLastHost = true; diff --git a/src/DetectsSoftDeletes.php b/src/DetectsSoftDeletes.php index 74e95d21..8aa97a9d 100644 --- a/src/DetectsSoftDeletes.php +++ b/src/DetectsSoftDeletes.php @@ -8,12 +8,8 @@ trait DetectsSoftDeletes { /** * Determine if the model is using soft-deletes. - * - * @param Model $model - * - * @return bool */ - protected function isUsingSoftDeletes(Model $model) + protected function isUsingSoftDeletes(Model $model): bool { return method_exists($model, 'trashed'); } diff --git a/src/ImportableFromLdap.php b/src/ImportableFromLdap.php index 05bd195e..c6cb130a 100644 --- a/src/ImportableFromLdap.php +++ b/src/ImportableFromLdap.php @@ -6,64 +6,48 @@ trait ImportableFromLdap { /** * Get the database column name of the domain. - * - * @return string */ - public function getLdapDomainColumn() + public function getLdapDomainColumn(): string { return 'domain'; } /** * Get the models LDAP domain. - * - * @return string */ - public function getLdapDomain() + public function getLdapDomain(): ?string { return $this->{$this->getLdapDomainColumn()}; } /** * Set the models LDAP domain. - * - * @param string $domain - * - * @return void */ - public function setLdapDomain($domain) + public function setLdapDomain(?string $domain): void { $this->{$this->getLdapDomainColumn()} = $domain; } /** * Get the models LDAP GUID database column name. - * - * @return string */ - public function getLdapGuidColumn() + public function getLdapGuidColumn(): string { return 'guid'; } /** * Get the models LDAP GUID. - * - * @return string */ - public function getLdapGuid() + public function getLdapGuid(): ?string { return $this->{$this->getLdapGuidColumn()}; } /** * Set the models LDAP GUID. - * - * @param string $guid - * - * @return void */ - public function setLdapGuid($guid) + public function setLdapGuid(?string $guid): void { $this->{$this->getLdapGuidColumn()} = $guid; } diff --git a/src/LdapAuthServiceProvider.php b/src/LdapAuthServiceProvider.php index 8ac0bc22..08ccf08c 100644 --- a/src/LdapAuthServiceProvider.php +++ b/src/LdapAuthServiceProvider.php @@ -15,10 +15,8 @@ class LdapAuthServiceProvider extends ServiceProvider { /** * Run service provider boot operations. - * - * @return void */ - public function boot() + public function boot(): void { $this->loadTranslationsFrom(__DIR__.'/../resources/lang/', 'ldap'); @@ -30,20 +28,16 @@ public function boot() /** * Register the LDAP auth commands. - * - * @return void */ - protected function registerCommands() + protected function registerCommands(): void { $this->commands([ImportLdapUsers::class]); } /** * Register the LDAP auth migrations. - * - * @return void */ - protected function registerMigrations() + protected function registerMigrations(): void { if (! $this->app->runningInConsole()) { return; @@ -58,10 +52,8 @@ protected function registerMigrations() /** * Register the LDAP auth provider. - * - * @return void */ - protected function registerAuthProvider() + protected function registerAuthProvider(): void { Auth::provider('ldap', function ($app, array $config) { return array_key_exists('database', $config) @@ -72,10 +64,8 @@ protected function registerAuthProvider() /** * Registers the login controller listener to handle LDAP errors. - * - * @return void */ - protected function registerLoginControllerListeners() + protected function registerLoginControllerListeners(): void { BindFailureListener::usingLaravelUi(); BindFailureListener::usingLaravelJetstream(); @@ -83,12 +73,8 @@ protected function registerLoginControllerListeners() /** * Get a new database user provider. - * - * @param array $config - * - * @return DatabaseUserProvider */ - protected function makeDatabaseUserProvider(array $config) + protected function makeDatabaseUserProvider(array $config): DatabaseUserProvider { return app(DatabaseUserProvider::class, [ 'users' => $this->makeLdapUserRepository($config), @@ -100,12 +86,8 @@ protected function makeDatabaseUserProvider(array $config) /** * Make a new plain LDAP user provider. - * - * @param array $config - * - * @return NoDatabaseUserProvider */ - protected function makePlainUserProvider(array $config) + protected function makePlainUserProvider(array $config): NoDatabaseUserProvider { return app(NoDatabaseUserProvider::class, [ 'users' => $this->makeLdapUserRepository($config), @@ -115,12 +97,8 @@ protected function makePlainUserProvider(array $config) /** * Make a new Eloquent user provider. - * - * @param array $config - * - * @return EloquentUserProvider */ - protected function makeEloquentUserProvider($config) + protected function makeEloquentUserProvider(array $config): EloquentUserProvider { return app(EloquentUserProvider::class, [ 'hasher' => $this->app->make('hash'), @@ -130,36 +108,25 @@ protected function makeEloquentUserProvider($config) /** * Make a new LDAP user authenticator. - * - * @param array $config - * - * @return LdapUserAuthenticator */ - protected function makeLdapUserAuthenticator(array $config) + protected function makeLdapUserAuthenticator(array $config): LdapUserAuthenticator { return app(LdapUserAuthenticator::class, ['rules' => $config['rules'] ?? []]); } /** * Make a new LDAP user repository. - * - * @param array $config - * - * @return LdapUserRepository */ - protected function makeLdapUserRepository(array $config) + protected function makeLdapUserRepository(array $config): LdapUserRepository { + return app(LdapUserRepository::class, ['model' => $config['model']]); } /** * Make a new LDAP user importer. - * - * @param array $config - * - * @return UserSynchronizer */ - protected function makeLdapUserSynchronizer(array $config) + protected function makeLdapUserSynchronizer(array $config): UserSynchronizer { return app(UserSynchronizer::class, [ 'eloquentModel' => $config['model'], diff --git a/src/LdapImportable.php b/src/LdapImportable.php index ef3e3fa6..0975a02b 100644 --- a/src/LdapImportable.php +++ b/src/LdapImportable.php @@ -6,47 +6,31 @@ interface LdapImportable { /** * Get the database column name for the LDAP domain. - * - * @return string */ - public function getLdapDomainColumn(); + public function getLdapDomainColumn(): string; /** * Get the models LDAP domain. - * - * @return string */ - public function getLdapDomain(); + public function getLdapDomain(): ?string; /** * Set the models LDAP domain. - * - * @param string $domain - * - * @return void */ - public function setLdapDomain($domain); + public function setLdapDomain(?string $domain): void; /** * Get the database column name for the LDAP guid. - * - * @return string */ - public function getLdapGuidColumn(); + public function getLdapGuidColumn(): string; /** * Get the models LDAP GUID. - * - * @return string */ - public function getLdapGuid(); + public function getLdapGuid(): ?string; /** * Set the models LDAP GUID. - * - * @param string $guid - * - * @return void */ - public function setLdapGuid($guid); + public function setLdapGuid(?string $guid): void; } diff --git a/src/LdapRecord.php b/src/LdapRecord.php index 36fa2959..6a5e0d8d 100644 --- a/src/LdapRecord.php +++ b/src/LdapRecord.php @@ -11,7 +11,7 @@ class LdapRecord * * @var bool */ - public static $failingQuietly = true; + public static bool $failingQuietly = true; /** * Don't catch exceptions during authentication. diff --git a/src/LdapServiceProvider.php b/src/LdapServiceProvider.php index 4272c9ec..fdc12346 100644 --- a/src/LdapServiceProvider.php +++ b/src/LdapServiceProvider.php @@ -22,10 +22,8 @@ class LdapServiceProvider extends ServiceProvider { /** * Run service provider boot operations. - * - * @return void */ - public function boot() + public function boot(): void { $this->loadEnvironmentConnections(); @@ -42,10 +40,8 @@ public function boot() /** * Register the publishable LDAP configuration file. - * - * @return void */ - protected function registerConfiguration() + protected function registerConfiguration(): void { if ($this->app->runningInConsole()) { $this->publishes([ @@ -56,10 +52,8 @@ protected function registerConfiguration() /** * Register the LDAP artisan commands. - * - * @return void */ - protected function registerCommands() + protected function registerCommands(): void { $this->commands([ GetRootDse::class, @@ -73,10 +67,8 @@ protected function registerCommands() /** * Register the LDAP operation logger. - * - * @return void */ - protected function registerLogging() + protected function registerLogging(): void { if (! Config::get('ldap.logging', false)) { return; @@ -96,10 +88,8 @@ protected function registerLogging() /** * Register the application's LDAP connections. - * - * @return void */ - protected function registerLdapConnections() + protected function registerLdapConnections(): void { Container::setDefaultConnection( Config::get('ldap.default', 'default') @@ -110,10 +100,8 @@ protected function registerLdapConnections() /** * Register the connections that exist in the configuration file. - * - * @return void */ - protected function registerConfiguredConnections() + protected function registerConfiguredConnections(): void { foreach (Config::get('ldap.connections', []) as $name => $config) { $connection = $this->makeConnection($config); @@ -128,10 +116,8 @@ protected function registerConfiguredConnections() /** * Registers the LDAP event listeners. - * - * @return void */ - protected function registerEventListeners() + protected function registerEventListeners(): void { Event::listen('LdapRecord\Laravel\Events\*', function ($eventName, array $events) { collect($events)->filter(function ($event) { @@ -144,10 +130,8 @@ protected function registerEventListeners() /** * Register the connections that exist in the environment file. - * - * @return void */ - protected function loadEnvironmentConnections() + protected function loadEnvironmentConnections(): void { $connections = array_filter( array_map('trim', explode(',', env('LDAP_CONNECTIONS', ''))) @@ -169,24 +153,16 @@ protected function loadEnvironmentConnections() /** * Make a new LDAP connection. - * - * @param array $config - * - * @return Connection */ - protected function makeConnection($config) + protected function makeConnection(array $config): Connection { return app(Connection::class, ['config' => $config]); } /** * Register the LDAP cache store on the given connection. - * - * @param Connection $connection - * - * @return void */ - protected function registerLdapCache(Connection $connection) + protected function registerLdapCache(Connection $connection): void { if (! is_null($cache = Cache::getFacadeRoot())) { $connection->setCache( @@ -197,12 +173,8 @@ protected function registerLdapCache(Connection $connection) /** * Make a connection's configuration from the connection's environment name. - * - * @param string $connection - * - * @return array */ - protected function makeConnectionConfigFromEnv($connection) + protected function makeConnectionConfigFromEnv(string $connection): array { return array_filter([ 'hosts' => explode(',', env($this->makeEnvVariable('LDAP_{name}_HOSTS', $connection))), @@ -219,12 +191,8 @@ protected function makeConnectionConfigFromEnv($connection) /** * Make a connection's custom config options array from the env. - * - * @param string $connection - * - * @return array */ - protected function makeCustomOptionsFromEnv($connection) + protected function makeCustomOptionsFromEnv(string $connection): array { $constant = $this->makeEnvVariable('LDAP_{name}_OPT', $connection); @@ -249,13 +217,8 @@ protected function makeCustomOptionsFromEnv($connection) /** * Substitute the env name template with the given connection name. - * - * @param string $env - * @param string $name - * - * @return string */ - protected function makeEnvVariable($env, $name) + protected function makeEnvVariable(string $env, string $name): string { return str_replace('{name}', strtoupper($name), $env); } diff --git a/src/LdapUserAuthenticator.php b/src/LdapUserAuthenticator.php index b21205c3..524e5532 100644 --- a/src/LdapUserAuthenticator.php +++ b/src/LdapUserAuthenticator.php @@ -10,6 +10,7 @@ use LdapRecord\Laravel\Events\Auth\EloquentUserTrashed; use LdapRecord\Laravel\Events\Auth\Rejected; use LdapRecord\Models\Model; +use Illuminate\Database\Eloquent\Model as Eloquent; class LdapUserAuthenticator { @@ -20,21 +21,19 @@ class LdapUserAuthenticator * * @var array */ - protected $rules = []; + protected array $rules = []; /** * The eloquent user model. - * - * @var \Illuminate\Database\Eloquent\Model|null */ - protected $eloquentModel; + protected ?Eloquent $eloquentModel = null; /** * The authenticator to use for validating the users password. * * @var Closure */ - protected $authenticator; + protected Closure $authenticator; /** * Constructor. diff --git a/src/Testing/DirectoryEmulator.php b/src/Testing/DirectoryEmulator.php index 8f8bd2dc..e98f0c04 100644 --- a/src/Testing/DirectoryEmulator.php +++ b/src/Testing/DirectoryEmulator.php @@ -2,6 +2,7 @@ namespace LdapRecord\Laravel\Testing; +use LdapRecord\Testing\ConnectionFake; use LdapRecord\Testing\DirectoryFake; class DirectoryEmulator extends DirectoryFake @@ -16,7 +17,7 @@ class DirectoryEmulator extends DirectoryFake * * @throws \LdapRecord\ContainerException */ - public static function setup($name = null, array $config = []) + public static function setup(string $name = null, array $config = []): ConnectionFake { return tap(parent::setup($name), function (EmulatedConnectionFake $fake) use ($name, $config) { $fake->name($name); @@ -32,7 +33,7 @@ public static function setup($name = null, array $config = []) * * @return EmulatedConnectionFake */ - public static function makeConnectionFake(array $config = []) + public static function makeConnectionFake(array $config = []): EmulatedConnectionFake { return EmulatedConnectionFake::make($config)->shouldBeConnected(); } @@ -42,7 +43,7 @@ public static function makeConnectionFake(array $config = []) * * @return void */ - public static function tearDown() + public static function tearDown(): void { parent::tearDown(); diff --git a/src/Testing/Emulated/EmulatesModelQueries.php b/src/Testing/Emulated/EmulatesModelQueries.php index 762bb0b9..535d1b49 100644 --- a/src/Testing/Emulated/EmulatesModelQueries.php +++ b/src/Testing/Emulated/EmulatesModelQueries.php @@ -4,6 +4,7 @@ use Illuminate\Support\Arr; use LdapRecord\Laravel\Testing\EmulatesQueries; +use LdapRecord\Models\Collection; trait EmulatesModelQueries { @@ -14,7 +15,7 @@ trait EmulatesModelQueries /** * @inheritdoc */ - public function newInstance($baseDn = null) + public function newInstance(string $baseDn = null): static { return (new self($this->connection)) ->setModel($this->model) @@ -24,7 +25,7 @@ public function newInstance($baseDn = null) /** * @inheritdoc */ - public function insertAttributes($dn, array $attributes) + public function insertAttributes(string $dn, array $attributes): bool { if (! $model = $this->find($dn)) { return false; @@ -34,13 +35,15 @@ public function insertAttributes($dn, array $attributes) $model->{$name} = array_merge($model->{$name} ?? [], Arr::wrap($values)); } - return $model->save(); + $model->save(); + + return true; } /** * @inheritdoc */ - public function update($dn, array $modifications) + public function update(string $dn, array $modifications): bool { if (! $model = $this->findEloquentModelByDn($dn)) { return false; @@ -56,7 +59,7 @@ public function update($dn, array $modifications) /** * @inheritdoc */ - public function updateAttributes($dn, array $attributes) + public function updateAttributes(string $dn, array $attributes): bool { if (! $model = $this->find($dn)) { return false; @@ -66,13 +69,15 @@ public function updateAttributes($dn, array $attributes) $model->{$name} = $values; } - return $model->save(); + $model->save(); + + return true; } /** * @inheritdoc */ - public function deleteAttributes($dn, array $attributes) + public function deleteAttributes(string $dn, array $attributes): bool { if (! $model = $this->find($dn)) { return false; @@ -88,7 +93,9 @@ public function deleteAttributes($dn, array $attributes) } } - return $model->save(); + $model->save(); + + return true; } /** @@ -98,7 +105,7 @@ public function deleteAttributes($dn, array $attributes) * * @return array */ - public function parse($resource) + public function parse(mixed $resource): array { return $resource->toArray(); } @@ -106,7 +113,7 @@ public function parse($resource) /** * @inheritdoc */ - protected function process(array $results) + protected function process(array $results): Collection { return $this->model->newCollection($results)->transform(function ($result) { return $this->resultToModelInstance($result); @@ -153,10 +160,8 @@ protected function addFilterToDatabaseQuery($query, $field, $operator, $value) /** * Override the possibility of the underlying LDAP model being compatible with ANR. - * - * @return false */ - protected function modelIsCompatibleWithAnr() + protected function modelIsCompatibleWithAnr(): bool { return true; } diff --git a/src/Testing/EmulatedBuilder.php b/src/Testing/EmulatedBuilder.php index e0230bb4..9f7ab738 100644 --- a/src/Testing/EmulatedBuilder.php +++ b/src/Testing/EmulatedBuilder.php @@ -7,6 +7,7 @@ use LdapRecord\Models\Types\ActiveDirectory; use LdapRecord\Models\Types\OpenLDAP; use LdapRecord\Query\Builder; +use LdapRecord\Query\Model\Builder as ModelBuilder; class EmulatedBuilder extends Builder { @@ -19,7 +20,7 @@ class EmulatedBuilder extends Builder * * @return mixed */ - public function model(Model $model) + public function model(Model $model): ModelBuilder { $builder = $this->determineBuilderFromModel($model); @@ -54,7 +55,7 @@ protected function determineBuilderFromModel(Model $model) * * @return array */ - protected function process($results) + protected function process($results): array { return array_map([$this, 'mergeAttributesAndTransformResult'], $results); } diff --git a/src/Testing/EmulatedConnectionFake.php b/src/Testing/EmulatedConnectionFake.php index bedad83d..3c75042a 100644 --- a/src/Testing/EmulatedConnectionFake.php +++ b/src/Testing/EmulatedConnectionFake.php @@ -38,7 +38,7 @@ public function name($name = null) * * @throws \LdapRecord\Configuration\ConfigurationException */ - public function query() + public function query(): EmulatedBuilder { return (new EmulatedBuilder($this)) ->setBaseDn($this->configuration->get('base_dn')); diff --git a/src/Testing/EmulatesQueries.php b/src/Testing/EmulatesQueries.php index c6110878..affb8407 100644 --- a/src/Testing/EmulatesQueries.php +++ b/src/Testing/EmulatesQueries.php @@ -10,6 +10,7 @@ use LdapRecord\Models\Attributes\DistinguishedName; use LdapRecord\Models\Attributes\Guid; use LdapRecord\Models\BatchModification; +use LdapRecord\Models\Model as LdapRecord; use LdapRecord\Query\Collection; use LdapRecord\Query\Model\Builder; use Ramsey\Uuid\Uuid; @@ -103,7 +104,7 @@ public function getEloquentQuery() * * @return Builder */ - public function newNestedInstance(Closure $closure = null, $state = 'and') + public function newNestedInstance(Closure $closure = null, string $state = 'and'): static { $query = $this->newInstance()->nested()->setNestedQueryState($state); @@ -124,7 +125,7 @@ public function newNestedInstance(Closure $closure = null, $state = 'and') /** * @inheritdoc */ - public function clearFilters() + public function clearFilters(): static { // When clear filters is called, we must clear the // current Eloquent query instance with it to @@ -151,7 +152,7 @@ public function setNestedQueryState($state) /** * @inheritdoc */ - public function orFilter(Closure $closure) + public function orFilter(Closure $closure): static { $query = $this->newNestedInstance($closure, 'or'); @@ -187,7 +188,7 @@ public function findEloquentModelByGuid($guid) /** * @inheritdoc */ - public function addFilter($type, array $bindings) + public function addFilter($type, array $bindings): static { $relationMethod = $this->determineRelationMethod($type, $bindings); @@ -375,10 +376,10 @@ protected function applyBatchModificationToModel($model, array $modification) /** * @inheritdoc */ - public function findOrFail($dn, $columns = ['*']) + public function findOrFail($dn, $columns = ['*']): LdapRecord|array { if (! $database = $this->findEloquentModelByDn($dn)) { - return; + $this->throwNotFoundException($this->getUnescapedQuery(), $dn); } return $this->getFirstRecordFromResult( @@ -389,10 +390,10 @@ public function findOrFail($dn, $columns = ['*']) /** * @inheritdoc */ - public function findByGuidOrFail($guid, $columns = ['*']) + public function findByGuidOrFail(string $guid, array|string $columns = ['*']): LdapRecord { if (! $database = $this->findEloquentModelByGuid($guid)) { - return; + $this->throwNotFoundException($this->getUnescapedQuery(), $this->dn); } return $this->getFirstRecordFromResult( @@ -427,7 +428,7 @@ protected function getFirstRecordFromResult($result) /** * @inheritdoc */ - public function insert($dn, array $attributes) + public function insert($dn, array $attributes): bool { if (! Arr::get($attributes, 'objectclass')) { throw new Exception('LDAP objects must have object classes to be created.'); @@ -483,7 +484,7 @@ protected function applyObjectAttributesToEloquent(LdapObject $model, $dn, $attr /** * @inheritdoc */ - public function updateAttributes($dn, array $attributes) + public function updateAttributes($dn, array $attributes): bool { if (! $model = $this->findEloquentModelByDn($dn)) { return false; @@ -582,7 +583,7 @@ protected function attributeValueIsGuid($value) /** * @inheritdoc */ - public function rename($dn, $rdn, $newParentDn, $deleteOldRdn = true) + public function rename($dn, $rdn, $newParentDn, $deleteOldRdn = true): bool { $database = $this->findEloquentModelByDn($dn); @@ -600,7 +601,7 @@ public function rename($dn, $rdn, $newParentDn, $deleteOldRdn = true) /** * @inheritdoc */ - public function delete($dn) + public function delete($dn): bool { if (! $database = $this->findEloquentModelByDn($dn)) { return false; @@ -612,7 +613,7 @@ public function delete($dn) /** * @inheritdoc */ - public function escape($value, $ignore = '', $flags = 0) + public function escape(mixed $value = null, string $ignore = '', int $flags = 0): UnescapedValue { return new UnescapedValue($value); } @@ -620,7 +621,7 @@ public function escape($value, $ignore = '', $flags = 0) /** * @inheritdoc */ - public function paginate($pageSize = 1000, $isCritical = false) + public function paginate(int $pageSize = 1000, bool $isCritical = false): Collection|array { return $this->get(); } @@ -628,7 +629,7 @@ public function paginate($pageSize = 1000, $isCritical = false) /** * @inheritdoc */ - public function run($query) + public function run(string $filter): mixed { if ($this->limit > 0) { $this->query->limit($this->limit); @@ -643,7 +644,7 @@ public function run($query) // Emulate performing a single "read" operation. $this->query->where('dn', '=', $this->dn); break; - case 'listing': + case 'list': // Emulate performing a directory "listing" operation. $this->query->where('parent_dn', '=', $this->dn); break; @@ -663,7 +664,7 @@ public function run($query) * * @return array */ - public function parse($resource) + public function parse(mixed $resource): array { return $resource->toArray(); } diff --git a/src/Testing/UnescapedValue.php b/src/Testing/UnescapedValue.php index 7cfb2a22..221afda3 100644 --- a/src/Testing/UnescapedValue.php +++ b/src/Testing/UnescapedValue.php @@ -9,17 +9,17 @@ class UnescapedValue extends EscapedValue /** * @inheritdoc */ - public function __toString() + public function __toString(): string { - return (string) $this->get(); + return $this->get(); } /** * @inheritdoc */ - public function get() + public function get(): string { // Don't escape values. - return $this->value; + return (string) $this->value; } } diff --git a/tests/Feature/Commands/GetRootDseTest.php b/tests/Feature/Commands/GetRootDseTest.php index d4dc0e49..34dc4b07 100644 --- a/tests/Feature/Commands/GetRootDseTest.php +++ b/tests/Feature/Commands/GetRootDseTest.php @@ -87,8 +87,8 @@ public function test_command_displays_no_attributes_error_when_rootdse_is_empty_ class RootDse extends Entry { - public function getCreatableDn($name = null, $attribute = null) + public function getCreatableDn(?string $name = null, ?string $attribute = null): string { - return null; + return ''; } } diff --git a/tests/Feature/Commands/ImportLdapUsersTest.php b/tests/Feature/Commands/ImportLdapUsersTest.php index cc733472..d74f56f7 100644 --- a/tests/Feature/Commands/ImportLdapUsersTest.php +++ b/tests/Feature/Commands/ImportLdapUsersTest.php @@ -40,8 +40,7 @@ public function test_message_is_shown_when_no_users_are_found_for_importing() $repo = m::mock(LdapUserRepository::class, function ($repo) { $query = m::mock(Builder::class); - $query->shouldReceive('paginate')->once()->andReturnSelf(); - $query->shouldReceive('count')->once()->andReturn(0); + $query->shouldReceive('paginate')->once()->andReturn(new Collection()); $repo->shouldReceive('query')->once()->andReturn($query); }); diff --git a/tests/Feature/Commands/TestLdapConnectionTest.php b/tests/Feature/Commands/TestLdapConnectionTest.php index 8d0d7e2d..728f5ef7 100644 --- a/tests/Feature/Commands/TestLdapConnectionTest.php +++ b/tests/Feature/Commands/TestLdapConnectionTest.php @@ -17,7 +17,7 @@ public function test_command_tests_ldap_connectivity() { $connection = m::mock(Connection::class); - $connection->shouldReceive('setDispatcher')->once()->with(DispatcherInterface::class)->andReturnNull(); + $connection->shouldReceive('setDispatcher')->once()->with(DispatcherInterface::class)->andReturn($connection); Container::addConnection($connection); diff --git a/tests/Feature/Emulator/EmulatedImportTest.php b/tests/Feature/Emulator/EmulatedImportTest.php index c36e2972..58840728 100644 --- a/tests/Feature/Emulator/EmulatedImportTest.php +++ b/tests/Feature/Emulator/EmulatedImportTest.php @@ -85,7 +85,7 @@ public function test_disabled_users_are_soft_deleted_when_flag_is_set() 'cn' => $this->faker->name, 'mail' => $this->faker->email, 'objectguid' => $this->faker->uuid, - 'userAccountControl' => (new AccountControl)->accountIsDisabled(), + 'userAccountControl' => (new AccountControl)->setAccountIsDisabled(), ]); $this->artisan('ldap:import', [ @@ -105,7 +105,7 @@ public function test_enabled_users_who_are_soft_deleted_are_restored_when_flag_i 'cn' => $this->faker->name, 'mail' => $this->faker->email, 'objectguid' => $this->faker->uuid, - 'userAccountControl' => (new AccountControl)->accountIsNormal(), + 'userAccountControl' => (new AccountControl)->setAccountIsNormal(), ]); $database = TestUserModelStub::create([ @@ -350,8 +350,8 @@ public function test_scopes_can_be_applied_to_command() class TestImportUserModelScope implements Scope { - public function apply(Builder $query, Model $model) + public function apply(Builder $query, Model $model): void { - return $query->where('cn', 'Bar'); + $query->where('cn', 'Bar'); } } diff --git a/tests/Feature/Emulator/EmulatedModelQueryTest.php b/tests/Feature/Emulator/EmulatedModelQueryTest.php index d07c4fe4..f9bc97cd 100644 --- a/tests/Feature/Emulator/EmulatedModelQueryTest.php +++ b/tests/Feature/Emulator/EmulatedModelQueryTest.php @@ -9,6 +9,7 @@ use LdapRecord\Models\ActiveDirectory\Group; use LdapRecord\Models\ActiveDirectory\User; use LdapRecord\Models\Entry; +use LdapRecord\Models\Relations\HasManyIn; use Ramsey\Uuid\Uuid; class EmulatedModelQueryTest extends TestCase @@ -257,7 +258,7 @@ public function test_get_only_returns_matching_object_classes() $model = new class extends Entry { - public static $objectClasses = ['three', 'four']; + public static array $objectClasses = ['three', 'four']; }; $this->assertCount(0, $model::get()); @@ -569,7 +570,7 @@ public function test_listing() (new TestModelStub(['cn' => 'Jen']))->inside('ou=Accounts,ou=Users,dc=local,dc=com')->save(); $this->assertCount(2, TestModelStub::in('ou=Users,dc=local,dc=com')->get()); - $this->assertCount(1, TestModelStub::listing()->in('ou=Users,dc=local,dc=com')->get()); + $this->assertCount(1, TestModelStub::list()->in('ou=Users,dc=local,dc=com')->get()); $this->assertCount(4, TestModelStub::in('dc=local,dc=com')->get()); } @@ -629,14 +630,14 @@ public function test_domain_scoping() $alpha = new class extends Entry { - protected $connection = 'alpha'; - public static $objectClasses = ['one', 'two']; + protected ?string $connection = 'alpha'; + public static array $objectClasses = ['one', 'two']; }; $bravo = new class extends Entry { - protected $connection = 'bravo'; - public static $objectClasses = ['one', 'two']; + protected ?string $connection = 'bravo'; + public static array $objectClasses = ['one', 'two']; }; $alphaUser = $alpha::create(['cn' => 'John']); @@ -652,12 +653,12 @@ public function test_domain_scoping() class TestModelStub extends Entry { - public static $objectClasses = ['one', 'two']; + public static array $objectClasses = ['one', 'two']; } class TestHasManyInStub extends TestModelStub { - public function members() + public function members(): HasManyIn { return $this->hasManyIn(TestModelStub::class, 'members'); } diff --git a/tests/Feature/ListenForLdapBindFailureTest.php b/tests/Feature/ListenForLdapBindFailureTest.php index 9e82a207..a66c449e 100644 --- a/tests/Feature/ListenForLdapBindFailureTest.php +++ b/tests/Feature/ListenForLdapBindFailureTest.php @@ -4,6 +4,7 @@ use Illuminate\Support\Facades\Auth; use LdapRecord\Laravel\Tests\TestCase; +use LdapRecord\LdapResultResponse; use LdapRecord\Models\ActiveDirectory\User; use LdapRecord\Testing\DirectoryFake; use LdapRecord\Testing\LdapFake; @@ -56,25 +57,25 @@ public function test_validation_exception_is_not_thrown_until_all_connection_hos LdapFake::operation('bind') ->with('user', 'secret') ->twice() - ->andReturn(false), + ->andReturn(new LdapResultResponse(1)), // Third bind attempt passes. LdapFake::operation('bind') ->with('user', 'secret') ->once() - ->andReturn(true), + ->andReturn(new LdapResultResponse()), // Bind is attempted with the authenticating user and passes. LdapFake::operation('bind') ->with('cn=jdoe,dc=local,dc=com', 'secret') ->once() - ->andReturn(true), + ->andReturn(new LdapResultResponse()), // Rebind is attempted with configured user account. LdapFake::operation('bind') ->with('user', 'secret') ->once() - ->andReturn(true), + ->andReturn(new LdapResultResponse(0)), // Search operation is executed for authenticating user. LdapFake::operation('search') @@ -83,7 +84,8 @@ public function test_validation_exception_is_not_thrown_until_all_connection_hos ->andReturn($expectedQueryResult), LdapFake::operation('parseResult') - ->once(), + ->once() + ->andReturn(new LdapResultResponse()), ])->shouldReturnError("Can't contact LDAP server"); $result = Auth::attempt([ diff --git a/tests/Unit/LdapImporterTest.php b/tests/Unit/LdapImporterTest.php index 3d07e37b..0cb7627e 100644 --- a/tests/Unit/LdapImporterTest.php +++ b/tests/Unit/LdapImporterTest.php @@ -100,9 +100,9 @@ public function test_scopes_can_be_applied_to_import_query() class TestImporterScopeStub implements Scope { - public function apply(Builder $query, LdapModel $model) + public function apply(Builder $query, LdapModel $model): void { - return $query->where('cn', 'Second Group'); + $query->where('cn', 'Second Group'); } } diff --git a/tests/Unit/LdapServiceProviderTest.php b/tests/Unit/LdapServiceProviderTest.php index 9470b17f..6798321e 100644 --- a/tests/Unit/LdapServiceProviderTest.php +++ b/tests/Unit/LdapServiceProviderTest.php @@ -69,7 +69,7 @@ public function test_logger_is_set_on_container_when_enabled() public function test_cache_is_set_on_connection_when_enabled() { - $this->assertInstanceOf(Cache::class, Container::getInstance()->get('default')->getCache()); + $this->assertInstanceOf(Cache::class, Container::getInstance()->getConnection('default')->getCache()); } public function test_connections_from_environment_variables_are_setup() diff --git a/tests/Unit/LdapUserRepositoryTest.php b/tests/Unit/LdapUserRepositoryTest.php index 31f65077..ef142291 100644 --- a/tests/Unit/LdapUserRepositoryTest.php +++ b/tests/Unit/LdapUserRepositoryTest.php @@ -62,7 +62,7 @@ public function test_find_by_credentials_returns_model() $repository = m::mock(LdapUserRepository::class, function ($repository) use ($user) { $query = m::mock(Builder::class); - $query->shouldReceive('where')->withArgs(['username', 'foo'])->andReturnSelf(); + $query->shouldReceive('where')->with('username', 'foo')->andReturnSelf(); $query->shouldReceive('first')->once()->andReturn($user); $repository->makePartial()->shouldAllowMockingProtectedMethods(); @@ -78,7 +78,7 @@ public function test_find_by_credentials_with_fallback_returns_model() $repository = m::mock(LdapUserRepository::class, function ($repository) use ($user) { $query = m::mock(Builder::class); - $query->shouldReceive('where')->withArgs(['username', 'foo'])->andReturnSelf(); + $query->shouldReceive('where')->with('username', 'foo')->andReturnSelf(); $query->shouldReceive('first')->once()->andReturn($user); $repository->makePartial()->shouldAllowMockingProtectedMethods(); @@ -90,24 +90,26 @@ public function test_find_by_credentials_with_fallback_returns_model() public function test_find_by_attribute_and_value_returns_model() { - $repository = m::mock(LdapUserRepository::class, function ($repository) { + $model = new Entry(); + + $repository = m::mock(LdapUserRepository::class, function ($repository) use ($model) { $query = m::mock(Builder::class); - $query->shouldReceive('findBy')->once()->withArgs(['foo', 'bar'])->andReturn('baz'); + $query->shouldReceive('findBy')->once()->with('foo', 'bar')->andReturn($model); $repository->makePartial()->shouldAllowMockingProtectedMethods(); $repository->shouldReceive('newModelQuery')->once()->andReturn($query); }); - $this->assertSame('baz', $repository->findBy('foo', 'bar')); + $this->assertSame($model, $repository->findBy('foo', 'bar')); } public function test_find_by_model_returns_model() { - $model = new \stdClass(); + $model = new Entry(); $repository = m::mock(LdapUserRepository::class, function ($repository) use ($model) { $query = m::mock(Builder::class); - $query->shouldReceive('findByGuid')->once()->withArgs(['guid'])->andReturn($model); + $query->shouldReceive('findByGuid')->once()->with('guid')->andReturn($model); $repository->makePartial()->shouldAllowMockingProtectedMethods(); $repository->shouldReceive('newModelQuery')->once()->andReturn($query); @@ -121,14 +123,16 @@ public function test_find_by_model_returns_model() public function test_find_by_guid_returns_model() { - $repository = m::mock(LdapUserRepository::class, function ($repository) { + $model = new Entry(); + + $repository = m::mock(LdapUserRepository::class, function ($repository) use ($model) { $query = m::mock(Builder::class); - $query->shouldReceive('findByGuid')->once()->withArgs(['guid'])->andReturn('foo'); + $query->shouldReceive('findByGuid')->once()->with('guid')->andReturn($model); $repository->makePartial()->shouldAllowMockingProtectedMethods(); $repository->shouldReceive('newModelQuery')->once()->andReturn($query); }); - $this->assertSame('foo', $repository->findByGuid('guid')); + $this->assertSame($model, $repository->findByGuid('guid')); } }