Passed
Pull Request — master (#153)
by
unknown
07:39 queued 04:49
created

BeanDescriptorTest::testTableWithNoPrimaryKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
rs 10
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 Doctrine\DBAL\Types\Type;
28
use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer;
29
use TheCodingMachine\TDBM\TDBMAbstractServiceTest;
30
use TheCodingMachine\TDBM\TDBMException;
31
use TheCodingMachine\TDBM\TDBMSchemaAnalyzer;
32
use TheCodingMachine\TDBM\Utils\Annotation\AnnotationParser;
33
34
class BeanDescriptorTest extends TDBMAbstractServiceTest
35
{
36
    /**
37
     * @var Schema
38
     */
39
    protected $schema;
40
    /**
41
     * @var SchemaAnalyzer
42
     */
43
    protected $schemaAnalyzer;
44
45
    /**
46
     * @var TDBMSchemaAnalyzer
47
     */
48
    protected $tdbmSchemaAnalyzer;
49
50
    protected function setUp(): void
51
    {
52
        parent::setUp();
53
        $schemaManager = $this->tdbmService->getConnection()->getSchemaManager();
54
        $this->schemaAnalyzer = new SchemaAnalyzer($schemaManager);
55
        $this->schema = $schemaManager->createSchema();
56
        $this->tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($this->tdbmService->getConnection(), new VoidCache(), $this->schemaAnalyzer);
57
    }
58
59
    public function testConstructor(): void
60
    {
61
        $usersTable = $this->schema->getTable('users');
62
        $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());
63
        $propertyDescriptors = $beanDescriptor->getBeanPropertyDescriptors();
64
        $firstElem = reset($propertyDescriptors);
65
        $idProperty = $propertyDescriptors['$id'];
66
        $this->assertEquals($firstElem, $idProperty);
67
        $this->assertEquals('person', $idProperty->getTable()->getName());
68
        $this->assertInstanceOf('TheCodingMachine\\TDBM\\Utils\\ScalarBeanPropertyDescriptor', $idProperty);
69
        $countryProperty = $propertyDescriptors['$country'];
70
        $this->assertInstanceOf('TheCodingMachine\\TDBM\\Utils\\ObjectBeanPropertyDescriptor', $countryProperty);
71
        $nameProperty = $propertyDescriptors['$name'];
72
        $this->assertInstanceOf('TheCodingMachine\\TDBM\\Utils\\ScalarBeanPropertyDescriptor', $nameProperty);
73
    }
74
75
    public function testGetConstructorProperties(): void
76
    {
77
        $usersTable = $this->schema->getTable('users');
78
        $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());
79
        $constructorPropertyDescriptors = $beanDescriptor->getConstructorProperties();
80
        $this->assertArrayHasKey('$name', $constructorPropertyDescriptors);
81
        // password is nullable
82
        $this->assertArrayNotHasKey('$password', $constructorPropertyDescriptors);
83
        // id is autoincremented
84
        $this->assertArrayNotHasKey('$id', $constructorPropertyDescriptors);
85
    }
86
87
    public function testGetTable(): void
88
    {
89
        $usersTable = $this->schema->getTable('users');
90
        $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());
91
        $this->assertSame($usersTable, $beanDescriptor->getTable());
92
    }
93
94
    public function testTableWithNoPrimaryKey(): void
95
    {
96
        $table = new Table('no_primary_key');
97
        $this->expectException(TDBMException::class);
98
        $this->expectExceptionMessage('Table "no_primary_key" does not have any primary key');
99
        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());
100
    }
101
102
    public function testTableWithLazyLoadingColumn(): void
103
    {
104
        $table = $this->schema->createTable('lazy_loading');
105
        $table->addColumn('lazyLoading', Type::BOOLEAN);
106
        $table->setPrimaryKey(['lazyLoading']);
107
        $sqlStmts = $this->schema->getMigrateFromSql($this->getConnection()->getSchemaManager()->createSchema(), $this->getConnection()->getDatabasePlatform());
108
109
        foreach ($sqlStmts as $sqlStmt) {
110
            $this->getConnection()->exec($sqlStmt);
111
        }
112
113
        $this->expectException(TDBMException::class);
114
        $this->expectExceptionMessage('Primary Column name `lazyLoading` is not allowed.');
115
        $beanDescriptor = 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());
116
        $beanDescriptor->generateDaoPhpCode();
117
    }
118
119
    /*public function testGeneratePhpCode() {
120
        $usersTable = $this->schema->getTable("users");
121
        $beanDescriptor = new BeanDescriptor($usersTable, $this->schemaAnalyzer, $this->schema);
122
        $phpCode = $beanDescriptor->generatePhpCode("MyNamespace\\");
123
124
        echo $phpCode;
125
    }*/
126
}
127