1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
Copyright (C) 2006-2014 David Négrier - THE CODING MACHINE |
5
|
|
|
|
6
|
|
|
This program is free software; you can redistribute it and/or modify |
7
|
|
|
it under the terms of the GNU General Public License as published by |
8
|
|
|
the Free Software Foundation; either version 2 of the License, or |
9
|
|
|
(at your option) any later version. |
10
|
|
|
|
11
|
|
|
This program is distributed in the hope that it will be useful, |
12
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
GNU General Public License for more details. |
15
|
|
|
|
16
|
|
|
You should have received a copy of the GNU General Public License |
17
|
|
|
along with this program; if not, write to the Free Software |
18
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TheCodingMachine\TDBM\Utils; |
22
|
|
|
|
23
|
|
|
use Doctrine\Common\Cache\VoidCache; |
24
|
|
|
use Doctrine\DBAL\Schema\Schema; |
25
|
|
|
use Doctrine\DBAL\Schema\Table; |
26
|
|
|
use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer; |
27
|
|
|
use TheCodingMachine\TDBM\TDBMAbstractServiceTest; |
28
|
|
|
use TheCodingMachine\TDBM\TDBMException; |
29
|
|
|
use TheCodingMachine\TDBM\TDBMSchemaAnalyzer; |
30
|
|
|
|
31
|
|
|
class BeanDescriptorTest extends TDBMAbstractServiceTest |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var Schema |
35
|
|
|
*/ |
36
|
|
|
protected $schema; |
37
|
|
|
/** |
38
|
|
|
* @var SchemaAnalyzer |
39
|
|
|
*/ |
40
|
|
|
protected $schemaAnalyzer; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var TDBMSchemaAnalyzer |
44
|
|
|
*/ |
45
|
|
|
protected $tdbmSchemaAnalyzer; |
46
|
|
|
|
47
|
|
|
protected function setUp() |
48
|
|
|
{ |
49
|
|
|
parent::setUp(); |
50
|
|
|
$schemaManager = $this->tdbmService->getConnection()->getSchemaManager(); |
51
|
|
|
$this->schemaAnalyzer = new SchemaAnalyzer($schemaManager); |
52
|
|
|
$this->schema = $schemaManager->createSchema(); |
53
|
|
|
$this->tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($this->tdbmService->getConnection(), new VoidCache(), $this->schemaAnalyzer); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testConstructor() |
57
|
|
|
{ |
58
|
|
|
$usersTable = $this->schema->getTable('users'); |
59
|
|
|
$beanDescriptor = new BeanDescriptor($usersTable, 'Tdbm\\Test\\Beans', 'Tdbm\\Test\\Beans\\Generated', $this->schemaAnalyzer, $this->schema, $this->tdbmSchemaAnalyzer, new DefaultNamingStrategy()); |
60
|
|
|
$propertyDescriptors = $beanDescriptor->getBeanPropertyDescriptors(); |
61
|
|
|
$firstElem = reset($propertyDescriptors); |
62
|
|
|
$idProperty = $propertyDescriptors['$id']; |
63
|
|
|
$this->assertEquals($firstElem, $idProperty); |
64
|
|
|
$this->assertEquals('person', $idProperty->getTable()->getName()); |
65
|
|
|
$this->assertInstanceOf('TheCodingMachine\\TDBM\\Utils\\ScalarBeanPropertyDescriptor', $idProperty); |
66
|
|
|
$countryProperty = $propertyDescriptors['$country']; |
67
|
|
|
$this->assertInstanceOf('TheCodingMachine\\TDBM\\Utils\\ObjectBeanPropertyDescriptor', $countryProperty); |
68
|
|
|
$nameProperty = $propertyDescriptors['$name']; |
69
|
|
|
$this->assertInstanceOf('TheCodingMachine\\TDBM\\Utils\\ScalarBeanPropertyDescriptor', $nameProperty); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testGetConstructorProperties() |
73
|
|
|
{ |
74
|
|
|
$usersTable = $this->schema->getTable('users'); |
75
|
|
|
$beanDescriptor = new BeanDescriptor($usersTable, 'Tdbm\\Test\\Beans', 'Tdbm\\Test\\Beans\\Generated', $this->schemaAnalyzer, $this->schema, $this->tdbmSchemaAnalyzer, new DefaultNamingStrategy()); |
76
|
|
|
$constructorPropertyDescriptors = $beanDescriptor->getConstructorProperties(); |
77
|
|
|
$this->assertArrayHasKey('$name', $constructorPropertyDescriptors); |
78
|
|
|
// password is nullable |
79
|
|
|
$this->assertArrayNotHasKey('$password', $constructorPropertyDescriptors); |
80
|
|
|
// id is autoincremented |
81
|
|
|
$this->assertArrayNotHasKey('$id', $constructorPropertyDescriptors); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testGetTable() |
85
|
|
|
{ |
86
|
|
|
$usersTable = $this->schema->getTable('users'); |
87
|
|
|
$beanDescriptor = new BeanDescriptor($usersTable, 'Tdbm\\Test\\Beans', 'Tdbm\\Test\\Beans\\Generated', $this->schemaAnalyzer, $this->schema, $this->tdbmSchemaAnalyzer, new DefaultNamingStrategy()); |
88
|
|
|
$this->assertSame($usersTable, $beanDescriptor->getTable()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testTableWithNoPrimaryKey() |
92
|
|
|
{ |
93
|
|
|
$table = new Table('no_primary_key'); |
94
|
|
|
$this->expectException(TDBMException::class); |
95
|
|
|
$this->expectExceptionMessage('Table "no_primary_key" does not have any primary key'); |
96
|
|
|
new BeanDescriptor($table, 'Foo\\Bar', 'Foo\\Generated\\Bar', $this->schemaAnalyzer, $this->schema, $this->tdbmSchemaAnalyzer, new DefaultNamingStrategy()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/*public function testGeneratePhpCode() { |
|
|
|
|
100
|
|
|
$usersTable = $this->schema->getTable("users"); |
101
|
|
|
$beanDescriptor = new BeanDescriptor($usersTable, $this->schemaAnalyzer, $this->schema); |
102
|
|
|
$phpCode = $beanDescriptor->generatePhpCode("MyNamespace\\"); |
103
|
|
|
|
104
|
|
|
echo $phpCode; |
105
|
|
|
}*/ |
106
|
|
|
} |
107
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.