Completed
Push — master ( 0d0a18...6997ac )
by Julián
02:15
created

MongoDBBuilderTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 97
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * doctrine-manager-builder (https://github.com/juliangut/doctrine-manager-builder)
4
 * Doctrine2 managers builder
5
 *
6
 * @license BSD-3-Clause
7
 * @author Julián Gutiérrez <[email protected]>
8
 */
9
10
namespace Jgut\Doctrine\ManagerBuilder\Tests;
11
12
use Doctrine\MongoDB\Connection;
13
use Doctrine\ODM\MongoDB\DocumentManager;
14
use Jgut\Doctrine\ManagerBuilder\ManagerBuilder;
15
use Jgut\Doctrine\ManagerBuilder\MongoDBBuilder;
16
use Symfony\Component\Console\Command\Command;
17
use Symfony\Component\Console\Helper\HelperSet;
18
19
/**
20
 * MongoDB entity builder tests.
21
 *
22
 * @group mongodb
23
 */
24
class MongoDBBuilderTest extends \PHPUnit_Framework_TestCase
25
{
26
    /**
27
     * @var MongoDBBuilder
28
     */
29
    protected $builder;
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function setUp()
35
    {
36
        $this->builder = new MongoDBBuilder([], 'test');
37
    }
38
39
    /**
40
     * @expectedException \InvalidArgumentException
41
     */
42
    public function testManagerNoConnection()
43
    {
44
        $this->builder->setOption('connection', null);
45
        $this->builder->setOption(
46
            'metadata_mapping',
47
            [
48
                ['type' => ManagerBuilder::METADATA_MAPPING_ANNOTATION, 'path' => __DIR__, 'namespace' => 'annotation'],
49
                ['type' => ManagerBuilder::METADATA_MAPPING_XML, 'path' => __DIR__, 'namespace' => 'xml'],
50
                ['type' => ManagerBuilder::METADATA_MAPPING_YAML, 'path' => __DIR__, 'namespace' => 'yaml'],
51
                ['type' => ManagerBuilder::METADATA_MAPPING_PHP, 'path' => __DIR__, 'namespace' => 'php'],
52
            ]
53
        );
54
55
        $this->builder->getManager(true);
56
    }
57
58
    /**
59
     * @expectedException \RuntimeException
60
     * @expectedExceptionMessage Cannot use different EventManager instances for DocumentManager and Connection.
61
     */
62
    public function testManageWrongConnection()
63
    {
64
        $this->builder->setOption('connection', new Connection('localhost'));
65
        $this->builder->setOption(
66
            'metadata_mapping',
67
            [['type' => ManagerBuilder::METADATA_MAPPING_ANNOTATION, 'path' => __DIR__]]
68
        );
69
70
        $this->builder->getManager();
71
    }
72
73
    public function testManager()
74
    {
75
        $connection = new Connection('localhost', [], null, $this->builder->getEventManager());
76
77
        $this->builder->setOption('connection', $connection);
78
        $this->builder->setOption(
79
            'metadata_mapping',
80
            [['type' => ManagerBuilder::METADATA_MAPPING_ANNOTATION, 'path' => __DIR__]]
81
        );
82
        $this->builder->setOption('default_database', 'ddbb');
83
        $this->builder->setOption('logger_callable', 'class_exists');
84
85
        static::assertInstanceOf(DocumentManager::class, $this->builder->getManager());
86
    }
87
88
    public function testConsoleCommands()
89
    {
90
        $this->builder->setOption('connection', ['server' => 'localhost']);
91
        $this->builder->setOption(
92
            'metadata_mapping',
93
            [['type' => ManagerBuilder::METADATA_MAPPING_ANNOTATION, 'path' => __DIR__]]
94
        );
95
96
        $commands = $this->builder->getConsoleCommands();
97
98
        return array_walk(
99
            $commands,
100
            function (Command $command) {
101
                static::assertEquals(1, preg_match('/^test:odm:/', $command->getName()));
102
            }
103
        );
104
    }
105
106
    public function testConsoleHelperSet()
107
    {
108
        $this->builder->setOption('connection', ['server' => 'localhost']);
109
        $this->builder->setOption(
110
            'metadata_mapping',
111
            [['type' => ManagerBuilder::METADATA_MAPPING_ANNOTATION, 'path' => __DIR__]]
112
        );
113
114
        $helperSet = $this->builder->getConsoleHelperSet();
115
116
        static::assertInstanceOf(HelperSet::class, $helperSet);
117
        static::assertTrue($helperSet->has('documentManager'));
118
        static::assertTrue($helperSet->has('dm'));
119
    }
120
}
121