From d3612a0f7b0f1ec5cd8f528af5e1eaa9e82f3e0d Mon Sep 17 00:00:00 2001 From: mringler Date: Fri, 12 Mar 2021 09:07:23 +0100 Subject: [PATCH 1/3] merged tests for name replacement --- .../ActiveQuery/CriteriaReplaceNameTest.php | 176 ++++++++++++++++++ .../Runtime/ActiveQuery/ModelCriteriaTest.php | 112 ++--------- .../ModelCriteriaWithNamespaceTest.php | 71 ------- .../ModelCriteriaWithSchemaTest.php | 82 -------- .../ActiveQuery/TestableModelCriteria.php | 21 --- 5 files changed, 191 insertions(+), 271 deletions(-) create mode 100644 tests/Propel/Tests/Runtime/ActiveQuery/CriteriaReplaceNameTest.php delete mode 100644 tests/Propel/Tests/Runtime/ActiveQuery/ModelCriteriaWithNamespaceTest.php delete mode 100644 tests/Propel/Tests/Runtime/ActiveQuery/ModelCriteriaWithSchemaTest.php delete mode 100644 tests/Propel/Tests/Runtime/ActiveQuery/TestableModelCriteria.php diff --git a/tests/Propel/Tests/Runtime/ActiveQuery/CriteriaReplaceNameTest.php b/tests/Propel/Tests/Runtime/ActiveQuery/CriteriaReplaceNameTest.php new file mode 100644 index 0000000000..c580421594 --- /dev/null +++ b/tests/Propel/Tests/Runtime/ActiveQuery/CriteriaReplaceNameTest.php @@ -0,0 +1,176 @@ +runTestReplaceName($c, $origClause, $columnPhpName, $modifiedClause); + } + + /** + * @dataProvider BookReplaceNamesDataProvider + * + * @return void + */ + public function testReplaceNameFromBook(string $origClause, ?string $columnPhpName, string $modifiedClause) + { + include self::PROJECT_ROOT . '/tests/Fixtures/bookstore/build/conf/bookstore-conf.php'; + $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); + $this->runTestReplaceName($c, $origClause, $columnPhpName, $modifiedClause); + } + + /** + * @dataProvider BookstoreContestReplaceNamesDataProvider + * + * @return void + */ + public function testReplaceNameFromBookstoreContest(string $origClause, ?string $columnPhpName, string $modifiedClause) + { + include self::PROJECT_ROOT . '/tests/Fixtures/bookstore/build/conf/bookstore-conf.php'; + $c = new ModelCriteria('bookstore-schemas', '\Propel\Tests\BookstoreSchemas\BookstoreContest'); + $this->runTestReplaceName($c, $origClause, $columnPhpName, $modifiedClause); + } + + /** + * @return void + */ + protected function runTestReplaceName(ModelCriteria $c, string $origClause, ?string $columnPhpName, string $modifiedClause) + { + $c->replaceNames($origClause); + $replacedColumns = $c->replacedColumns; + + if ($columnPhpName) { + $this->assertCount(1, $replacedColumns); + $columnMap = $c->getTableMap()->getColumnByPhpName($columnPhpName); + $this->assertEquals([$columnMap], $replacedColumns); + } + $this->assertEquals($modifiedClause, $origClause); + } + + /** + * Provides test data + * + * @return string[][]|string[][][] + */ + public static function ReplaceMultipleNamesDataProvider() + { + return [ + ['(Propel\Tests\Bookstore\Book.Id+Book.Id)=1', ['Id', 'Id'], '(book.id+book.id)=1'], // match multiple names + ['CONCAT(Propel\Tests\Bookstore\Book.Title,"Book.Id")= ?', ['Title'], 'CONCAT(book.title,"Book.Id")= ?'], // ignore names in strings + ['CONCAT(Propel\Tests\Bookstore\Book.Title," Book.Id ")= ?', ['Title'], 'CONCAT(book.title," Book.Id ")= ?'], // ignore names in strings + ['MATCH (Propel\Tests\Bookstore\Book.Title,Book.isbn) AGAINST (?)', ['Title', 'ISBN'], 'MATCH (book.title,book.isbn) AGAINST (?)'], + ]; + } + + /** + * @dataProvider ReplaceMultipleNamesDataProvider + * + * @return void + */ + public function testReplaceMultipleNames($origClause, $expectedColumns, $modifiedClause) + { + $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); + $c->replaceNames($origClause); + $replacedColumns = $c->replacedColumns; + + $this->assertCount(count($expectedColumns), $replacedColumns); + foreach ($replacedColumns as $index => $replacedColumn) { + $expectedColumnName = $expectedColumns[$index]; + $expectedColumnMap = $c->getTableMap()->getColumnByPhpName($expectedColumnName); + + $this->assertEquals($expectedColumnMap, $replacedColumn); + } + $this->assertEquals($modifiedClause, $origClause); + } +} diff --git a/tests/Propel/Tests/Runtime/ActiveQuery/ModelCriteriaTest.php b/tests/Propel/Tests/Runtime/ActiveQuery/ModelCriteriaTest.php index efe7d06542..1f4b3f76db 100644 --- a/tests/Propel/Tests/Runtime/ActiveQuery/ModelCriteriaTest.php +++ b/tests/Propel/Tests/Runtime/ActiveQuery/ModelCriteriaTest.php @@ -114,88 +114,6 @@ public function testFormatter() } } - public static function conditionsForTestReplaceNames() - { - return [ - ['Propel\Tests\Bookstore\Book.Title = ?', 'Title', 'book.title = ?'], // basic case - ['Propel\Tests\Bookstore\Book.Title=?', 'Title', 'book.title=?'], // without spaces - ['Propel\Tests\Bookstore\Book.Id<= ?', 'Id', 'book.id<= ?'], // with non-equal comparator - ['Propel\Tests\Bookstore\Book.AuthorId LIKE ?', 'AuthorId', 'book.author_id LIKE ?'], // with SQL keyword separator - ['(Propel\Tests\Bookstore\Book.AuthorId) LIKE ?', 'AuthorId', '(book.author_id) LIKE ?'], // with parenthesis - ['(Propel\Tests\Bookstore\Book.Id*1.5)=1', 'Id', '(book.id*1.5)=1'], // ignore numbers - // dealing with quotes - ["Propel\Tests\Bookstore\Book.Id + ' ' + Propel\Tests\Bookstore\Book.AuthorId", null, "book.id + ' ' + book.author_id"], - ["'Propel\Tests\Bookstore\Book.Id' + Propel\Tests\Bookstore\Book.AuthorId", null, "'Propel\Tests\Bookstore\Book.Id' + book.author_id"], - ["Propel\Tests\Bookstore\Book.Id + 'Propel\Tests\Bookstore\Book.AuthorId'", null, "book.id + 'Propel\Tests\Bookstore\Book.AuthorId'"], - ['1=1', null, '1=1'], // with no name - ['', null, ''], // with empty string - - //without NS - ['Book.Title = ?', 'Title', 'book.title = ?'], // basic case - ['Book.Title=?', 'Title', 'book.title=?'], // without spaces - ['Book.Id<= ?', 'Id', 'book.id<= ?'], // with non-equal comparator - ['Book.AuthorId LIKE ?', 'AuthorId', 'book.author_id LIKE ?'], // with SQL keyword separator - ['(Book.AuthorId) LIKE ?', 'AuthorId', '(book.author_id) LIKE ?'], // with parenthesis - ['(Book.Id*1.5)=1', 'Id', '(book.id*1.5)=1'], // ignore numbers - // dealing with quotes - ["Book.Id + ' ' + Book.AuthorId", null, "book.id + ' ' + book.author_id"], - ["'Book.Id' + Book.AuthorId", null, "'Book.Id' + book.author_id"], - ["Book.Id + 'Book.AuthorId'", null, "book.id + 'Book.AuthorId'"], - - ]; - } - - /** - * @dataProvider conditionsForTestReplaceNames - * - * @return void - */ - public function testReplaceNames($origClause, $columnPhpName, $modifiedClause) - { - $c = new TestableModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); - $this->doTestReplaceNames($c, BookTableMap::getTableMap(), $origClause, $columnPhpName, $modifiedClause); - } - - /** - * @return void - */ - public function doTestReplaceNames(Criteria $c, $tableMap, $origClause, $columnPhpName, $modifiedClause) - { - $c->replaceNames($origClause); - $columns = $c->replacedColumns; - if ($columnPhpName) { - $this->assertEquals([$tableMap->getColumnByPhpName($columnPhpName)], $columns); - } - $this->assertEquals($modifiedClause, $origClause); - } - - public static function conditionsForTestReplaceMultipleNames() - { - return [ - ['(Propel\Tests\Bookstore\Book.Id+Book.Id)=1', ['Id', 'Id'], '(book.id+book.id)=1'], // match multiple names - ['CONCAT(Propel\Tests\Bookstore\Book.Title,"Book.Id")= ?', ['Title', 'Id'], 'CONCAT(book.title,"Book.Id")= ?'], // ignore names in strings - ['CONCAT(Propel\Tests\Bookstore\Book.Title," Book.Id ")= ?', ['Title', 'Id'], 'CONCAT(book.title," Book.Id ")= ?'], // ignore names in strings - ['MATCH (Propel\Tests\Bookstore\Book.Title,Book.isbn) AGAINST (?)', ['Title', 'ISBN'], 'MATCH (book.title,book.isbn) AGAINST (?)'], - ]; - } - - /** - * @dataProvider conditionsForTestReplaceMultipleNames - * - * @return void - */ - public function testReplaceMultipleNames($origClause, $expectedColumns, $modifiedClause) - { - $c = new TestableModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); - $c->replaceNames($origClause); - $foundColumns = $c->replacedColumns; - foreach ($foundColumns as $column) { - $expectedColumn = BookTableMap::getTableMap()->getColumnByPhpName(array_shift($expectedColumns)); - $this->assertEquals($expectedColumn, $column); - } - $this->assertEquals($modifiedClause, $origClause); - } - /** * @return void */ @@ -1293,7 +1211,7 @@ public function testGetJoin() */ public function testWith() { - $c = new TestableModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); + $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->join('Propel\Tests\Bookstore\Book.Author'); $c->with('Author'); $withs = $c->getWith(); @@ -1317,7 +1235,7 @@ public function testWithThrowsExceptionWhenJoinLacks() */ public function testWithAlias() { - $c = new TestableModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); + $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->join('Propel\Tests\Bookstore\Book.Author a'); $c->with('a'); $withs = $c->getWith(); @@ -1341,7 +1259,7 @@ public function testWithThrowsExceptionWhenNotUsingAlias() */ public function testWithAddsSelectColumns() { - $c = new TestableModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); + $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); BookTableMap::addSelectColumns($c); $c->join('Propel\Tests\Bookstore\Book.Author'); $c->with('Author'); @@ -1366,7 +1284,7 @@ public function testWithAddsSelectColumns() */ public function testWithAliasAddsSelectColumns() { - $c = new TestableModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); + $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); BookTableMap::addSelectColumns($c); $c->join('Propel\Tests\Bookstore\Book.Author a'); $c->with('a'); @@ -1391,7 +1309,7 @@ public function testWithAliasAddsSelectColumns() */ public function testWithAddsSelectColumnsOfMainTable() { - $c = new TestableModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); + $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->join('Propel\Tests\Bookstore\Book.Author'); $c->with('Author'); $expectedColumns = [ @@ -1415,7 +1333,7 @@ public function testWithAddsSelectColumnsOfMainTable() */ public function testWithAliasAddsSelectColumnsOfMainTable() { - $c = new TestableModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); + $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->setModelAlias('b', true); $c->join('b.Author a'); $c->with('a'); @@ -1440,7 +1358,7 @@ public function testWithAliasAddsSelectColumnsOfMainTable() */ public function testWithOneToManyAddsSelectColumns() { - $c = new TestableModelCriteria('bookstore', 'Propel\Tests\Bookstore\Author'); + $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Author'); AuthorTableMap::addSelectColumns($c); $c->leftJoin('Propel\Tests\Bookstore\Author.Book'); $c->with('Book'); @@ -1465,7 +1383,7 @@ public function testWithOneToManyAddsSelectColumns() */ public function testJoinWith() { - $c = new TestableModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); + $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->joinWith('Propel\Tests\Bookstore\Book.Author'); $expectedColumns = [ BookTableMap::COL_ID, @@ -1491,7 +1409,7 @@ public function testJoinWith() */ public function testJoinWithType() { - $c = new TestableModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); + $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->joinWith('Propel\Tests\Bookstore\Book.Author', Criteria::LEFT_JOIN); $joins = $c->getJoins(); $join = $joins['Author']; @@ -1503,7 +1421,7 @@ public function testJoinWithType() */ public function testJoinWithAlias() { - $c = new TestableModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); + $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->joinWith('Propel\Tests\Bookstore\Book.Author a'); $expectedColumns = [ BookTableMap::COL_ID, @@ -1526,7 +1444,7 @@ public function testJoinWithAlias() */ public function testJoinWithSeveral() { - $c = new TestableModelCriteria('bookstore', 'Propel\Tests\Bookstore\Review'); + $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Review'); $c->joinWith('Review.Book'); $c->joinWith('Propel\Tests\Bookstore\Book.Author'); $c->joinWith('Book.Publisher'); @@ -1562,7 +1480,7 @@ public function testJoinWithSeveral() */ public function testJoinWithTwice() { - $c = new TestableModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); + $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->join('Propel\Tests\Bookstore\Book.Review'); $c->joinWith('Propel\Tests\Bookstore\Book.Author'); $c->joinWith('Propel\Tests\Bookstore\Book.Review'); @@ -2636,7 +2554,7 @@ public function testMagicJoin() */ public function testMagicJoinWith() { - $c = new TestableModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); + $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->leftJoinWith('Propel\Tests\Bookstore\Book.Author a'); $expectedColumns = [ BookTableMap::COL_ID, @@ -2662,7 +2580,7 @@ public function testMagicJoinWith() */ public function testMagicJoinWithRelation() { - $c = new TestableModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); + $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->joinWithAuthor(); $expectedColumns = [ BookTableMap::COL_ID, @@ -2688,7 +2606,7 @@ public function testMagicJoinWithRelation() */ public function testMagicJoinWithTypeAndRelation() { - $c = new TestableModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); + $c = new ModelCriteria('bookstore', 'Propel\Tests\Bookstore\Book'); $c->leftJoinWithAuthor(); $expectedColumns = [ BookTableMap::COL_ID, diff --git a/tests/Propel/Tests/Runtime/ActiveQuery/ModelCriteriaWithNamespaceTest.php b/tests/Propel/Tests/Runtime/ActiveQuery/ModelCriteriaWithNamespaceTest.php deleted file mode 100644 index a08da385c0..0000000000 --- a/tests/Propel/Tests/Runtime/ActiveQuery/ModelCriteriaWithNamespaceTest.php +++ /dev/null @@ -1,71 +0,0 @@ - - */ -class ModelCriteriaWithNamespaceTest extends NamespacesTestBase -{ - public static function conditionsForTestReplaceNamesWithNamespaces() - { - return [ - ['Foo\\Bar\\NamespacedBook.Title = ?', 'Title', 'namespaced_book.title = ?'], // basic case - ['Foo\\Bar\\NamespacedBook.Title=?', 'Title', 'namespaced_book.title=?'], // without spaces - ['Foo\\Bar\\NamespacedBook.Id<= ?', 'Id', 'namespaced_book.id<= ?'], // with non-equal comparator - ['Foo\\Bar\\NamespacedBook.AuthorId LIKE ?', 'AuthorId', 'namespaced_book.author_id LIKE ?'], // with SQL keyword separator - ['(Foo\\Bar\\NamespacedBook.AuthorId) LIKE ?', 'AuthorId', '(namespaced_book.author_id) LIKE ?'], // with parenthesis - ['(Foo\\Bar\\NamespacedBook.Id*1.5)=1', 'Id', '(namespaced_book.id*1.5)=1'], // ignore numbers - // dealing with quotes - ["Foo\\Bar\\NamespacedBook.Id + ' ' + Foo\\Bar\\NamespacedBook.AuthorId", null, "namespaced_book.id + ' ' + namespaced_book.author_id"], - ["'Foo\\Bar\\NamespacedBook.Id' + Foo\\Bar\\NamespacedBook.AuthorId", null, "'Foo\\Bar\\NamespacedBook.Id' + namespaced_book.author_id"], - ["Foo\\Bar\\NamespacedBook.Id + 'Foo\\Bar\\NamespacedBook.AuthorId'", null, "namespaced_book.id + 'Foo\\Bar\\NamespacedBook.AuthorId'"], - ]; - } - - /** - * @dataProvider conditionsForTestReplaceNamesWithNamespaces - * - * @return void - */ - public function testReplaceNamesWithNamespaces($origClause, $columnPhpName, $modifiedClause) - { - $c = new TestableModelCriteriaWithNamespace('bookstore_namespaced', 'Foo\\Bar\\NamespacedBook'); - $this->doTestReplaceNames($c, NamespacedBookTableMap::getTableMap(), $origClause, $columnPhpName, $modifiedClause); - } - - /** - * @return void - */ - public function doTestReplaceNames($c, $tableMap, $origClause, $columnPhpName, $modifiedClause) - { - $c->replaceNames($origClause); - $columns = $c->replacedColumns; - if ($columnPhpName) { - $this->assertEquals([$tableMap->getColumnByPhpName($columnPhpName)], $columns); - } - $this->assertEquals($modifiedClause, $origClause); - } -} - -class TestableModelCriteriaWithNamespace extends ModelCriteria -{ - public $joins = []; - - public function replaceNames(&$sql) - { - return parent::replaceNames($sql); - } -} diff --git a/tests/Propel/Tests/Runtime/ActiveQuery/ModelCriteriaWithSchemaTest.php b/tests/Propel/Tests/Runtime/ActiveQuery/ModelCriteriaWithSchemaTest.php deleted file mode 100644 index 03f986220e..0000000000 --- a/tests/Propel/Tests/Runtime/ActiveQuery/ModelCriteriaWithSchemaTest.php +++ /dev/null @@ -1,82 +0,0 @@ -createSelectSql($params); - - $this->assertEquals($expectedSql, $result, $message); - $this->assertEquals($expectedParams, $params, $message); - } - - public static function conditionsForTestReplaceNamesWithSchemas() - { - return [ - ['BookstoreContest.PrizeBookId = ?', 'PrizeBookId', 'contest.bookstore_contest.prize_book_id = ?'], // basic case - ['BookstoreContest.PrizeBookId=?', 'PrizeBookId', 'contest.bookstore_contest.prize_book_id=?'], // without spaces - ['BookstoreContest.Id<= ?', 'Id', 'contest.bookstore_contest.id<= ?'], // with non-equal comparator - ['BookstoreContest.BookstoreId LIKE ?', 'BookstoreId', 'contest.bookstore_contest.bookstore_id LIKE ?'], // with SQL keyword separator - ['(BookstoreContest.BookstoreId) LIKE ?', 'BookstoreId', '(contest.bookstore_contest.bookstore_id) LIKE ?'], // with parenthesis - ['(BookstoreContest.Id*1.5)=1', 'Id', '(contest.bookstore_contest.id*1.5)=1'], // ignore numbers - ]; - } - - /** - * @dataProvider conditionsForTestReplaceNamesWithSchemas - * - * @return void - */ - public function testReplaceNamesWithSchemas($origClause, $columnPhpName, $modifiedClause) - { - $c = new TestableModelCriteriaWithSchema('bookstore-schemas', '\Propel\Tests\BookstoreSchemas\BookstoreContest'); - $this->doTestReplaceNames($c, BookstoreContestTableMap::getTableMap(), $origClause, $columnPhpName, $modifiedClause); - } - - /** - * @return void - */ - public function doTestReplaceNames($c, $tableMap, $origClause, $columnPhpName, $modifiedClause) - { - $c->replaceNames($origClause); - $columns = $c->replacedColumns; - if ($columnPhpName) { - $this->assertEquals([$tableMap->getColumnByPhpName($columnPhpName)], $columns); - } - $modifiedClause = preg_replace('/^(\(?)contest\./', '$1contest' . $this->getPlatform()->getSchemaDelimiter(), $modifiedClause); - $this->assertEquals($modifiedClause, $origClause); - } -} - -class TestableModelCriteriaWithSchema extends ModelCriteria -{ - public $joins = []; - - public function replaceNames(&$sql) - { - return parent::replaceNames($sql); - } -} diff --git a/tests/Propel/Tests/Runtime/ActiveQuery/TestableModelCriteria.php b/tests/Propel/Tests/Runtime/ActiveQuery/TestableModelCriteria.php deleted file mode 100644 index f7d3655fa3..0000000000 --- a/tests/Propel/Tests/Runtime/ActiveQuery/TestableModelCriteria.php +++ /dev/null @@ -1,21 +0,0 @@ - Date: Fri, 12 Mar 2021 09:55:07 +0100 Subject: [PATCH 2/3] bugfix: keep alias from subquery --- .../Runtime/ActiveQuery/ModelCriteria.php | 3 +- .../ActiveQuery/CriteriaReplaceNameTest.php | 31 ++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/Propel/Runtime/ActiveQuery/ModelCriteria.php b/src/Propel/Runtime/ActiveQuery/ModelCriteria.php index 873868ac2f..243d0e8628 100644 --- a/src/Propel/Runtime/ActiveQuery/ModelCriteria.php +++ b/src/Propel/Runtime/ActiveQuery/ModelCriteria.php @@ -2204,8 +2204,9 @@ protected function getColumnFromSubQuery($class, $phpName, $failSilently = true) if ($tableMap->hasColumnByPhpName($phpName)) { $column = $tableMap->getColumnByPhpName($phpName); $realColumnName = $class . '.' . $column->getName(); + $this->currentAlias = $class; - return [$column, $realColumnName]; + return [null, $realColumnName]; } if (isset($subQueryCriteria->asColumns[$phpName])) { // aliased column diff --git a/tests/Propel/Tests/Runtime/ActiveQuery/CriteriaReplaceNameTest.php b/tests/Propel/Tests/Runtime/ActiveQuery/CriteriaReplaceNameTest.php index c580421594..99516503e6 100644 --- a/tests/Propel/Tests/Runtime/ActiveQuery/CriteriaReplaceNameTest.php +++ b/tests/Propel/Tests/Runtime/ActiveQuery/CriteriaReplaceNameTest.php @@ -9,6 +9,8 @@ namespace Propel\Tests\Runtime\ActiveQuery; use Propel\Runtime\ActiveQuery\ModelCriteria; +use Propel\Tests\Bookstore\AuthorQuery; +use Propel\Tests\Bookstore\BookQuery; use Propel\Tests\TestCase; /** @@ -17,6 +19,7 @@ class CriteriaReplaceNameTest extends TestCase { private const PROJECT_ROOT = __DIR__ . '/../../../../..'; + /** * Provides test data * @@ -168,9 +171,35 @@ public function testReplaceMultipleNames($origClause, $expectedColumns, $modifie foreach ($replacedColumns as $index => $replacedColumn) { $expectedColumnName = $expectedColumns[$index]; $expectedColumnMap = $c->getTableMap()->getColumnByPhpName($expectedColumnName); - + $this->assertEquals($expectedColumnMap, $replacedColumn); } $this->assertEquals($modifiedClause, $origClause); } + + /** + * @return void + */ + public function testReplaceNamesFromSubquery() + { + $numberOfBooksQuery = BookQuery::create() + ->addAsColumn('NumberOfBooks', 'COUNT(Book.Id)') + ->select(['NumberOfBooks', 'AuthorId']) + ->groupBy('Book.AuthorId'); + + $joinCondition = 'Author.Id = numberOfBooks.AuthorId'; + + $authorQuery = AuthorQuery::create() + ->addSelectQuery($numberOfBooksQuery, 'numberOfBooks', false) + ->where($joinCondition) + ->withColumn('numberOfBooks.NumberOfBooks', 'NumberOfBooks'); + + $authorQuery->replaceNames($joinCondition); // note that replaceNames() changes the input string + + $this->assertEquals('author.id = numberOfBooks.AuthorId', $joinCondition, 'Aliases from subquery should not be replaced'); + + $authorIdColumnMap = $authorQuery->getTableMap()->getColumnByPhpName('Id'); + $replacedColumns = $authorQuery->replacedColumns; + $this->assertEquals([$authorIdColumnMap], $replacedColumns, 'Only own column (AuthorId) should count as replaced column'); + } } From 2c5263544d5f1b47fd1f0b91871d7e0cf885ee2d Mon Sep 17 00:00:00 2001 From: mringler Date: Fri, 12 Mar 2021 10:55:36 +0100 Subject: [PATCH 3/3] removed small semantic mistakes --- phpstan-baseline.neon | 10 ---------- src/Propel/Runtime/ActiveQuery/BaseModelCriteria.php | 3 +-- src/Propel/Runtime/ActiveQuery/Criteria.php | 12 ++++++++++++ src/Propel/Runtime/ActiveQuery/ModelCriteria.php | 12 ------------ 4 files changed, 13 insertions(+), 24 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 2381e9e67e..cde0450dcb 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -420,16 +420,6 @@ parameters: count: 1 path: src/Propel/Runtime/ActiveQuery/Criteria.php - - - message: "#^Access to an undefined property Propel\\\\Runtime\\\\ActiveQuery\\\\Criteria\\:\\:\\$currentAlias\\.$#" - count: 1 - path: src/Propel/Runtime/ActiveQuery/Criteria.php - - - - message: "#^Access to an undefined property Propel\\\\Runtime\\\\ActiveQuery\\\\Criteria\\:\\:\\$foundMatch\\.$#" - count: 1 - path: src/Propel/Runtime/ActiveQuery/Criteria.php - - message: "#^Call to an undefined method Propel\\\\Runtime\\\\Adapter\\\\AdapterInterface\\:\\:cleanupSQL\\(\\)\\.$#" count: 1 diff --git a/src/Propel/Runtime/ActiveQuery/BaseModelCriteria.php b/src/Propel/Runtime/ActiveQuery/BaseModelCriteria.php index faca9e502a..e4740afe77 100644 --- a/src/Propel/Runtime/ActiveQuery/BaseModelCriteria.php +++ b/src/Propel/Runtime/ActiveQuery/BaseModelCriteria.php @@ -65,8 +65,7 @@ class BaseModelCriteria extends Criteria implements IteratorAggregate */ public function __construct($dbName = null, $modelName = null, $modelAlias = null) { - $this->setDbName($dbName); - $this->originalDbName = $dbName; + parent::__construct($dbName); $this->setModelName($modelName); $this->modelAlias = $modelAlias; } diff --git a/src/Propel/Runtime/ActiveQuery/Criteria.php b/src/Propel/Runtime/ActiveQuery/Criteria.php index 6a2cd02ae9..7a9e13a02d 100644 --- a/src/Propel/Runtime/ActiveQuery/Criteria.php +++ b/src/Propel/Runtime/ActiveQuery/Criteria.php @@ -277,6 +277,18 @@ class Criteria */ public $replacedColumns = []; + /** + * temporary property used in replaceNames + * + * @var string|null + */ + protected $currentAlias; + + /** + * @var bool + */ + protected $foundMatch = false; + /** * Creates a new instance with the default capacity which corresponds to * the specified database. diff --git a/src/Propel/Runtime/ActiveQuery/ModelCriteria.php b/src/Propel/Runtime/ActiveQuery/ModelCriteria.php index 243d0e8628..74b99abb96 100644 --- a/src/Propel/Runtime/ActiveQuery/ModelCriteria.php +++ b/src/Propel/Runtime/ActiveQuery/ModelCriteria.php @@ -92,18 +92,6 @@ class ModelCriteria extends BaseModelCriteria */ protected $select; - /** - * temporary property used in replaceNames - * - * @var string|null - */ - protected $currentAlias; - - /** - * @var bool - */ - protected $foundMatch = false; - /** * Used to memorize whether we added self-select columns before. *