22
33namespace Doctrine \ODM \MongoDB \Tests \Functional ;
44
5+ use Doctrine \Common \Collections \ArrayCollection ;
6+ use Doctrine \ODM \MongoDB \LockException ;
57use Doctrine \ODM \MongoDB \LockMode ;
68use Doctrine \ODM \MongoDB \Mapping \Annotations as ODM ;
9+ use Documents \Issue ;
710
811class LockTest extends \Doctrine \ODM \MongoDB \Tests \BaseTest
912{
@@ -365,6 +368,45 @@ public function testInvalidVersionDocument()
365368 $ this ->setExpectedException ('Doctrine\ODM\MongoDB\MongoDBException ' , 'Invalid version field type string. Version field must be int or date. ' );
366369 $ this ->dm ->getClassMetadata (__NAMESPACE__ .'\InvalidVersionDocument ' );
367370 }
371+
372+ /**
373+ * @expectedException Doctrine\ODM\MongoDB\LockException
374+ */
375+ public function testUpdatingCollectionRespectsVersionNumber ()
376+ {
377+ $ d = new LockInt ('test ' );
378+ $ d ->issues ->add (new Issue ('hi ' , 'ohai ' ));
379+ $ this ->dm ->persist ($ d );
380+ $ this ->dm ->flush ();
381+
382+ // simulate another request updating document in the meantime
383+ $ this ->dm ->getDocumentCollection (__NAMESPACE__ .'\LockInt ' )->update (
384+ array ('_id ' => new \MongoId ($ d ->id )),
385+ array ('$set ' => array ('version ' => 2 ))
386+ );
387+
388+ $ d ->issues ->add (new Issue ('oops ' , 'version mismatch ' ));
389+ $ this ->uow ->getCollectionPersister ()->update ($ d ->issues , array ());
390+ }
391+
392+ /**
393+ * @expectedException Doctrine\ODM\MongoDB\LockException
394+ */
395+ public function testDeletingCollectionRespectsVersionNumber ()
396+ {
397+ $ d = new LockInt ('test ' );
398+ $ d ->issues ->add (new Issue ('hi ' , 'ohai ' ));
399+ $ this ->dm ->persist ($ d );
400+ $ this ->dm ->flush ();
401+
402+ // simulate another request updating document in the meantime
403+ $ this ->dm ->getDocumentCollection (__NAMESPACE__ .'\LockInt ' )->update (
404+ array ('_id ' => new \MongoId ($ d ->id )),
405+ array ('$set ' => array ('version ' => 2 ))
406+ );
407+
408+ $ this ->uow ->getCollectionPersister ()->delete ($ d ->issues , array ());
409+ }
368410}
369411
370412/** @ODM\MappedSuperclass */
@@ -379,8 +421,14 @@ abstract class AbstractVersionBase
379421 /** @ODM\Lock @ODM\Int */
380422 public $ locked ;
381423
424+ /**
425+ * @ODM\EmbedMany(targetDocument="Documents\Issue")
426+ */
427+ public $ issues ;
428+
382429 public function __construct ($ title = null )
383430 {
431+ $ this ->issues = new ArrayCollection ();
384432 $ this ->title = $ title ;
385433 }
386434
@@ -391,7 +439,7 @@ public function getId()
391439
392440 public function getTitle ()
393441 {
394- return $ title ;
442+ return $ this -> title ;
395443 }
396444
397445 public function getVersion ()
0 commit comments