Skip to content

Commit 4b8fabc

Browse files
committed
ResultIterator: Add code listener, Add tests for exceptions
1 parent 6c5ba90 commit 4b8fabc

6 files changed

+85
-3
lines changed

src/Utils/BaseCodeGeneratorListener.php

+5
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ public function onBaseDaoFindByIndexGenerated(MethodGenerator $methodGenerator,
136136
return $methodGenerator;
137137
}
138138

139+
public function onBaseResultIteratorGenerated(FileGenerator $fileGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration): ?FileGenerator
140+
{
141+
return $fileGenerator;
142+
}
143+
139144
/**
140145
* @param BeanDescriptor[] $beanDescriptors
141146
*/

src/Utils/BeanDescriptor.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,7 @@ public function generateDaoPhpCode(): ?FileGenerator
11291129
}
11301130

11311131
/**
1132-
* Writes the representation of the PHP DAO file.
1132+
* Writes the representation of the PHP ResultIterator file.
11331133
*/
11341134
public function generateResultIteratorPhpCode(): ?FileGenerator
11351135
{
@@ -1163,6 +1163,8 @@ public function generateResultIteratorPhpCode(): ?FileGenerator
11631163
[new Tag\MethodTag('getIterator', ['\\' . $beanClassName . '[]'])]
11641164
))->setWordWrap(false));
11651165

1166+
$file = $this->codeGeneratorListener->onBaseResultIteratorGenerated($file, $this, $this->configuration);
1167+
11661168
return $file;
11671169
}
11681170

src/Utils/CodeGeneratorEventDispatcher.php

+11
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,17 @@ public function onBaseDaoFindByIndexGenerated(MethodGenerator $methodGenerator,
264264
return $methodGenerator;
265265
}
266266

267+
public function onBaseResultIteratorGenerated(FileGenerator $fileGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration): ?FileGenerator
268+
{
269+
foreach ($this->listeners as $listener) {
270+
$fileGenerator = $listener->onBaseResultIteratorGenerated($fileGenerator, $beanDescriptor, $configuration);
271+
if ($fileGenerator === null) {
272+
break;
273+
}
274+
}
275+
return $fileGenerator;
276+
}
277+
267278
/**
268279
* @param BeanDescriptor[] $beanDescriptors
269280
*/

src/Utils/CodeGeneratorListenerInterface.php

