| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace As3\Modlr\Persister\MongoDb\Tests; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use PHPUnit_Framework_TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Doctrine\MongoDB\Configuration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Doctrine\MongoDB\Connection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use As3\Modlr; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class PersisterTest extends PHPUnit_Framework_TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     protected static $dbName = 'modlr_mongodb'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     protected $connection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     protected $smf; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     protected $persister; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     public function setUp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         // Initialize the doctrine connection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $config = new Configuration(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $config->setLoggerCallable(function($msg) {}); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $this->connection = new Connection(null, array(), $config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         // Initialize the metadata factory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $typeFactory = new Modlr\DataTypes\TypeFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $validator = new Modlr\Util\Validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $config = new Modlr\Rest\RestConfiguration($validator); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $config->setRootEndpoint('/modlr/api/'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $entityUtil = new Modlr\Util\EntityUtility($config, $typeFactory); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->smf = new Modlr\Persister\MongoDb\StorageMetadataFactory($entityUtil); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $formatter = new Modlr\Persister\MongoDb\Formatter(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $query = new Modlr\Persister\MongoDb\Query($this->connection, $formatter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $hydrator = new Modlr\Persister\MongoDb\Hydrator(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->persister = new Modlr\Persister\MongoDb\Persister($query, $this->smf, $hydrator); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     public function tearDown() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $collections = $this->connection->selectDatabase(self::$dbName)->listCollections(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         foreach ($collections as $collection) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             $collection->drop(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $this->connection->close(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         unset($this->connection); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     public function testPersisterValues() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $this->assertEquals('mongodb', $this->persister->getPersisterKey()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $this->assertEquals('_type', $this->persister->getPolymorphicKey()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $this->assertEquals('_id', $this->persister->getIdentifierKey()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     public function testMDFInstance() | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         $this->assertInstanceOf( | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |             'As3\Modlr\Metadata\Interfaces\StorageMetadataFactoryInterface', | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |             $this->persister->getPersistenceMetadataFactory() | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @expectedException           As3\Modlr\Persister\PersisterException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @expectedExceptionMessage    ID conversion currently only supports an object strategy, or none at all. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     public function testConvertIdInvalidStrategy() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $this->assertInstanceOf('MongoId', $this->persister->convertId('test', 'blag')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @expectedException           As3\Modlr\Persister\PersisterException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @expectedExceptionMessage    ID generation currently only supports an object strategy, or none at all. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     public function testGenerateIdInvalidStrategy() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $this->assertInstanceOf('MongoId', $this->persister->generateId('blag')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     public function testConvertIdObject() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $test = '49a7011a05c677b9a916612a'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $id = new \MongoId($test); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $this->assertEquals($id, $this->persister->convertId($id)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $this->assertEquals($id, $this->persister->convertId($id, 'object')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     public function testGenerateIdObject() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $this->assertInstanceOf('MongoId', $this->persister->generateId()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $this->assertInstanceOf('MongoId', $this->persister->generateId('object')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @expectedException           As3\Modlr\Persister\PersisterException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @expectedExceptionMessage    ID conversion currently only supports an object strategy, or none at all. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     public function testConvertIdIncrementId() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $test = 12345; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $this->assertEquals($test, $this->persister->convertId($test, 'incrementId')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @expectedException           As3\Modlr\Persister\PersisterException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @expectedExceptionMessage    ID generation currently only supports an object strategy, or none at all. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     public function testGenerateIdIncrementId() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $val = $this->persister->generateId('incrementId'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $this->assertGreaterThan($val, $this->persister->generateId('incrementId')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 121 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 122 |  |  |  |