ProxyTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 7
dl 0
loc 61
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 32 1
A testGetReference() 0 11 1
A testProxyFactorySetsProxyMetadata() 0 8 1
1
<?php
2
3
namespace Doctrine\Tests\ODM\CouchDB\Functional;
4
5
use Doctrine\Common\Persistence\Mapping\RuntimeReflectionService;
6
7
class ProxyTest extends \Doctrine\Tests\ODM\CouchDB\CouchDBFunctionalTestCase
8
{
9
    /**
10
     * @var DocumentManager
11
     */
12
    private $dm;
13
14
    public function setUp()
15
    {
16
        $this->type = 'Doctrine\Tests\ODM\CouchDB\Functional\Article';
0 ignored issues
show
Bug introduced by
The property type does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
17
18
        $this->dm = $this->createDocumentManager();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createDocumentManager() of type object<Doctrine\ODM\CouchDB\DocumentManager> is incompatible with the declared type object<Doctrine\Tests\OD...tional\DocumentManager> of property $dm.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
19
20
        $database = $this->dm->getDatabase();
21
        $httpClient = $this->dm->getHttpClient();
22
23
        $httpClient->request('DELETE', '/' . $database);
24
        $resp = $httpClient->request('PUT', '/' . $database);
25
        $this->assertEquals(201, $resp->status);
26
27
        $data = json_encode(array(
28
            '_id' => "1",
29
            'title' => 'foo',
30
            'body' => 'bar',
31
            'type' => $this->type
32
        ));
33
        $resp = $httpClient->request('PUT', '/' . $database . '/1', $data);
34
        $this->assertEquals(201, $resp->status);
35
36
        $cmf = $this->dm->getClassMetadataFactory();
37
        $metadata = new \Doctrine\ODM\CouchDB\Mapping\ClassMetadata($this->type);
38
        $metadata->mapField(array('fieldName' => 'id', 'id' => true));
39
        $metadata->mapField(array('fieldName' => 'title', 'type' => 'string'));
40
        $metadata->mapField(array('fieldName' => 'body', 'type' => 'string'));
41
        $metadata->idGenerator = \Doctrine\ODM\CouchDB\Mapping\ClassMetadata::IDGENERATOR_UUID;
42
        $metadata->initializeReflection(new RuntimeReflectionService());
43
        $metadata->wakeupReflection(new RuntimeReflectionService());
44
        $cmf->setMetadataFor($this->type, $metadata);
45
    }
46
47
    public function testGetReference()
48
    {
49
        $proxy = $this->dm->getReference($this->type, 1);
50
51
        $this->assertInstanceOf('Doctrine\ODM\CouchDB\Proxy\Proxy', $proxy);
52
        $this->assertFalse($proxy->__isInitialized__);
53
54
        $this->assertEquals('foo', $proxy->getTitle());
55
        $this->assertTrue($proxy->__isInitialized__);
56
        $this->assertEquals('bar', $proxy->getBody());
57
    }
58
59
    public function testProxyFactorySetsProxyMetadata()
60
    {
61
        $proxy = $this->dm->getReference($this->type, 1);
62
63
        $proxyClass = get_class($proxy);
64
        $this->assertTrue($this->dm->getClassMetadataFactory()->hasMetadataFor($proxyClass), "Proxy class '" . $proxyClass . "' should be registered as metadata.");
65
        $this->assertSame($this->dm->getClassMetadata($proxyClass), $this->dm->getClassMetadata($this->type), "Metadata instances of proxy class and real instance have to be the same.");
66
    }
67
}
68
69
class Article
70
{
71
    private $id;
72
    private $title;
73
    private $body;
74
75
    public function __construct($title, $body)
76
    {
77
        $this->title = $title;
78
        $this->body = $body;
79
    }
80
81
    public function getTitle()
82
    {
83
        return $this->title;
84
    }
85
86
    public function getBody()
87
    {
88
        return $this->body;
89
    }
90
}
91