Skip to content

Commit

Permalink
Refactor rename tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stevebauman committed Nov 7, 2023
1 parent 3e2f26d commit e8e9e16
Showing 1 changed file with 35 additions and 52 deletions.
87 changes: 35 additions & 52 deletions tests/Unit/Models/ModelRenameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
use LdapRecord\Models\Entry;
use LdapRecord\Models\Model;
use LdapRecord\Models\ModelDoesNotExistException;
use LdapRecord\Query\Model\Builder;
use LdapRecord\Testing\DirectoryFake;
use LdapRecord\Testing\LdapFake;
use LdapRecord\Tests\TestCase;
use Mockery as m;

class ModelRenameTest extends TestCase
{
Expand All @@ -33,8 +31,17 @@ protected function tearDown(): void

public function test_rename()
{
$model = new ModelRenameTestStub();
$model->setRawAttributes(['dn' => 'cn=John Doe,dc=acme,dc=org']);
DirectoryFake::setup()
->getLdapConnection()
->expect(
LdapFake::operation('rename')
->with('cn=John Doe,dc=acme,dc=org', 'cn=Jane Doe', 'dc=acme,dc=org')
->andReturnTrue()
);

$model = (new Entry)->setRawAttributes([
'dn' => 'cn=John Doe,dc=acme,dc=org',
]);

$model->rename('cn=Jane Doe');

Expand All @@ -44,10 +51,19 @@ public function test_rename()
$this->assertTrue($model->wasRecentlyRenamed);
}

public function test_rename_with_no_attribute()
public function test_rename_with_no_attribute_name()
{
$model = new ModelRenameTestStub();
$model->setRawAttributes(['dn' => 'cn=John Doe,dc=acme,dc=org']);
DirectoryFake::setup()
->getLdapConnection()
->expect(
LdapFake::operation('rename')
->with('cn=John Doe,dc=acme,dc=org', 'cn=Jane Doe', 'dc=acme,dc=org')
->andReturnTrue()
);

$model = (new Entry)->setRawAttributes([
'dn' => 'cn=John Doe,dc=acme,dc=org',
]);

$model->rename('Jane Doe');

Expand All @@ -57,10 +73,19 @@ public function test_rename_with_no_attribute()
$this->assertTrue($model->wasRecentlyRenamed);
}

public function test_rename_with_parent()
public function test_rename_with_new_parent()
{
$model = new ModelRenameWithParentTestStub();
$model->setRawAttributes(['dn' => 'cn=John Doe,dc=acme,dc=org']);
DirectoryFake::setup()
->getLdapConnection()
->expect(
LdapFake::operation('rename')
->with('cn=John Doe,dc=acme,dc=org', 'cn=Jane Doe', 'ou=Users,dc=acme,dc=org')
->andReturnTrue()
);

$model = (new Entry)->setRawAttributes([
'dn' => 'cn=John Doe,dc=acme,dc=org',
]);

$model->rename('cn=Jane Doe', 'ou=Users,dc=acme,dc=org');

Expand Down Expand Up @@ -179,45 +204,3 @@ public function test_move()
$this->assertTrue($model->wasRecentlyRenamed);
}
}

class ModelRenameTestStub extends Model
{
public function newQuery(): Builder
{
$builder = m::mock(Builder::class);
$builder->shouldReceive('rename')
->with('cn=John Doe,dc=acme,dc=org', 'cn=Jane Doe', 'dc=acme,dc=org', true)
->once()
->andReturnTrue();

return $builder;
}
}

class ModelRenameWithParentTestStub extends Model
{
public function newQuery(): Builder
{
$builder = m::mock(Builder::class);
$builder->shouldReceive('rename')
->with('cn=John Doe,dc=acme,dc=org', 'cn=Jane Doe', 'ou=Users,dc=acme,dc=org', true)
->once()
->andReturnTrue();

return $builder;
}
}

class ModelMoveTestStub extends Model
{
public function newQuery(): Builder
{
$builder = m::mock(Builder::class);
$builder->shouldReceive('rename')
->with('cn=John Doe,dc=acme,dc=org', 'cn=John Doe', 'ou=Users,dc=acme,dc=org', true)
->once()
->andReturnTrue();

return $builder;
}
}

0 comments on commit e8e9e16

Please sign in to comment.