thecodingmachine /
tdbm
| 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
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 |