Skip to content

Commit 558dcab

Browse files
committed
Improving code coverage and exception throwing for lazy beans
1 parent e34752b commit 558dcab

File tree

7 files changed

+83
-8
lines changed

7 files changed

+83
-8
lines changed

src/DbRow.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public function _dbLoadIfNotLoaded(): void
217217
$dataLoader = $this->partialQuery->getStorageNode()->getManyToOneDataLoader($this->partialQuery->getKey());
218218

219219
if (count($this->primaryKeys) !== 1) {
220-
throw new \RuntimeException('Dataloader patterns only supports primary keys on one columns. Table "'.$this->dbTableName.'" has a PK on '.count($this->primaryKeys). ' columns');
220+
throw new \RuntimeException('Data-loader patterns only supports primary keys on one column. Table "'.$this->dbTableName.'" has a PK on '.count($this->primaryKeys). ' columns'); // @codeCoverageIgnore
221221
}
222222
$pks = $this->primaryKeys;
223223
$pkId = array_pop($pks);
@@ -234,7 +234,7 @@ public function _dbLoadIfNotLoaded(): void
234234
$result->closeCursor();
235235

236236
if ($row === false) {
237-
throw new TDBMException("Could not retrieve object from table \"$this->dbTableName\" using filter \".$sql_where.\" with data \"".var_export($parameters, true)."\".");
237+
throw new NoBeanFoundException("Could not retrieve object from table \"$this->dbTableName\" using filter \"$sql_where\" with data \"".var_export($parameters, true). '".');
238238
}
239239
}
240240

src/QueryFactory/FindObjectsQueryFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ protected function compute(): void
4343
[
4444
$this->magicSql,
4545
$this->magicSqlCount,
46-
$this->columnDescList
46+
$this->columnDescList,
47+
$this->magicSqlSubQuery
4748
] = $this->cache->fetch($key);
4849
return;
4950
}
@@ -85,6 +86,7 @@ protected function compute(): void
8586
$this->magicSql,
8687
$this->magicSqlCount,
8788
$this->columnDescList,
89+
$this->magicSqlSubQuery,
8890
]);
8991
}
9092

src/QueryFactory/SmartEagerLoad/Query/StaticPartialQuery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function getQueryFrom(): string
7373
$this->magicQuery->setOutputDialect(null);
7474
$fromIndex = strpos($sql, 'FROM');
7575
if ($fromIndex === false) {
76-
throw new TDBMException('Expected smart eager loader query to contain a "FROM"');
76+
throw new TDBMException('Expected smart eager loader query to contain a "FROM"'); // @codeCoverageIgnore
7777
}
7878
$this->magicFrom = substr($sql, $fromIndex);
7979
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace TheCodingMachine\TDBM\QueryFactory\SmartEagerLoad;
4+
5+
use Doctrine\DBAL\Connection;
6+
use PHPUnit\Framework\TestCase;
7+
use TheCodingMachine\TDBM\TDBMException;
8+
9+
class ManyToOneDataLoaderTest extends TestCase
10+
{
11+
12+
public function testGet()
13+
{
14+
$connection = $this->createMock(Connection::class);
15+
$connection->method('fetchAll')->willReturn([]);
16+
$dataLoader = new ManyToOneDataLoader($connection, 'SELECT * FROM users', 'id');
17+
18+
$this->expectException(TDBMException::class);
19+
$dataLoader->get('42');
20+
}
21+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace TheCodingMachine\TDBM\QueryFactory\SmartEagerLoad\Query;
4+
5+
use Doctrine\DBAL\Connection;
6+
use Mouf\Database\MagicQuery;
7+
use PHPUnit\Framework\TestCase;
8+
use TheCodingMachine\TDBM\QueryFactory\SmartEagerLoad\StorageNode;
9+
use TheCodingMachine\TDBM\TDBMException;
10+
11+
class StaticPartialQueryTest extends TestCase
12+
{
13+
14+
public function testRegisterDataLoader()
15+
{
16+
$query = new StaticPartialQuery('FROM users', ['foo'=>42], ['users'], $this->createMock(StorageNode::class), new MagicQuery());
17+
$this->expectException(TDBMException::class);
18+
$query->registerDataLoader($this->createMock(Connection::class));
19+
}
20+
21+
public function testGetMagicQuery()
22+
{
23+
$magicQuery = new MagicQuery();
24+
$query = new StaticPartialQuery('FROM users', ['foo'=>42], ['users'], $this->createMock(StorageNode::class), $magicQuery);
25+
$this->assertSame($magicQuery, $query->getMagicQuery());
26+
}
27+
28+
public function testGetParameters()
29+
{
30+
$magicQuery = new MagicQuery();
31+
$query = new StaticPartialQuery('FROM users', ['foo'=>42], ['users'], $this->createMock(StorageNode::class), $magicQuery);
32+
$this->assertSame(['foo'=>42], $query->getParameters());
33+
}
34+
}

tests/TDBMAbstractServiceTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ abstract class TDBMAbstractServiceTest extends TestCase
6969
/**
7070
* @var ArrayCache
7171
*/
72-
private $cache;
72+
private static $cache;
7373

7474
public static function setUpBeforeClass(): void
7575
{
@@ -133,10 +133,10 @@ protected function getDummyGeneratorListener() : DummyGeneratorListener
133133

134134
protected function getCache(): ArrayCache
135135
{
136-
if ($this->cache === null) {
137-
$this->cache = new ArrayCache();
136+
if (self::$cache === null) {
137+
self::$cache = new ArrayCache();
138138
}
139-
return $this->cache;
139+
return self::$cache;
140140
}
141141

142142
protected function getConfiguration() : ConfigurationInterface

tests/TDBMDaoGeneratorTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2217,6 +2217,9 @@ public function testSubQueryExceptionOnPrimaryKeysWithMultipleColumns(): void
22172217
$states->_getSubQuery();
22182218
}
22192219

2220+
/**
2221+
* @depends testDaoGeneration
2222+
*/
22202223
public function testManyToOneEagerLoading(): void
22212224
{
22222225
$userDao = new UserDao($this->tdbmService);
@@ -2237,4 +2240,19 @@ public function testManyToOneEagerLoading(): void
22372240
$this->assertTrue($users->getIterator()->hasManyToOneDataLoader('__country_id'));
22382241
$this->assertSame(['UK', 'France', 'Jamaica', 'UK', 'UK', 'Mexico'], $countryNames);
22392242
}
2243+
2244+
/**
2245+
* @depends testDaoGeneration
2246+
*/
2247+
public function testLazyLoadBadIdException(): void
2248+
{
2249+
$countryDao = new CountryDao($this->tdbmService);
2250+
$lazyBean = $countryDao->getById(-1, true);
2251+
2252+
$this->expectException(NoBeanFoundException::class);
2253+
$this->expectExceptionMessage("Could not retrieve object from table \"country\" using filter \"(`id` = :tdbmparam1)\" with data \"array (
2254+
'tdbmparam1' => -1,
2255+
)\".");
2256+
$lazyBean->getLabel();
2257+
}
22402258
}

0 commit comments

Comments
 (0)