Skip to content
This repository was archived by the owner on Jul 24, 2023. It is now read-only.

Commit 2d48aa7

Browse files
committed
Import command tweaks and fixes
- Use PHPUnit 7.* - Use Orchestra Testbench 3.7 for Laravel 5.7 compatibility - Throw a runtime exception when no users are found when running the command - Check if the console is interactive before asking confirmation to display users to be imported / synchornized - Check if the console is not interactive before asking to import the located users - Updated console tests
1 parent aaed127 commit 2d48aa7

File tree

3 files changed

+59
-19
lines changed

3 files changed

+59
-19
lines changed

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
},
1111
"require-dev": {
1212
"mockery/mockery": "~1.0",
13-
"phpunit/phpunit": "~6.0",
14-
"orchestra/testbench": "~3.2"
13+
"phpunit/phpunit": "~7.0",
14+
"orchestra/testbench": "~3.7"
1515
},
1616
"archive": {
1717
"exclude": ["/tests"]

src/Commands/Console/Import.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class Import extends Command
3939
*
4040
* @return void
4141
*
42+
* @throws \RuntimeException
4243
* @throws \Adldap\Models\ModelNotFoundException
4344
*/
4445
public function handle()
@@ -47,17 +48,25 @@ public function handle()
4748

4849
$count = count($users);
4950

50-
if ($count === 1) {
51+
if ($count === 0) {
52+
throw new \RuntimeException("There were no users found to import.");
53+
} else if ($count === 1) {
5154
$this->info("Found user '{$users[0]->getCommonName()}'.");
5255
} else {
5356
$this->info("Found {$count} user(s).");
5457
}
5558

56-
if ($this->confirm('Would you like to display the user(s) to be imported / synchronized?', $default = false)) {
59+
if (
60+
$this->input->isInteractive() &&
61+
$this->confirm('Would you like to display the user(s) to be imported / synchronized?', $default = false)
62+
) {
5763
$this->display($users);
5864
}
5965

60-
if ($this->confirm('Would you like these users to be imported / synchronized?', $default = true)) {
66+
if (
67+
! $this->input->isInteractive() ||
68+
$this->confirm('Would you like these users to be imported / synchronized?', $default = true)
69+
) {
6170
$imported = $this->import($users);
6271

6372
$this->info("Successfully imported / synchronized {$imported} user(s).");

tests/Console/ImportTest.php

+45-14
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ public function test_importing_one_user()
2323
->shouldReceive('getLdapDiscoveryAttribute')->once()->andReturn('mail')
2424
->shouldReceive('getEloquentUsernameAttribute')->once()->andReturn('email');
2525

26-
$r = $this->artisan('adldap:import', ['user' => 'jdoe', '--no-interaction']);
26+
$this->artisan('adldap:import', ['user' => 'jdoe', '--no-interaction' => true])
27+
->expectsOutput("Found user 'John Doe'.")
28+
->expectsOutput("Successfully imported / synchronized 1 user(s).")
29+
->assertExitCode(0);
2730

28-
$this->assertEquals(0, $r);
2931
$this->assertDatabaseHas('users', ['email' => '[email protected]']);
3032
}
3133

@@ -55,13 +57,37 @@ public function test_importing_multiple_users()
5557
->shouldReceive('getLdapDiscoveryAttribute')->twice()->andReturn('mail')
5658
->shouldReceive('getEloquentUsernameAttribute')->twice()->andReturn('email');
5759

58-
$r = $this->artisan('adldap:import', ['--no-interaction']);
60+
$this->artisan('adldap:import', ['--no-interaction' => true])
61+
->expectsOutput("Found 2 user(s).")
62+
->expectsOutput("Successfully imported / synchronized 2 user(s).")
63+
->assertExitCode(0);
5964

60-
$this->assertEquals(0, $r);
6165
$this->assertDatabaseHas('users', ['email' => '[email protected]']);
6266
$this->assertDatabaseHas('users', ['email' => '[email protected]']);
6367
}
6468

69+
public function test_questions_asked_with_interaction()
70+
{
71+
$b = m::mock(Builder::class);
72+
73+
$u = $this->makeLdapUser();
74+
75+
$b->shouldReceive('findOrFail')->once()->with('jdoe')->andReturn($u);
76+
77+
Resolver::shouldReceive('query')->once()->andReturn($b)
78+
->shouldReceive('getLdapDiscoveryAttribute')->once()->andReturn('mail')
79+
->shouldReceive('getEloquentUsernameAttribute')->once()->andReturn('email');
80+
81+
$this->artisan('adldap:import', ['user' => 'jdoe'])
82+
->expectsOutput("Found user 'John Doe'.")
83+
->expectsQuestion('Would you like to display the user(s) to be imported / synchronized?', 'no')
84+
->expectsQuestion('Would you like these users to be imported / synchronized?', 'yes')
85+
->expectsOutput("Successfully imported / synchronized 1 user(s).")
86+
->assertExitCode(0);
87+
88+
$this->assertDatabaseHas('users', ['email' => '[email protected]']);
89+
}
90+
6591
public function test_model_will_be_restored_when_ldap_account_is_active()
6692
{
6793
$model = TestUser::create([
@@ -93,9 +119,11 @@ public function test_model_will_be_restored_when_ldap_account_is_active()
93119
->shouldReceive('getLdapDiscoveryAttribute')->once()->andReturn('mail')
94120
->shouldReceive('getEloquentUsernameAttribute')->once()->andReturn('email');
95121

96-
$r = $this->artisan('adldap:import', ['--restore' => true, '--no-interaction' => true]);
122+
$this->artisan('adldap:import', ['--restore' => true, '--no-interaction' => true])
123+
->expectsOutput("Found user 'John Doe'.")
124+
->expectsOutput("Successfully imported / synchronized 1 user(s).")
125+
->assertExitCode(0);
97126

98-
$this->assertEquals(0, $r);
99127
$this->assertFalse($model->fresh()->trashed());
100128
}
101129

@@ -128,27 +156,30 @@ public function test_model_will_be_soft_deleted_when_ldap_account_is_disabled()
128156
->shouldReceive('getLdapDiscoveryAttribute')->once()->andReturn('mail')
129157
->shouldReceive('getEloquentUsernameAttribute')->once()->andReturn('email');
130158

131-
$r = $this->artisan('adldap:import', ['--delete' => true, '--no-interaction' => true]);
132-
133-
$this->assertEquals(0, $r);
159+
$this->artisan('adldap:import', ['--delete' => true, '--no-interaction' => true])
160+
->expectsOutput("Found user 'John Doe'.")
161+
->expectsOutput("Successfully imported / synchronized 1 user(s).")
162+
->assertExitCode(0);
163+
134164
$this->assertTrue($model->fresh()->trashed());
135165
}
136166

167+
/**
168+
* @expectedException \RuntimeException
169+
*/
137170
public function test_filter_option_applies_to_ldap_query()
138171
{
139172
$f = '(samaccountname=jdoe)';
140173

141174
$b = m::mock(Builder::class);
142175

143176
$b
144-
->shouldReceive('rawFilter')->once()->with($f)->andReturn($b)
145-
->shouldReceive('paginate')->once()->andReturn($b)
177+
->shouldReceive('rawFilter')->once()->with($f)->andReturnSelf()
178+
->shouldReceive('paginate')->once()->andReturnSelf()
146179
->shouldReceive('getResults')->once()->andReturn([]);
147180

148181
Resolver::shouldReceive('query')->once()->andReturn($b);
149182

150-
$r = $this->artisan('adldap:import', ['--filter' => $f, '--no-interaction' => true]);
151-
152-
$this->assertEquals(0, $r);
183+
$this->artisan('adldap:import', ['--filter' => $f, '--no-interaction' => true]);
153184
}
154185
}

0 commit comments

Comments
 (0)