Passed
Push — feature/composite-pk-get-by-id ( dc8bea )
by
unknown
05:45
created

BeanDescriptorTest::testGetTable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 Copyright (C) 2006-2014 David Négrier - THE CODING MACHINE
6
7
This program is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2 of the License, or
10
(at your option) any later version.
11
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
GNU General Public License for more details.
16
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software
19
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
*/
21
22
namespace TheCodingMachine\TDBM\Utils;
23
24
use Doctrine\Common\Cache\VoidCache;
25
use Doctrine\DBAL\Schema\Schema;
26
use Doctrine\DBAL\Schema\Table;
27
use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer;
28
use TheCodingMachine\TDBM\TDBMAbstractServiceTest;
29
use TheCodingMachine\TDBM\TDBMException;
30
use TheCodingMachine\TDBM\TDBMSchemaAnalyzer;
31
use TheCodingMachine\TDBM\Utils\Annotation\AnnotationParser;
32
33
class BeanDescriptorTest extends TDBMAbstractServiceTest
34
{
35
    /**
36
     * @var Schema
37
     */
38
    protected $schema;
39
    /**
40
     * @var SchemaAnalyzer
41
     */
42
    protected $schemaAnalyzer;
43
44
    /**
45
     * @var TDBMSchemaAnalyzer
46
     */
47
    protected $tdbmSchemaAnalyzer;
48
49
    protected function setUp(): void
50
    {
51
        parent::setUp();
52
        $schemaManager = $this->tdbmService->getConnection()->getSchemaManager();
53
        $this->schemaAnalyzer = new SchemaAnalyzer($schemaManager);
54
        $this->schema = $schemaManager->createSchema();
55
        $this->tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($this->tdbmService->getConnection(), new VoidCache(), $this->schemaAnalyzer);
56
    }
57
58
    public function testConstructor(): void
59
    {
60
        $usersTable = $this->schema->getTable('users');
61
        $beanDescriptor = new BeanDescriptor($usersTable, 'Tdbm\\Test\\Beans', 'Tdbm\\Test\\Beans\\Generated', 'Tdbm\\Test\\Daos', 'Tdbm\\Test\\Daos\\Generated', $this->schemaAnalyzer, $this->schema, $this->tdbmSchemaAnalyzer, $this->getNamingStrategy(), AnnotationParser::buildWithDefaultAnnotations([]), new BaseCodeGeneratorListener(), $this->getConfiguration());
62
        $propertyDescriptors = $beanDescriptor->getBeanPropertyDescriptors();
63
        $firstElem = reset($propertyDescriptors);
64
        $idProperty = $propertyDescriptors['$id'];
65
        $this->assertEquals($firstElem, $idProperty);
66
        $this->assertEquals('person', $idProperty->getTable()->getName());
67
        $this->assertInstanceOf('TheCodingMachine\\TDBM\\Utils\\ScalarBeanPropertyDescriptor', $idProperty);
68
        $countryProperty = $propertyDescriptors['$country'];
69
        $this->assertInstanceOf('TheCodingMachine\\TDBM\\Utils\\ObjectBeanPropertyDescriptor', $countryProperty);
70
        $nameProperty = $propertyDescriptors['$name'];
71
        $this->assertInstanceOf('TheCodingMachine\\TDBM\\Utils\\ScalarBeanPropertyDescriptor', $nameProperty);
72
    }
73
74
    public function testGetConstructorProperties(): void
75
    {
76
        $usersTable = $this->schema->getTable('users');
77
        $beanDescriptor = new BeanDescriptor($usersTable, 'Tdbm\\Test\\Beans', 'Tdbm\\Test\\Beans\\Generated', 'Tdbm\\Test\\Daos', 'Tdbm\\Test\\Daos\\Generated', $this->schemaAnalyzer, $this->schema, $this->tdbmSchemaAnalyzer, $this->getNamingStrategy(), AnnotationParser::buildWithDefaultAnnotations([]), new BaseCodeGeneratorListener(), $this->getConfiguration());
78
        $constructorPropertyDescriptors = $beanDescriptor->getConstructorProperties();
79
        $this->assertArrayHasKey('$name', $constructorPropertyDescriptors);
80
        // password is nullable
81
        $this->assertArrayNotHasKey('$password', $constructorPropertyDescriptors);
82
        // id is autoincremented
83
        $this->assertArrayNotHasKey('$id', $constructorPropertyDescriptors);
84
    }
85
86
    public function testGetTable(): void
87
    {
88
        $usersTable = $this->schema->getTable('users');
89
        $beanDescriptor = new BeanDescriptor($usersTable, 'Tdbm\\Test\\Beans', 'Tdbm\\Test\\Beans\\Generated', 'Tdbm\\Test\\Daos', 'Tdbm\\Test\\Daos\\Generated', $this->schemaAnalyzer, $this->schema, $this->tdbmSchemaAnalyzer, $this->getNamingStrategy(), AnnotationParser::buildWithDefaultAnnotations([]), new BaseCodeGeneratorListener(), $this->getConfiguration());
90
        $this->assertSame($usersTable, $beanDescriptor->getTable());
91
    }
92
93
    public function testTableWithNoPrimaryKey(): void
94
    {
95
        $table = new Table('no_primary_key');
96
        $this->expectException(TDBMException::class);
97
        $this->expectExceptionMessage('Table "no_primary_key" does not have any primary key');
98
        new BeanDescriptor($table, 'Foo\\Bar', 'Foo\\Generated\\Bar', 'Tdbm\\Test\\Daos', 'Tdbm\\Test\\Daos\\Generated', $this->schemaAnalyzer, $this->schema, $this->tdbmSchemaAnalyzer, $this->getNamingStrategy(), AnnotationParser::buildWithDefaultAnnotations([]), new BaseCodeGeneratorListener(), $this->getConfiguration());
99
    }
100
101
    public function testTableWithLazyLoadingColumn(): void
102
    {
103
        $table = new Table('lazy_loading');
104
        $table->addColumn('lazyLoading', 'bool');
105
        $this->expectException(TDBMException::class);
106
        $this->expectExceptionMessage('Primary Column name `lazyLoading` is not allowed.');
107
        new BeanDescriptor($table, 'Foo\\Bar', 'Foo\\Generated\\Bar', 'Tdbm\\Test\\Daos', 'Tdbm\\Test\\Daos\\Generated', $this->schemaAnalyzer, $this->schema, $this->tdbmSchemaAnalyzer, $this->getNamingStrategy(), AnnotationParser::buildWithDefaultAnnotations([]), new BaseCodeGeneratorListener(), $this->getConfiguration());
108
    }
109
110
    /*public function testGeneratePhpCode() {
111
        $usersTable = $this->schema->getTable("users");
112
        $beanDescriptor = new BeanDescriptor($usersTable, $this->schemaAnalyzer, $this->schema);
113
        $phpCode = $beanDescriptor->generatePhpCode("MyNamespace\\");
114
115
        echo $phpCode;
116
    }*/
117
}
118