Issues (291)

tests/Utils/BeanDescriptorTest.php (8 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 Copyright (C) 2006-2014 David Négrier - THE CODING MACHINE
7
8
This program is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 2 of the License, or
11
(at your option) any later version.
12
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
GNU General Public License for more details.
17
18
You should have received a copy of the GNU General Public License
19
along with this program; if not, write to the Free Software
20
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21
*/
22
23
namespace TheCodingMachine\TDBM\Utils;
24
25
use Doctrine\Common\Cache\ArrayCache;
26
use Doctrine\Common\Cache\VoidCache;
27
use Doctrine\DBAL\Schema\Schema;
28
use Doctrine\DBAL\Schema\Table;
29
use Doctrine\DBAL\Types\Type;
30
use Doctrine\DBAL\Types\Types;
31
use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer;
32
use TheCodingMachine\TDBM\Configuration;
33
use TheCodingMachine\TDBM\SchemaLockFileDumper;
34
use TheCodingMachine\TDBM\TDBMAbstractServiceTest;
35
use TheCodingMachine\TDBM\TDBMException;
36
use TheCodingMachine\TDBM\TDBMSchemaAnalyzer;
37
use TheCodingMachine\TDBM\Utils\Annotation\AnnotationParser;
38
39
class BeanDescriptorTest extends TDBMAbstractServiceTest
40
{
41
    /**
42
     * @var Schema
43
     */
44
    protected $schema;
45
    /**
46
     * @var SchemaAnalyzer
47
     */
48
    protected $schemaAnalyzer;
49
50
    /**
51
     * @var TDBMSchemaAnalyzer
52
     */
53
    protected $tdbmSchemaAnalyzer;
54
55
    protected function setUp(): void
56
    {
57
        parent::setUp();
58
        $schemaManager = $this->tdbmService->getConnection()->getSchemaManager();
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Connection::getSchemaManager() has been deprecated: Use {@see createSchemaManager()} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

58
        $schemaManager = /** @scrutinizer ignore-deprecated */ $this->tdbmService->getConnection()->getSchemaManager();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
59
        $this->schemaAnalyzer = new SchemaAnalyzer($schemaManager);
60
        $this->schema = $schemaManager->createSchema();
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\Abs...Manager::createSchema() has been deprecated: Use {@link introspectSchema()} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

60
        $this->schema = /** @scrutinizer ignore-deprecated */ $schemaManager->createSchema();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
61
        $schemaLockFileDumper = new SchemaLockFileDumper($this->tdbmService->getConnection(), new VoidCache(), Configuration::getDefaultLockFilePath());
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Cache\VoidCache has been deprecated: Deprecated without replacement in doctrine/cache 1.11. This class will be dropped in 2.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

61
        $schemaLockFileDumper = new SchemaLockFileDumper($this->tdbmService->getConnection(), /** @scrutinizer ignore-deprecated */ new VoidCache(), Configuration::getDefaultLockFilePath());
Loading history...
62
        $this->tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($this->tdbmService->getConnection(), new VoidCache(), $this->schemaAnalyzer, $schemaLockFileDumper);
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Cache\VoidCache has been deprecated: Deprecated without replacement in doctrine/cache 1.11. This class will be dropped in 2.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

62
        $this->tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($this->tdbmService->getConnection(), /** @scrutinizer ignore-deprecated */ new VoidCache(), $this->schemaAnalyzer, $schemaLockFileDumper);
Loading history...
63
    }
64
65
    public function testConstructor(): void
66
    {
67
        $usersTable = $this->schema->getTable('users');
68
        $beanDescriptor = new BeanDescriptor($usersTable, 'Tdbm\\Test\\Beans', 'Tdbm\\Test\\Beans\\Generated', 'Tdbm\\Test\\Daos', 'Tdbm\\Test\\Daos\\Generated', 'Tdbm\\Test\\ResultIterators', 'Tdbm\\Test\\ResultIterators\\Generated', $this->schemaAnalyzer, $this->schema, $this->tdbmSchemaAnalyzer, $this->getNamingStrategy(), AnnotationParser::buildWithDefaultAnnotations([]), new BaseCodeGeneratorListener(), $this->getConfiguration(), $this->createMock(BeanRegistry::class));
69
        $beanDescriptor->initBeanPropertyDescriptors();
70
        $propertyDescriptors = $beanDescriptor->getBeanPropertyDescriptors();
71
        $firstElem = reset($propertyDescriptors);
72
        $idProperty = $propertyDescriptors['$id'];
73
        $this->assertEquals($firstElem, $idProperty);
74
        $this->assertEquals('person', $idProperty->getTable()->getName());
75
        $this->assertInstanceOf('TheCodingMachine\\TDBM\\Utils\\ScalarBeanPropertyDescriptor', $idProperty);
76
        $countryProperty = $propertyDescriptors['$country'];
77
        $this->assertInstanceOf('TheCodingMachine\\TDBM\\Utils\\ObjectBeanPropertyDescriptor', $countryProperty);
78
        $nameProperty = $propertyDescriptors['$name'];
79
        $this->assertInstanceOf('TheCodingMachine\\TDBM\\Utils\\ScalarBeanPropertyDescriptor', $nameProperty);
80
    }
81
82
    public function testGetConstructorProperties(): void
83
    {
84
        $usersTable = $this->schema->getTable('users');
85
        $beanDescriptor = new BeanDescriptor($usersTable, 'Tdbm\\Test\\Beans', 'Tdbm\\Test\\Beans\\Generated', 'Tdbm\\Test\\Daos', 'Tdbm\\Test\\Daos\\Generated', 'Tdbm\\Test\\ResultIterators', 'Tdbm\\Test\\ResultIterators\\Generated', $this->schemaAnalyzer, $this->schema, $this->tdbmSchemaAnalyzer, $this->getNamingStrategy(), AnnotationParser::buildWithDefaultAnnotations([]), new BaseCodeGeneratorListener(), $this->getConfiguration(), $this->createMock(BeanRegistry::class));
86
        $beanDescriptor->initBeanPropertyDescriptors();
87
        $constructorPropertyDescriptors = $beanDescriptor->getConstructorProperties();
88
        $this->assertArrayHasKey('$name', $constructorPropertyDescriptors);
89
        // password is nullable
90
        $this->assertArrayNotHasKey('$password', $constructorPropertyDescriptors);
91
        // id is autoincremented
92
        $this->assertArrayNotHasKey('$id', $constructorPropertyDescriptors);
93
    }
94
95
    public function testGetTable(): void
96
    {
97
        $usersTable = $this->schema->getTable('users');
98
        $beanDescriptor = new BeanDescriptor($usersTable, 'Tdbm\\Test\\Beans', 'Tdbm\\Test\\Beans\\Generated', 'Tdbm\\Test\\Daos', 'Tdbm\\Test\\Daos\\Generated', 'Tdbm\\Test\\ResultIterators', 'Tdbm\\Test\\ResultIterators\\Generated', $this->schemaAnalyzer, $this->schema, $this->tdbmSchemaAnalyzer, $this->getNamingStrategy(), AnnotationParser::buildWithDefaultAnnotations([]), new BaseCodeGeneratorListener(), $this->getConfiguration(), $this->createMock(BeanRegistry::class));
99
        $this->assertSame($usersTable, $beanDescriptor->getTable());
100
    }
101
102
    public function testTableWithNoPrimaryKey(): void
103
    {
104
        $table = new Table('no_primary_key');
105
        $beanDescriptor = new BeanDescriptor($table, 'Foo\\Bar', 'Foo\\Generated\\Bar', 'Tdbm\\Test\\Daos', 'Tdbm\\Test\\Daos\\Generated', 'Tdbm\\Test\\ResultIterators', 'Tdbm\\Test\\ResultIterators\\Generated', $this->schemaAnalyzer, $this->schema, $this->tdbmSchemaAnalyzer, $this->getNamingStrategy(), AnnotationParser::buildWithDefaultAnnotations([]), new BaseCodeGeneratorListener(), $this->getConfiguration(), $this->createMock(BeanRegistry::class));
106
        $this->expectException(TDBMException::class);
107
        $this->expectExceptionMessage('Table "no_primary_key" does not have any primary key');
108
        $beanDescriptor->initBeanPropertyDescriptors();
109
    }
110
111
    public function testTableWithLazyLoadingColumn(): void
112
    {
113
        $table = $this->schema->createTable('lazy_loading');
114
        $table->addColumn('lazyLoading', Types::BOOLEAN);
115
        $table->setPrimaryKey(['lazyLoading']);
116
        $sqlStmts = $this->schema->getMigrateFromSql($this->getConnection()->getSchemaManager()->createSchema(), $this->getConnection()->getDatabasePlatform());
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Connection::getSchemaManager() has been deprecated: Use {@see createSchemaManager()} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

116
        $sqlStmts = $this->schema->getMigrateFromSql(/** @scrutinizer ignore-deprecated */ $this->getConnection()->getSchemaManager()->createSchema(), $this->getConnection()->getDatabasePlatform());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\Abs...Manager::createSchema() has been deprecated: Use {@link introspectSchema()} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

116
        $sqlStmts = $this->schema->getMigrateFromSql(/** @scrutinizer ignore-deprecated */ $this->getConnection()->getSchemaManager()->createSchema(), $this->getConnection()->getDatabasePlatform());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\Schema::getMigrateFromSql() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

116
        $sqlStmts = /** @scrutinizer ignore-deprecated */ $this->schema->getMigrateFromSql($this->getConnection()->getSchemaManager()->createSchema(), $this->getConnection()->getDatabasePlatform());
Loading history...
117
118
        foreach ($sqlStmts as $sqlStmt) {
119
            $this->getConnection()->exec($sqlStmt);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Connection::exec() has been deprecated: please use {@see executeStatement()} instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

119
            /** @scrutinizer ignore-deprecated */ $this->getConnection()->exec($sqlStmt);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
120
        }
121
122
        $this->expectException(TDBMException::class);
123
        $this->expectExceptionMessage('Primary Column name `lazyLoading` is not allowed.');
124
        $beanDescriptor = new BeanDescriptor($table, 'Foo\\Bar', 'Foo\\Generated\\Bar', 'Tdbm\\Test\\Daos', 'Tdbm\\Test\\Daos\\Generated', 'Tdbm\\Test\\ResultIterators', 'Tdbm\\Test\\ResultIterators\\Generated', $this->schemaAnalyzer, $this->schema, $this->tdbmSchemaAnalyzer, $this->getNamingStrategy(), AnnotationParser::buildWithDefaultAnnotations([]), new BaseCodeGeneratorListener(), $this->getConfiguration(), $this->createMock(BeanRegistry::class));
125
        $beanDescriptor->generateDaoPhpCode();
126
    }
127
128
    /*public function testGeneratePhpCode() {
129
        $usersTable = $this->schema->getTable("users");
130
        $beanDescriptor = new BeanDescriptor($usersTable, $this->schemaAnalyzer, $this->schema);
131
        $phpCode = $beanDescriptor->generatePhpCode("MyNamespace\\");
132
133
        echo $phpCode;
134
    }*/
135
}
136