Skip to content

Commit

Permalink
Merge pull request #195 from phpcr/use-data-provider
Browse files Browse the repository at this point in the history
use a data provider to see what actually fails
  • Loading branch information
dbu authored Nov 25, 2021
2 parents 9db8e41 + 5d6d0d9 commit 14d86bc
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 43 deletions.
61 changes: 33 additions & 28 deletions tests/Query/QOM/ConvertQueriesBackAndForthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@

class ConvertQueriesBackAndForthTest extends BaseCase
{
/**
* @var string[]
*/
protected $sql2Queries;

/**
* @var QueryObjectModelInterface[]
*/
Expand All @@ -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()));

Expand All @@ -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");
}
}
49 changes: 34 additions & 15 deletions tests/Query/QOM/Sql2ToQomConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,21 @@

class Sql2ToQomConverterTest extends \PHPCR\Test\BaseCase
{
protected $sql2Queries;

/**
* @var QueryObjectModelInterface[]
*/
protected $qomQueries;

/** @var Sql2ToQomQueryConverter */
/**
* @var Sql2ToQomQueryConverter
*/
protected $parser;

public function setUp(): void
{
parent::setUp();

$factory = $this->session->getWorkspace()->getQueryManager()->getQOMFactory();
$this->sql2Queries = Sql2TestQueries::getQueries();
$this->qomQueries = QomTestQueries::getQueries($factory);

try {
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 14d86bc

Please sign in to comment.