Example #1
0
 public function test__construct()
 {
     // Should throw an error if argument array (metadata) isn't provided
     $this->assert->error(function () {
         new Src\Definition();
     })->withType(E_RECOVERABLE_ERROR);
     // Should set the default type "mixed"
     $definition = new Src\Definition(array('attribute' => 'attribute', 'key' => 'key', 'accessor' => 'accessor', 'mutator' => 'mutator'));
     $this->assert->string($definition->getType())->isEqualTo('mixed');
     // Should set a provided type
     $definition = new Src\Definition(array('attribute' => 'attribute', 'key' => 'key', 'type' => 'string', 'accessor' => 'accessor', 'mutator' => 'mutator'));
     $this->assert->string($definition->getType())->isEqualTo('string');
     // Should ignore mappedClass when providing a supported non-mappable (pseudo) type
     $definition = new Src\Definition(array('attribute' => 'attribute', 'key' => 'key', 'type' => 'string', 'accessor' => 'accessor', 'mutator' => 'mutator', 'mappedClass' => '\\User\\Namespace'));
     $this->assert->string($definition->getType())->isEqualTo('string')->variable($definition->getMappedType())->isNull()->variable($definition->getMappedClass())->isNull();
     // Should hanlde a custom type (FQDN): must be defined as a type and a mappedClass
     $definition = new Src\Definition(array('attribute' => 'attribute', 'key' => 'key', 'accessor' => 'accessor', 'mutator' => 'mutator', 'type' => '\\User\\Namespace\\Object'));
     $this->assert->string($definition->getType())->isEqualTo('\\User\\Namespace\\Object')->string($definition->getMappedType())->isEqualTo(Src\Definition::DOCUMENT)->string($definition->getMappedClass())->isEqualTo('\\User\\Namespace\\Object');
     // Should prepend a \ to a custom type (FQDN)
     $definition = new Src\Definition(array('attribute' => 'attribute', 'key' => 'key', 'accessor' => 'accessor', 'mutator' => 'mutator', 'type' => 'User\\Namespace\\Object'));
     $this->assert->string($definition->getType())->isEqualTo('\\User\\Namespace\\Object')->string($definition->getMappedClass())->isEqualTo('\\User\\Namespace\\Object');
     // Should handle embedded collection of documents with type array and custom type as mappedClass
     $definition = new Src\Definition(array('attribute' => 'attribute', 'key' => 'key', 'accessor' => 'accessor', 'mutator' => 'mutator', 'type' => 'array', 'mappedClass' => '\\Valid\\Namespace\\Object'));
     $this->assert->string($definition->getType())->isEqualTo('array')->string($definition->getMappedType())->isEqualTo(Src\Definition::COLLECTION)->string($definition->getMappedClass())->isEqualTo('\\Valid\\Namespace\\Object');
     // Should prepend a \ for embedded collection of documents
     $definition = new Src\Definition(array('attribute' => 'attribute', 'key' => 'key', 'accessor' => 'accessor', 'mutator' => 'mutator', 'type' => 'array', 'mappedClass' => 'Valid\\Namespace\\Object'));
     $this->assert->string($definition->getType())->isEqualTo('array')->string($definition->getMappedClass())->isEqualTo('\\Valid\\Namespace\\Object');
     // Should throw exception if type is not supported and isn't a valid FQDN
     $this->assert->exception(function () {
         new Src\Definition(array('attribute' => 'attribute', 'key' => 'key', 'accessor' => 'accessor', 'mutator' => 'mutator', 'type' => 'invalid_FQDN'));
     })->isInstanceOf('InvalidArgumentException')->hasMessage('User type "invalid_FQDN" is not a valid FQDN');
     // Should throw exception if type is array and mappedClass isn't a valid FQDN
     $this->assert->exception(function () {
         new Src\Definition(array('attribute' => 'attribute', 'key' => 'key', 'accessor' => 'accessor', 'mutator' => 'mutator', 'type' => 'array', 'mappedClass' => 'invalid_FQDN'));
     })->isInstanceOf('InvalidArgumentException')->hasMessage('Mapped class "invalid_FQDN" is not a valid FQDN');
 }