Skip to content

Commit

Permalink
Adding two test cases to BasicDocumentPersisterTest related to the re…
Browse files Browse the repository at this point in the history
…moval of embedded documents on an update.

In the first case, testModifyGroupsArrayDirectly() fetches the groups array from the user, removes two entries, and passes it back. The result is strange for two reasons:

  1) The update contains a pullAll() of all three original groups followed by a pushAll() on the one group that remains. This is inefficient, but should work.

  2) Even though the above should work, the final result of the test is that all three groups remain.

In the second case, testReplaceEntireGroupsArray() replaces the entire groups array with an array the contains one of the three original objects. The subsequent update contains only one pushAll() on the one group. There is no pullAll() to remove the two groups that are no longer on the User.
  • Loading branch information
weaverryan committed Sep 20, 2010
1 parent 524c5ae commit 9968697
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
Expand Up @@ -392,6 +392,128 @@ public function testReplaceGroups()
$this->assertEquals(3, count($user->getGroups()));
}

public function testModifyGroupsArrayDirectly()
{
$this->dm->getUnitOfWork()->setDocumentPersister(
'Documents\User', new BasicDocumentPersister($this->dm, $this->classMetadata)
);

$account = new Account();
$account->setName('Jon Test Account');

$user = new User();
$user->setUsername('jon333');
$user->setPassword('changeme');
$user->setAccount($account);

$user->addGroup(new Group('administrator'));
$user->addGroup(new Group('member'));
$user->addGroup(new Group('moderator'));

$this->dm->persist($user);
$this->dm->flush();
$this->dm->clear();

unset($user, $account);

$user = $this->dm->findOne('Documents\User');

// remove two of the groups and pass the groups back into the User
$groups = $user->getGroups();
unset($groups[0]);
unset($groups[2]);
$user->setGroups($groups);

$this->assertEquals(1, count($user->getGroups()));

$this->dm->getUnitOfWork()->setDocumentPersister(
'Documents\User', $this->persister
);

$this->persister->expects($this->once())
->method('update')
->with($user);

$this->dm->getUnitOfWork()->computeChangeSets();
$update = $this->persister->prepareUpdateData($user);

// the correct diff is just a pullAll of the two old Groups
$this->assertFalse(array_key_exists($this->escape('set'), $update));
$this->assertFalse(array_key_exists($this->escape('unset'), $update));
$this->assertFalse(array_key_exists($this->escape('pushAll'), $update));
$this->assertTrue(array_key_exists($this->escape('pullAll'), $update));
$this->assertTrue(array_key_exists('groups', $update[$this->escape('pullAll')]));
$this->assertEquals(2, count($update[$this->escape('pullAll')]['groups']));

$this->dm->flush();
$this->dm->clear();

unset($user);

$user = $this->dm->findOne('Documents\User');
$this->assertEquals(1, count($user->getGroups()));
}

public function testReplaceEntireGroupsArray()
{
$this->dm->getUnitOfWork()->setDocumentPersister(
'Documents\User', new BasicDocumentPersister($this->dm, $this->classMetadata)
);

$account = new Account();
$account->setName('Jon Test Account');

$user = new User();
$user->setUsername('jon333');
$user->setPassword('changeme');
$user->setAccount($account);

$group2 = new Group('member');
$user->addGroup(new Group('administrator'));
$user->addGroup($group2);
$user->addGroup(new Group('moderator'));

$this->dm->persist($user);
$this->dm->flush();
$this->dm->clear();

unset($user, $account);

$user = $this->dm->findOne('Documents\User');

// reffectively remove two of the groups
$user->setGroups(array($group2));

$this->assertEquals(1, count($user->getGroups()));

$this->dm->getUnitOfWork()->setDocumentPersister(
'Documents\User', $this->persister
);

$this->persister->expects($this->once())
->method('update')
->with($user);

$this->dm->getUnitOfWork()->computeChangeSets();
$update = $this->persister->prepareUpdateData($user);

// the correct diff is just a pullAll of the two old Groups
$this->assertFalse(array_key_exists($this->escape('set'), $update));
$this->assertFalse(array_key_exists($this->escape('unset'), $update));
$this->assertFalse(array_key_exists($this->escape('pushAll'), $update));
$this->assertTrue(array_key_exists($this->escape('pullAll'), $update));
$this->assertTrue(array_key_exists('groups', $update[$this->escape('pullAll')]));
$this->assertEquals(2, count($update[$this->escape('pullAll')]['groups']));

$this->dm->flush();
$this->dm->clear();

unset($user);

$user = $this->dm->findOne('Documents\User');
$this->assertEquals(1, count($user->getGroups()));
}

public function testCollectionField()
{
$classMetadata = $this->dm->getClassMetadata('Documents\Article');
Expand Down
9 changes: 9 additions & 0 deletions tests/Documents/User.php
Expand Up @@ -168,6 +168,15 @@ public function removeGroup($name)
return false;
}

public function setGroups($groups)
{
$this->groups = array();
foreach ($groups as $group)
{
$this->addGroup($group);
}
}

public function getHits()
{
return $this->hits;
Expand Down

0 comments on commit 9968697

Please sign in to comment.