Passed
Pull Request — master (#96)
by David
02:58
created

  A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
dl 0
loc 15
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace TheCodingMachine\TDBM\Utils;
5
6
use Doctrine\DBAL\Platforms\AbstractPlatform;
7
use Doctrine\DBAL\Platforms\MySqlPlatform;
8
use Doctrine\DBAL\Schema\AbstractSchemaManager;
9
use Doctrine\DBAL\Schema\MySqlSchemaManager;
10
use Doctrine\DBAL\Schema\Schema;
11
use Doctrine\DBAL\Schema\Table;
12
use TheCodingMachine\TDBM\TDBMAbstractServiceTest;
13
use TheCodingMachine\TDBM\Utils\Annotation\AnnotationParser;
14
15
class DefaultNamingStrategyTest extends TDBMAbstractServiceTest
16
{
17
    private function getDefaultNamingStrategy()
18
    {
19
        return new DefaultNamingStrategy(AnnotationParser::buildWithDefaultAnnotations([]), $this->getConnection()->getSchemaManager());
20
    }
21
22
    /**
23
     * @param Table[] $tables
24
     * @return DefaultNamingStrategy
25
     */
26
    private function getDefaultNamingStrategyWithStubTables(array $tables)
27
    {
28
        $stubSchemaManager = new class($tables, self::getConnection(), new MySqlPlatform()) extends MySqlSchemaManager {
29
            private $tables;
30
31
            /**
32
             * @param Table[] $tables
33
             */
34
            public function __construct(array $tables, \Doctrine\DBAL\Connection $conn, AbstractPlatform $platform = null)
35
            {
36
                parent::__construct($conn, $platform);
37
                $this->tables = $tables;
38
            }
39
40
            public function createSchema()
41
            {
42
                return new Schema($this->tables, [], $this->createSchemaConfig(), []);
43
            }
44
        };
45
46
        return new DefaultNamingStrategy(AnnotationParser::buildWithDefaultAnnotations([]), $stubSchemaManager);
47
    }
48
49
    public function testGetBeanName()
50
    {
51
        $strategy = $this->getDefaultNamingStrategy();
52
        $strategy->setBeanPrefix('');
53
        $strategy->setBeanSuffix('Bean');
54
55
        $this->assertSame('UserBean', $strategy->getBeanClassName("users"));
56
57
58
        $strategy2 = $this->getDefaultNamingStrategyWithStubTables([new Table('user'), new Table('users_countries'), new Table('users countries')]);
59
        $strategy2->setBeanPrefix('');
60
        $strategy2->setBeanSuffix('Bean');
61
        $this->assertSame('UserBean', $strategy2->getBeanClassName("user"));
62
        $this->assertSame('UserCountryBean', $strategy2->getBeanClassName("users_countries"));
63
        $this->assertSame('UserCountryBean', $strategy2->getBeanClassName("users countries"));
64
    }
65
66
    public function testGetBaseBeanName()
67
    {
68
        $strategy = $this->getDefaultNamingStrategy();
69
        $strategy->setBaseBeanPrefix('');
70
        $strategy->setBaseBeanSuffix('BaseBean');
71
        $this->assertSame('UserBaseBean', $strategy->getBaseBeanClassName("users"));
72
    }
73
74
    public function testGetDaoName()
75
    {
76
        $strategy = $this->getDefaultNamingStrategy();
77
        $strategy->setDaoPrefix('');
78
        $strategy->setDaoSuffix('Dao');
79
        $this->assertSame('UserDao', $strategy->getDaoClassName("users"));
80
    }
81
82
    public function testGetBaseDaoName()
83
    {
84
        $strategy = $this->getDefaultNamingStrategy();
85
        $strategy->setBaseDaoPrefix('');
86
        $strategy->setBaseDaoSuffix('BaseDao');
87
        $this->assertSame('UserBaseDao', $strategy->getBaseDaoClassName("users"));
88
    }
89
90
    public function testGetBeanNameDefault()
91
    {
92
        $strategy = $this->getDefaultNamingStrategy();
93
94
        $this->assertSame('User', $strategy->getBeanClassName("users"));
95
    }
96
97
    public function testGetBaseBeanNameDefault()
98
    {
99
        $strategy = $this->getDefaultNamingStrategy();
100
        $this->assertSame('AbstractUser', $strategy->getBaseBeanClassName("users"));
101
    }
102
103
    public function testGetDaoNameDefault()
104
    {
105
        $strategy = $this->getDefaultNamingStrategy();
106
        $this->assertSame('UserDao', $strategy->getDaoClassName("users"));
107
    }
108
109
    public function testGetBaseDaoNameDefault()
110
    {
111
        $strategy = $this->getDefaultNamingStrategy();
112
        $this->assertSame('AbstractUserDao', $strategy->getBaseDaoClassName("users"));
113
    }
114
115
    public function testGetDaoFactory()
116
    {
117
        $strategy = $this->getDefaultNamingStrategy();
118
        $this->assertSame('DaoFactory', $strategy->getDaoFactoryClassName());
119
    }
120
121
    public function testExceptions()
122
    {
123
        $table = new Table('chevaux');
124
        $strategy = $this->getDefaultNamingStrategyWithStubTables([$table]);
125
        $strategy->setExceptions([
126
            'chevaux' => 'Cheval'
127
        ]);
128
        $this->assertSame('ChevalDao', $strategy->getDaoClassName('chevaux'));
129
    }
130
131
    public function testBeanAnnotation()
132
    {
133
        $table = new Table('chevaux', [], [], [], 0, ['comment'=>'@Bean(name="Cheval")']);
134
        $strategy = $this->getDefaultNamingStrategyWithStubTables([$table]);
135
        $this->assertSame('ChevalDao', $strategy->getDaoClassName('chevaux'));
136
    }
137
138
    public function testUppercaseNames()
139
    {
140
        $strategy = $this->getDefaultNamingStrategy();
141
        $strategy->setDaoPrefix('');
142
        $strategy->setDaoSuffix('Dao');
143
        $this->assertSame('UserDao', $strategy->getDaoClassName("USERS"));
144
    }
145
}
146