+2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ public function onBaseDaoSetDefaultSortGenerated(MethodGenerator $methodGenerato
8080

8181
public function onBaseDaoFindByIndexGenerated(MethodGenerator $methodGenerator, Index $index, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator;
8282

83+
public function onBaseResultIteratorGenerated(FileGenerator $fileGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration): ?FileGenerator;
84+
8385
/**
8486
* @param BeanDescriptor[] $beanDescriptors
8587
*/

tests/TDBMServiceTest.php

+53-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
namespace TheCodingMachine\TDBM;
2323

2424
use Psr\Log\LogLevel;
25+
use Psr\Log\NullLogger;
2526
use Wa72\SimpleLogger\ArrayLogger;
2627

2728
class TDBMServiceTest extends TDBMAbstractServiceTest
@@ -682,7 +683,7 @@ public function testFindObjectsFromSql(): void
682683
*/
683684
public function testFindObjectsFromSqlBadTableName(): void
684685
{
685-
$this->expectException('TheCodingMachine\TDBM\TDBMException');
686+
$this->expectException(TDBMException::class);
686687
$this->tdbmService->findObjectsFromSql(
687688
'#{azerty',
688689
'roles JOIN roles_rights ON roles.id = roles_rights.role_id JOIN rights ON rights.label = roles_rights.right_label',
@@ -698,7 +699,7 @@ public function testFindObjectsFromSqlBadTableName(): void
698699
*/
699700
public function testFindObjectsFromSqlGroupBy(): void
700701
{
701-
$this->expectException('TheCodingMachine\TDBM\TDBMException');
702+
$this->expectException(TDBMException::class);
702703
$roles = $this->tdbmService->findObjectsFromSql(
703704
'roles',
704705
'roles JOIN roles_rights ON roles.id = roles_rights.role_id JOIN rights ON rights.label = roles_rights.right_label',
@@ -709,6 +710,20 @@ public function testFindObjectsFromSqlGroupBy(): void
709710
$role = $roles[0];
710711
}
711712

713+
/**
714+
*
715+
* @throws TDBMException
716+
*/
717+
public function testFindObjectsFromRawSqlBadTableName(): void
718+
{
719+
$this->expectException(TDBMException::class);
720+
$this->tdbmService->findObjectsFromRawSql(
721+
'#{azerty',
722+
'roles JOIN roles_rights ON roles.id = roles_rights.role_id JOIN rights ON rights.label = roles_rights.right_label WHERE rights.label = :right',
723+
array('right' => 'CAN_SING')
724+
);
725+
}
726+
712727
public function testFindObjectFromSql(): void
713728
{
714729
$role = $this->tdbmService->findObjectFromSql(
@@ -790,4 +805,40 @@ public function testFindObjectsFromSqlCountWithOneToManyLink(): void
790805

791806
$this->assertEquals(3, $countries->count());
792807
}
808+
809+
public function testBuildFilterFromFilterBagIterator(): void
810+
{
811+
$tdbmService = new TDBMService(new Configuration('TheCodingMachine\\TDBM\\Test\\Dao\\Bean', 'TheCodingMachine\\TDBM\\Test\\Dao', 'TheCodingMachine\\TDBM\\Test\\ResultIterator', self::getConnection(), $this->getNamingStrategy(), null, null, new NullLogger()));
812+
813+
[$sql, $parameters, $counter] = $tdbmService->buildFilterFromFilterBag(new \ArrayIterator(['id' => 1]), self::getConnection()->getDatabasePlatform());
814+
$this->assertRegExp('/\(.id. = :tdbmparam1\)/', $sql);
815+
$this->assertEquals($parameters['tdbmparam1'], 1);
816+
}
817+
818+
public function testFindObjectsMethodWithoutResultIteratorClass(): void
819+
{
820+
$this->expectException(TDBMInvalidArgumentException::class);
821+
$this->expectExceptionMessageRegExp('/^\$resultIteratorClass should be a `' . preg_quote(ResultIterator::class, '/') . '`. `(.*)` provided\.$/');
822+
$tdbmService = new TDBMService(new Configuration('TheCodingMachine\\TDBM\\Test\\Dao\\Bean', 'TheCodingMachine\\TDBM\\Test\\Dao', 'TheCodingMachine\\TDBM\\Test\\ResultIterator', self::getConnection(), $this->getNamingStrategy(), null, null, new NullLogger()));
823+
824+
$tdbmService->findObjects('', null, [], null, [], null, null, self::class);
825+
}
826+
827+
public function testFindObjectsFromSqlMethodWithoutResultIteratorClass(): void
828+
{
829+
$this->expectException(TDBMInvalidArgumentException::class);
830+
$this->expectExceptionMessageRegExp('/^\$resultIteratorClass should be a `' . preg_quote(ResultIterator::class, '/') . '`. `(.*)` provided\.$/');
831+
$tdbmService = new TDBMService(new Configuration('TheCodingMachine\\TDBM\\Test\\Dao\\Bean', 'TheCodingMachine\\TDBM\\Test\\Dao', 'TheCodingMachine\\TDBM\\Test\\ResultIterator', self::getConnection(), $this->getNamingStrategy(), null, null, new NullLogger()));
832+
833+
$tdbmService->findObjectsFromSql('', '', null, [], null, null, null, self::class);
834+
}
835+
836+
public function testFindObjectsFromRawSqlMethodWithoutResultIteratorClass(): void
837+
{
838+
$this->expectException(TDBMInvalidArgumentException::class);
839+
$this->expectExceptionMessageRegExp('/^\$resultIteratorClass should be a `' . preg_quote(ResultIterator::class, '/') . '`. `(.*)` provided\.$/');
840+
$tdbmService = new TDBMService(new Configuration('TheCodingMachine\\TDBM\\Test\\Dao\\Bean', 'TheCodingMachine\\TDBM\\Test\\Dao', 'TheCodingMachine\\TDBM\\Test\\ResultIterator', self::getConnection(), $this->getNamingStrategy(), null, null, new NullLogger()));
841+
842+
$tdbmService->findObjectsFromRawSql('', '', [], null, null, null, self::class);
843+
}
793844
}

tests/Utils/CodeGeneratorEventDispatcherTest.php

+11
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ public function onBaseDaoFindByIndexGenerated(MethodGenerator $methodGenerator,
132132
return null;
133133
}
134134

135+
public function onBaseResultIteratorGenerated(FileGenerator $fileGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration): ?FileGenerator
136+
{
137+
return null;
138+
}
139+
135140
/**
136141
* @param BeanDescriptor[] $beanDescriptors
137142
*/
@@ -299,6 +304,12 @@ public function testOnBaseDaoFindByIndexGenerated(): void
299304
$this->assertSame(null, $this->nullDispatcher->onBaseDaoFindByIndexGenerated($this->method1, $index, $this->beanDescriptor, $this->configuration, $this->class));
300305
}
301306

307+
public function testOnBaseResultIteratorGenerated(): void
308+
{
309+
$this->assertSame($this->file, $this->dispatcher->onBaseResultIteratorGenerated($this->file, $this->beanDescriptor, $this->configuration));
310+
$this->assertSame(null, $this->nullDispatcher->onBaseResultIteratorGenerated($this->file, $this->beanDescriptor, $this->configuration));
311+
}
312+
302313
public function testOnDaoFactoryGenerated(): void
303314
{
304315
$this->assertSame($this->file, $this->dispatcher->onDaoFactoryGenerated($this->file, [], $this->configuration));

0 commit comments

Comments
 (0)