Skip to content

Commit 4d931cd

Browse files
committed
Fixed tests and improved error messages
1 parent 23c6728 commit 4d931cd

File tree

11 files changed

+56
-122
lines changed

11 files changed

+56
-122
lines changed

src/Command/SqlGeneratorCommand.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,10 @@ private static function getDefault(\ReflectionProperty $property, \ReflectionCla
239239
if (!$property->hasDefaultValue()) {
240240
$primaryKeys = self::getPrimaryKeyColumns($bean);
241241

242-
if (\count($primaryKeys) === 1 && $property->getName() === $primaryKeys[0]) {
243-
if (\in_array($type->getName(), ['int', \CoolBeans\PrimaryKey\IntPrimaryKey::class], true)) {
244-
return ' AUTO_INCREMENT';
245-
}
242+
if (\count($primaryKeys) === 1 &&
243+
$property->getName() === $primaryKeys[0] &&
244+
\in_array($type->getName(), ['int', \CoolBeans\PrimaryKey\IntPrimaryKey::class], true)) {
245+
return ' AUTO_INCREMENT';
246246
}
247247

248248
return '';
@@ -467,11 +467,8 @@ private static function getPrimaryKeyColumns(\ReflectionClass $bean) : array
467467

468468
if (\count($attributes) > 0) {
469469
$primaryColumns = $attributes[0]->newInstance()->columns;
470-
471-
if (\count($primaryColumns) === 0) {
472-
throw new \CoolBeans\Exception\MissingPrimaryKey('Bean ' . $bean->getShortName() . ' has no primary key.');
473-
}
474470
} else {
471+
// default
475472
$primaryColumns = ['id'];
476473
}
477474

@@ -489,15 +486,19 @@ private static function printPrimaryKey(\ReflectionClass $bean) : string
489486

490487
private static function validateColumnsExists(\ReflectionClass $bean, array $columns) : void
491488
{
489+
if (\count($columns) === 0) {
490+
throw new \CoolBeans\Exception\EmptyColumnArray($bean->getShortName());
491+
}
492+
492493
if (\count($columns) !== \count(\array_flip($columns))) {
493-
throw new \RuntimeException('Column array has duplicates in Bean ' . $bean->getShortName() . '.');
494+
throw new \CoolBeans\Exception\DuplicateInColumnArray($bean->getShortName());
494495
}
495496

496497
foreach ($columns as $column) {
497498
try {
498499
$property = $bean->getProperty($column);
499500
} catch (\ReflectionException) {
500-
throw new \RuntimeException('Column [' . $column . '] given in column array doesnt exist in Bean ' . $bean->getShortName() . '.');
501+
throw new \CoolBeans\Exception\UnknownColumnInColumnArray($column, $bean->getShortName());
501502
}
502503
}
503504
}

src/Exception/BeanHasNoPrimaryKey.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/Exception/ClassUniqueConstraintDuplicateColumns.php

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace CoolBeans\Exception;
6+
7+
final class DuplicateInColumnArray extends \Exception
8+
{
9+
public function __construct(string $beanName)
10+
{
11+
parent::__construct('Column array has duplicates in Bean ' . $beanName . '.');
12+
}
13+
}

src/Exception/EmptyColumnArray.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace CoolBeans\Exception;
6+
7+
final class EmptyColumnArray extends \Exception
8+
{
9+
public function __construct(string $beanName)
10+
{
11+
parent::__construct('Column array cannot be empty in Bean ' . $beanName . '.');
12+
}
13+
}

src/Exception/PrimaryKeyColumnDoesntExist.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/Exception/PrimaryKeyMultipleColumnsNotImplemented.php

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace CoolBeans\Exception;
6+
7+
final class UnknownColumnInColumnArray extends \Exception
8+
{
9+
public function __construct(string $column, string $beanName)
10+
{
11+
parent::__construct('Column [' . $column . '] given in column array doesnt exist in Bean ' . $beanName . '.');
12+
}
13+
}

tests/Unit/Command/SqlGeneratorCommandTest.php

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -167,23 +167,6 @@ public function testSettings() : void
167167
self::assertSame('sqlGenerator', $command->getName());
168168
}
169169

170-
public function testDuplicateColumns() : void
171-
{
172-
$application = new \Symfony\Component\Console\Application();
173-
$application->addCommands([new \CoolBeans\Command\SqlGeneratorCommand()]);
174-
175-
$command = $application->find('sqlGenerator');
176-
$commandTester = new \Symfony\Component\Console\Tester\CommandTester($command);
177-
178-
$this->expectException(\CoolBeans\Exception\ClassUniqueConstraintDuplicateColumns::class);
179-
$this->expectExceptionMessage('Found duplicate columns defined in ClassUniqueConstraint attribute.');
180-
181-
$commandTester->execute([
182-
'command' => 'sqlGenerator',
183-
'source' => __DIR__ . '/../InvalidBean/DuplicateColumns/',
184-
]);
185-
}
186-
187170
public function testUndefinedProperty() : void
188171
{
189172
$application = new \Symfony\Component\Console\Application();
@@ -192,8 +175,8 @@ public function testUndefinedProperty() : void
192175
$command = $application->find('sqlGenerator');
193176
$commandTester = new \Symfony\Component\Console\Tester\CommandTester($command);
194177

195-
$this->expectException(\CoolBeans\Exception\ClassUniqueConstraintUndefinedProperty::class);
196-
$this->expectExceptionMessage('Property with name "invalid" given in ClassUniqueConstraint is not defined.');
178+
$this->expectException(\CoolBeans\Exception\UnknownColumnInColumnArray::class);
179+
$this->expectExceptionMessage('Column [invalid] given in column array doesnt exist in Bean InvalidBean.');
197180

198181
$commandTester->execute([
199182
'command' => 'sqlGenerator',
@@ -209,32 +192,15 @@ public function testMissingPrimaryKey() : void
209192
$command = $application->find('sqlGenerator');
210193
$commandTester = new \Symfony\Component\Console\Tester\CommandTester($command);
211194

212-
$this->expectException(\CoolBeans\Exception\MissingPrimaryKey::class);
213-
$this->expectExceptionMessage('Bean InvalidBean has no primary key.');
195+
$this->expectException(\CoolBeans\Exception\UnknownColumnInColumnArray::class);
196+
$this->expectExceptionMessage('Column [id] given in column array doesnt exist in Bean InvalidBean.');
214197

215198
$commandTester->execute([
216199
'command' => 'sqlGenerator',
217200
'source' => __DIR__ . '/../InvalidBean/MissingPrimaryKey/',
218201
]);
219202
}
220203

221-
public function testPrimaryKeyAttributeMultipleColumns() : void
222-
{
223-
$application = new \Symfony\Component\Console\Application();
224-
$application->addCommands([new \CoolBeans\Command\SqlGeneratorCommand()]);
225-
226-
$command = $application->find('sqlGenerator');
227-
$commandTester = new \Symfony\Component\Console\Tester\CommandTester($command);
228-
229-
$this->expectException(\CoolBeans\Exception\PrimaryKeyMultipleColumnsNotImplemented::class);
230-
$this->expectExceptionMessage('Multiple column PrimaryKey is not implemented yet.');
231-
232-
$commandTester->execute([
233-
'command' => 'sqlGenerator',
234-
'source' => __DIR__ . '/../InvalidBean/PrimaryKeyAttributeMultipleColumns/',
235-
]);
236-
}
237-
238204
public function testPrimaryKeyAttributeMissingColumn() : void
239205
{
240206
$application = new \Symfony\Component\Console\Application();
@@ -243,8 +209,8 @@ public function testPrimaryKeyAttributeMissingColumn() : void
243209
$command = $application->find('sqlGenerator');
244210
$commandTester = new \Symfony\Component\Console\Tester\CommandTester($command);
245211

246-
$this->expectException(\CoolBeans\Exception\PrimaryKeyColumnDoesntExist::class);
247-
$this->expectExceptionMessage('PrimaryKey attribute column(s) unknown doesn\'t exist in Bean InvalidBean.');
212+
$this->expectException(\CoolBeans\Exception\UnknownColumnInColumnArray::class);
213+
$this->expectExceptionMessage('Column [unknown] given in column array doesnt exist in Bean InvalidBean.');
248214

249215
$commandTester->execute([
250216
'command' => 'sqlGenerator',

tests/Unit/InvalidBean/DuplicateColumns/InvalidBean.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)