Skip to content

Instantly share code, notes, and snippets.

@xsist10
Last active November 30, 2023 21:15
Show Gist options
  • Save xsist10/9502ce318dc39ff25c24940f1378767c to your computer and use it in GitHub Desktop.
Save xsist10/9502ce318dc39ff25c24940f1378767c to your computer and use it in GitHub Desktop.
Test case for a bug in the PHPSQLParser
<?php
declare(strict_types=1);
namespace Cadfael\Tests\Factory;
use PHPSQLParser\PHPSQLParser;
use PHPUnit\Framework\TestCase;
class GreenlionTest extends TestCase
{
private function getParsed($query)
{
$parser = new PHPSQLParser($query);
return $parser->parsed;
}
/**
* This test passes with an entry for the primary key
*/
public function testSimpleCreate()
{
$parsed = $this->getParsed("
CREATE TABLE `table1` (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id)
) ENGINE=InnoDB;
");
$primary_key_statement = $parsed["TABLE"]["create-def"]["sub_tree"][1];
$this->assertEquals("primary-key", $primary_key_statement["expr_type"]);
$this->assertEquals(
["expr_type" => "reserved", "base_expr" => "PRIMARY"],
$primary_key_statement["sub_tree"][0]
);
$this->assertEquals(
["expr_type" => "reserved", "base_expr" => "KEY"],
$primary_key_statement["sub_tree"][1]
);
}
/**
* This test fails because the primary key is returned as an index
* and the subtree seems to have lost the PRIMARY part which might
* explain why it's been classified as index instead of primary
*/
public function testCreateWithIndex()
{
$parsed = $this->getParsed("
CREATE TABLE `table2` (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
INDEX idx_id (id),
PRIMARY KEY (id)
) ENGINE=InnoDB;
");
$primary_key_statement = $parsed["TABLE"]["create-def"]["sub_tree"][2];
$this->assertEquals("primary-key", $primary_key_statement["expr_type"]);
$this->assertEquals(
["expr_type" => "reserved", "base_expr" => "PRIMARY"],
$primary_key_statement["sub_tree"][0]
);
$this->assertEquals(
["expr_type" => "reserved", "base_expr" => "KEY"],
$primary_key_statement["sub_tree"][1]
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment