QuoteTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. $this->assertEquals('"ネーム"', $this->database->columnQuote("ネーム"));
  46. }
  47. public function columnNamesProvider(): array
  48. {
  49. return [
  50. ["9ColumnName"],
  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. $this->assertEquals('"_table"', $this->database->tableQuote("_table"));
  73. $this->assertEquals('"アカウント"', $this->database->tableQuote("アカウント"));
  74. }
  75. /**
  76. * @covers ::tableQuote()
  77. */
  78. public function testPrefixTableQuote()
  79. {
  80. $database = new Medoo([
  81. 'testMode' => true,
  82. 'prefix' => 'PREFIX_'
  83. ]);
  84. $this->assertEquals('"PREFIX_TableName"', $database->tableQuote("TableName"));
  85. }
  86. public function tableNamesProvider(): array
  87. {
  88. return [
  89. ["9TableName"],
  90. ["@TableName"],
  91. [".TableName"],
  92. ["TableName."],
  93. ["Table.name"]
  94. ];
  95. }
  96. /**
  97. * @covers ::tableQuote()
  98. * @dataProvider tableNamesProvider
  99. */
  100. public function testIncorrectTableQuote($table)
  101. {
  102. $this->expectException(InvalidArgumentException::class);
  103. $this->database->tableQuote($table);
  104. }
  105. }