QuoteTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace Medoo\Tests;
  3. use Medoo\Medoo;
  4. use InvalidArgumentException;
  5. /**
  6. * @coversDefaultClass \Medoo\Medoo
  7. */
  8. class QuoteTest extends MedooTestCase
  9. {
  10. /**
  11. * @covers ::quote()
  12. * @dataProvider typesProvider
  13. */
  14. public function testQuote($type)
  15. {
  16. $this->setType($type);
  17. $quotedString = $this->database->quote("Co'mpl''ex \"st'\"ring");
  18. $expected = [
  19. 'mysql' => <<<EOD
  20. 'Co\'mpl\'\'ex \"st\'\"ring'
  21. EOD,
  22. 'mssql' => <<<EOD
  23. 'Co''mpl''''ex "st''"ring'
  24. EOD,
  25. 'sqlite' => <<<EOD
  26. 'Co''mpl''''ex "st''"ring'
  27. EOD,
  28. 'pgsql' => <<<EOD
  29. 'Co''mpl''''ex "st''"ring'
  30. EOD,
  31. 'oracle' => <<<EOD
  32. 'Co''mpl''''ex "st''"ring'
  33. EOD
  34. ];
  35. $this->assertEquals($expected[$type], $quotedString);
  36. }
  37. /**
  38. * @covers ::columnQuote()
  39. */
  40. public function testColumnQuote()
  41. {
  42. $this->assertEquals('"ColumnName"', $this->database->columnQuote("ColumnName"));
  43. $this->assertEquals('"Column"."name"', $this->database->columnQuote("Column.name"));
  44. $this->assertEquals('"Column"."Name"', $this->database->columnQuote("Column.Name"));
  45. }
  46. public function columnNamesProvider(): array
  47. {
  48. return [
  49. ["9ColumnName"],
  50. ["@ColumnName"],
  51. [".ColumnName"],
  52. ["ColumnName."],
  53. ["_ColumnName"],
  54. ["ColumnName (alias)"]
  55. ];
  56. }
  57. /**
  58. * @covers ::columnQuote()
  59. * @dataProvider columnNamesProvider
  60. */
  61. public function testIncorrectColumnQuote($column)
  62. {
  63. $this->expectException(InvalidArgumentException::class);
  64. $this->database->columnQuote($column);
  65. }
  66. /**
  67. * @covers ::tableQuote()
  68. */
  69. public function testTableQuote()
  70. {
  71. $this->assertEquals('"TableName"', $this->database->tableQuote("TableName"));
  72. }
  73. /**
  74. * @covers ::tableQuote()
  75. */
  76. public function testPrefixTableQuote()
  77. {
  78. $database = new Medoo([
  79. 'testMode' => true,
  80. 'prefix' => 'PREFIX_'
  81. ]);
  82. $this->assertEquals('"PREFIX_TableName"', $database->tableQuote("TableName"));
  83. }
  84. public function tableNamesProvider(): array
  85. {
  86. return [
  87. ["9TableName"],
  88. ["@TableName"],
  89. [".TableName"],
  90. ["TableName."],
  91. ["_TableName"],
  92. ["Table.name"]
  93. ];
  94. }
  95. /**
  96. * @covers ::tableQuote()
  97. * @dataProvider tableNamesProvider
  98. */
  99. public function testIncorrectTableQuote($table)
  100. {
  101. $this->expectException(InvalidArgumentException::class);
  102. $this->database->tableQuote($table);
  103. }
  104. }