diff --git a/tests/Query/QOM/ConvertQueriesBackAndForthTest.php b/tests/Query/QOM/ConvertQueriesBackAndForthTest.php index 35f947c5..fc82ebaf 100644 --- a/tests/Query/QOM/ConvertQueriesBackAndForthTest.php +++ b/tests/Query/QOM/ConvertQueriesBackAndForthTest.php @@ -21,11 +21,6 @@ class ConvertQueriesBackAndForthTest extends BaseCase { - /** - * @var string[] - */ - protected $sql2Queries; - /** * @var QueryObjectModelInterface[] */ @@ -46,7 +41,6 @@ public function setUp(): void parent::setUp(); $factory = $this->session->getWorkspace()->getQueryManager()->getQOMFactory(); - $this->sql2Queries = Sql2TestQueries::getQueries(); $this->qomQueries = QomTestQueries::getQueries($factory); $this->qomParser = new QomToSql2QueryConverter(new Sql2Generator(new ValueConverter())); @@ -57,31 +51,42 @@ public function setUp(): void } } - public function testBackAndForth() + public function provideQueries() { - foreach ($this->qomQueries as $name => $originalQomQuery) { - $originalSql2Query = $this->sql2Queries[$name]; - if (is_array($originalSql2Query)) { - $this->assertGreaterThan(0, count($originalSql2Query), 'empty list of queries'); - $passed = false; - $sql2 = 'None of the QOM statements matched'; - foreach ($originalSql2Query as $query) { - $qom = $this->sql2Parser->parse($query); - if ($originalQomQuery->getStatement() == $qom->getStatement()) { - $sql2 = $this->qomParser->convert($qom); - if ($sql2 == $query) { - $passed = true; - break; - } - } + // unfortunately the provider can't create the QOM queries because phpunit calls the data providers before doing setUp/setupBeforeClass + foreach (Sql2TestQueries::getQueries() as $name => $originalSqlQuery) { + yield $name => [$name, $originalSqlQuery]; + } + } + + /** + * @dataProvider provideQueries + * + * @param string $name + * @param string|string[] $originalSql2Query + */ + public function testBackAndForth($name, $originalSql2Query) + { + if (!array_key_exists($name, $this->qomQueries)) { + $this->markTestSkipped('Case '.$name.' needs to be implemented'); + } + $originalQomQuery = $this->qomQueries[$name]; + if (is_array($originalSql2Query)) { + $this->assertGreaterThan(0, count($originalSql2Query), 'empty list of queries'); + foreach ($originalSql2Query as $query) { + $qom = $this->sql2Parser->parse($query); + if ($originalQomQuery->getStatement() == $qom->getStatement() + && $query == $this->qomParser->convert($qom) + ) { + return; } - $this->assertTrue($passed, "QOM-->SQL2->QOM: Query variation $name resulted in SQL2 that is not found: $sql2"); - } else { - $qom = $this->sql2Parser->parse($originalSql2Query); - $this->assertEquals($originalQomQuery, $qom, "QOM-->SQL2: Original query = $originalSql2Query"); - $sql2 = $this->qomParser->convert($qom); - $this->assertEquals($originalSql2Query, $sql2, "SQL2-->QOM: Original query = $originalSql2Query"); } + $this->fail("QOM-->SQL2->QOM: Query variation $name resulted in SQL2 that is not found: ".var_export($originalSql2Query, true)); } + + $qom = $this->sql2Parser->parse($originalSql2Query); + $this->assertEquals($originalQomQuery, $qom, "QOM-->SQL2: Original query = $originalSql2Query"); + $sql2 = $this->qomParser->convert($qom); + $this->assertEquals($originalSql2Query, $sql2, "SQL2-->QOM: Original query = $originalSql2Query"); } } diff --git a/tests/Query/QOM/Sql2ToQomConverterTest.php b/tests/Query/QOM/Sql2ToQomConverterTest.php index 662231c2..fdce6057 100644 --- a/tests/Query/QOM/Sql2ToQomConverterTest.php +++ b/tests/Query/QOM/Sql2ToQomConverterTest.php @@ -21,11 +21,14 @@ class Sql2ToQomConverterTest extends \PHPCR\Test\BaseCase { - protected $sql2Queries; - + /** + * @var QueryObjectModelInterface[] + */ protected $qomQueries; - /** @var Sql2ToQomQueryConverter */ + /** + * @var Sql2ToQomQueryConverter + */ protected $parser; public function setUp(): void @@ -33,7 +36,6 @@ public function setUp(): void parent::setUp(); $factory = $this->session->getWorkspace()->getQueryManager()->getQOMFactory(); - $this->sql2Queries = Sql2TestQueries::getQueries(); $this->qomQueries = QomTestQueries::getQueries($factory); try { @@ -66,20 +68,37 @@ public function testColumnsAndSelector() $this->assertEquals('prop2', $cols[1]->getColumnName()); } - public function testQueries() + public function provideQueries() { - foreach ($this->qomQueries as $name => $query) { - $sql2 = $this->sql2Queries[$name]; - if (is_array($sql2)) { - foreach ($sql2 as $sql2Variation) { - $qom = $this->parser->parse($sql2Variation); - $this->assertEquals($query, $qom, "Original query = $sql2Variation"); - } - } else { - $qom = $this->parser->parse($sql2); - $this->assertEquals($query, $qom, "Original query = $sql2"); + // unfortunately the provider can't create the QOM queries because phpunit calls the data providers before doing setUp/setupBeforeClass + foreach (Sql2TestQueries::getQueries() as $name => $sql2) { + yield $name => [$name, $sql2]; + } + } + + /** + * @dataProvider provideQueries + * + * @param string $name + * @param string|string[] $originalSql2Query + */ + public function testQueries($name, $sql2) + { + if (!array_key_exists($name, $this->qomQueries)) { + $this->markTestSkipped('Case '.$name.' needs to be implemented'); + } + $query = $this->qomQueries[$name]; + + if (is_array($sql2)) { + foreach ($sql2 as $sql2Variation) { + $qom = $this->parser->parse($sql2Variation); + $this->assertEquals($query, $qom, "Original query = $sql2Variation"); } + return; } + + $qom = $this->parser->parse($sql2); + $this->assertEquals($query, $qom, "Original query = $sql2"); } public function getSQL2WithWhitespace()