55namespace Bancer \NativeQueryMapperTest \TestCase ;
66
77use PHPUnit \Framework \TestCase ;
8+ use Bancer \NativeQueryMapper \ORM \UnknownAliasException ;
89use Bancer \NativeQueryMapperTest \TestApp \Model \Entity \Article ;
910use Bancer \NativeQueryMapperTest \TestApp \Model \Entity \Comment ;
1011use Bancer \NativeQueryMapperTest \TestApp \Model \Entity \Country ;
1415use Bancer \NativeQueryMapperTest \TestApp \Model \Table \ArticlesTable ;
1516use Bancer \NativeQueryMapperTest \TestApp \Model \Table \CommentsTable ;
1617use Bancer \NativeQueryMapperTest \TestApp \Model \Table \CountriesTable ;
17- use Cake \ORM \Locator \LocatorAwareTrait ;
18- use Bancer \NativeQueryMapper \ORM \UnknownAliasException ;
1918use Bancer \NativeQueryMapperTest \TestApp \Model \Table \UsersTable ;
19+ use Cake \ORM \Locator \LocatorAwareTrait ;
2020
2121class NativeQueryMapperTest extends TestCase
2222{
@@ -41,7 +41,9 @@ private function assertEqualsEntities(array $expected, array $actual): void
4141 public function testInvalidAlias (): void
4242 {
4343 $ this ->expectException (UnknownAliasException::class);
44- $ this ->expectExceptionMessage ("The query must use root table alias 'Articles' " );
44+ $ expectedMessage = "The query must select at least one column from the root table. " ;
45+ $ expectedMessage .= " The column alias must use Articles__{column_name} format " ;
46+ $ this ->expectExceptionMessage ($ expectedMessage );
4547 /** @var \Bancer\NativeQueryMapperTest\TestApp\Model\Table\ArticlesTable $ArticlesTable */
4648 $ ArticlesTable = $ this ->fetchTable (ArticlesTable::class);
4749 $ stmt = $ ArticlesTable ->prepareSQL ("
@@ -53,6 +55,38 @@ public function testInvalidAlias(): void
5355 $ ArticlesTable ->fromNativeQuery ($ stmt )->all ();
5456 }
5557
58+ public function testMissingColumnAlias (): void
59+ {
60+ $ this ->expectException (UnknownAliasException::class);
61+ $ this ->expectExceptionMessage ("Column 'title' must use an alias in the format {Alias}__title " );
62+ /** @var \Bancer\NativeQueryMapperTest\TestApp\Model\Table\ArticlesTable $ArticlesTable */
63+ $ ArticlesTable = $ this ->fetchTable (ArticlesTable::class);
64+ $ stmt = $ ArticlesTable ->prepareSQL ("
65+ SELECT
66+ id AS Articles__id,
67+ title
68+ FROM articles
69+ " );
70+ $ ArticlesTable ->fromNativeQuery ($ stmt )->all ();
71+ }
72+
73+ public function testIncompleteColumnAlias (): void
74+ {
75+ $ this ->expectException (UnknownAliasException::class);
76+ $ this ->expectExceptionMessage (
77+ "Alias 'Articles__' is invalid. Column alias must use {Alias}__{column_name} format " ,
78+ );
79+ /** @var \Bancer\NativeQueryMapperTest\TestApp\Model\Table\ArticlesTable $ArticlesTable */
80+ $ ArticlesTable = $ this ->fetchTable (ArticlesTable::class);
81+ $ stmt = $ ArticlesTable ->prepareSQL ("
82+ SELECT
83+ id AS Articles__id,
84+ title AS Articles__
85+ FROM articles
86+ " );
87+ $ ArticlesTable ->fromNativeQuery ($ stmt )->all ();
88+ }
89+
5690 public function testEmptyResultSet (): void
5791 {
5892 /** @var \Bancer\NativeQueryMapperTest\TestApp\Model\Table\ArticlesTable $ArticlesTable */
0 commit comments