A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
c 1
b 0
f 0
dl 0
loc 15
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TheCodingMachine\TDBM\Utils;
6
7
use Doctrine\DBAL\Platforms\AbstractPlatform;
8
use Doctrine\DBAL\Platforms\MySQLPlatform;
9
use Doctrine\DBAL\Schema\AbstractSchemaManager;
10
use Doctrine\DBAL\Schema\MySqlSchemaManager;
11
use Doctrine\DBAL\Schema\Schema;
12
use Doctrine\DBAL\Schema\Table;
13
use TheCodingMachine\TDBM\TDBMAbstractServiceTest;
14
use TheCodingMachine\TDBM\Utils\Annotation\AnnotationParser;
15
16
class DefaultNamingStrategyTest extends TDBMAbstractServiceTest
17
{
18
    private function getDefaultNamingStrategy()
19
    {
20
        return new DefaultNamingStrategy(AnnotationParser::buildWithDefaultAnnotations([]), $this->getConnection()->createSchemaManager());
21
    }
22
23
    /**
24
     * @param Table[] $tables
25
     * @return DefaultNamingStrategy
26
     */
27
    private function getDefaultNamingStrategyWithStubTables(array $tables)
28
    {
29
        $stubSchemaManager = new class ($tables, self::getConnection(), new MySqlPlatform()) extends MySqlSchemaManager {
30
            private $tables;
31
32
            /**
33
             * @param Table[] $tables
34
             */
35
            public function __construct(array $tables, \Doctrine\DBAL\Connection $conn, AbstractPlatform $platform = null)
36
            {
37
                parent::__construct($conn, $platform);
0 ignored issues
show
Bug introduced by
It seems like $platform can also be of type null; however, parameter $platform of Doctrine\DBAL\Schema\Abs...aManager::__construct() does only seem to accept Doctrine\DBAL\Platforms\AbstractPlatform, maybe add an additional type check? ( Ignorable by Annotation )

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

37
                parent::__construct($conn, /** @scrutinizer ignore-type */ $platform);
Loading history...
38
                $this->tables = $tables;
39
            }
40
41
            public function createSchema()
42
            {
43
                return new Schema($this->tables, [], $this->createSchemaConfig(), []);
44
            }
45
        };
46
47
        return new DefaultNamingStrategy(AnnotationParser::buildWithDefaultAnnotations([]), $stubSchemaManager);
48
    }
49
50
    public function testGetBeanName(): void
51
    {
52
        $strategy = $this->getDefaultNamingStrategy();
53
        $strategy->setBeanPrefix('');
54
        $strategy->setBeanSuffix('Bean');
55
56
        $this->assertSame('UserBean', $strategy->getBeanClassName("users"));
57
58
59
        $strategy2 = $this->getDefaultNamingStrategyWithStubTables([new Table('user'), new Table('users_countries'), new Table('users countries')]);
60
        $strategy2->setBeanPrefix('');
61
        $strategy2->setBeanSuffix('Bean');
62
        $this->assertSame('UserBean', $strategy2->getBeanClassName("user"));
63
        $this->assertSame('UserCountryBean', $strategy2->getBeanClassName("users_countries"));
64
        $this->assertSame('UserCountryBean', $strategy2->getBeanClassName("users countries"));
65
    }
66
67
    public function testGetBaseBeanName(): void
68
    {
69
        $strategy = $this->getDefaultNamingStrategy();
70
        $strategy->setBaseBeanPrefix('');
71
        $strategy->setBaseBeanSuffix('BaseBean');
72
        $this->assertSame('UserBaseBean', $strategy->getBaseBeanClassName("users"));
73
    }
74
75
    public function testGetDaoName(): void
76
    {
77
        $strategy = $this->getDefaultNamingStrategy();
78
        $strategy->setDaoPrefix('');
79
        $strategy->setDaoSuffix('Dao');
80
        $this->assertSame('UserDao', $strategy->getDaoClassName("users"));
81
    }
82
83
    public function testGetBaseDaoName(): void
84
    {
85
        $strategy = $this->getDefaultNamingStrategy();
86
        $strategy->setBaseDaoPrefix('');
87
        $strategy->setBaseDaoSuffix('BaseDao');
88
        $this->assertSame('UserBaseDao', $strategy->getBaseDaoClassName("users"));
89
    }
90
91
    public function testGetBeanNameDefault(): void
92
    {
93
        $strategy = $this->getDefaultNamingStrategy();
94
95
        $this->assertSame('User', $strategy->getBeanClassName("users"));
96
    }
97
98
    public function testGetBaseBeanNameDefault(): void
99
    {
100
        $strategy = $this->getDefaultNamingStrategy();
101
        $this->assertSame('AbstractUser', $strategy->getBaseBeanClassName("users"));
102
    }
103
104
    public function testGetDaoNameDefault(): void
105
    {
106
        $strategy = $this->getDefaultNamingStrategy();
107
        $this->assertSame('UserDao', $strategy->getDaoClassName("users"));
108
    }
109
110
    public function testGetBaseDaoNameDefault(): void
111
    {
112
        $strategy = $this->getDefaultNamingStrategy();
113
        $this->assertSame('AbstractUserDao', $strategy->getBaseDaoClassName("users"));
114
    }
115
116
    public function testGetDaoFactory(): void
117
    {
118
        $strategy = $this->getDefaultNamingStrategy();
119
        $this->assertSame('DaoFactory', $strategy->getDaoFactoryClassName());
120
    }
121
122
    public function testExceptions(): void
123
    {
124
        $table = new Table('chevaux');
125
        $strategy = $this->getDefaultNamingStrategyWithStubTables([$table]);
126
        $strategy->setExceptions([
127
            'chevaux' => 'Cheval'
128
        ]);
129
        $this->assertSame('ChevalDao', $strategy->getDaoClassName('chevaux'));
130
    }
131
132
    public function testBeanAnnotation(): void
133
    {
134
        $table = new Table('chevaux', [], [], [], [], ['comment' => '@Bean(name="Cheval")']);
135
        $strategy = $this->getDefaultNamingStrategyWithStubTables([$table]);
136
        $this->assertSame('ChevalDao', $strategy->getDaoClassName('chevaux'));
137
    }
138
139
    public function testUppercaseNames(): void
140
    {
141
        $strategy = $this->getDefaultNamingStrategy();
142
        $strategy->setDaoPrefix('');
143
        $strategy->setDaoSuffix('Dao');
144
        $this->assertSame('UserDao', $strategy->getDaoClassName("USERS"));
145
    }
146
}
147