DatabaseMySqlConnectionTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace Illuminate\Tests\Integration\Database\MySql;
  3. use Illuminate\Database\Schema\Blueprint;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Schema;
  6. /**
  7. * @requires extension pdo_mysql
  8. * @requires OS Linux|Darwin
  9. */
  10. class DatabaseMySqlConnectionTest extends MySqlTestCase
  11. {
  12. const TABLE = 'player';
  13. const FLOAT_COL = 'float_col';
  14. const JSON_COL = 'json_col';
  15. const FLOAT_VAL = 0.2;
  16. protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
  17. {
  18. if (! Schema::hasTable(self::TABLE)) {
  19. Schema::create(self::TABLE, function (Blueprint $table) {
  20. $table->json(self::JSON_COL)->nullable();
  21. $table->float(self::FLOAT_COL)->nullable();
  22. });
  23. }
  24. }
  25. protected function destroyDatabaseMigrations()
  26. {
  27. Schema::drop(self::TABLE);
  28. }
  29. /**
  30. * @dataProvider floatComparisonsDataProvider
  31. */
  32. public function testJsonFloatComparison($value, $operator, $shouldMatch)
  33. {
  34. DB::table(self::TABLE)->insert([self::JSON_COL => '{"rank":'.self::FLOAT_VAL.'}']);
  35. $this->assertSame(
  36. $shouldMatch,
  37. DB::table(self::TABLE)->where(self::JSON_COL.'->rank', $operator, $value)->exists(),
  38. self::JSON_COL.'->rank should '.($shouldMatch ? '' : 'not ')."be $operator $value"
  39. );
  40. }
  41. public function floatComparisonsDataProvider()
  42. {
  43. return [
  44. [0.2, '=', true],
  45. [0.2, '>', false],
  46. [0.2, '<', false],
  47. [0.1, '=', false],
  48. [0.1, '<', false],
  49. [0.1, '>', true],
  50. [0.3, '=', false],
  51. [0.3, '<', true],
  52. [0.3, '>', false],
  53. ];
  54. }
  55. public function testFloatValueStoredCorrectly()
  56. {
  57. DB::table(self::TABLE)->insert([self::FLOAT_COL => self::FLOAT_VAL]);
  58. $this->assertEquals(self::FLOAT_VAL, DB::table(self::TABLE)->value(self::FLOAT_COL));
  59. }
  60. /**
  61. * @dataProvider jsonWhereNullDataProvider
  62. */
  63. public function testJsonWhereNull($expected, $key, array $value = ['value' => 123])
  64. {
  65. DB::table(self::TABLE)->insert([self::JSON_COL => json_encode($value)]);
  66. $this->assertSame($expected, DB::table(self::TABLE)->whereNull(self::JSON_COL.'->'.$key)->exists());
  67. }
  68. /**
  69. * @dataProvider jsonWhereNullDataProvider
  70. */
  71. public function testJsonWhereNotNull($expected, $key, array $value = ['value' => 123])
  72. {
  73. DB::table(self::TABLE)->insert([self::JSON_COL => json_encode($value)]);
  74. $this->assertSame(! $expected, DB::table(self::TABLE)->whereNotNull(self::JSON_COL.'->'.$key)->exists());
  75. }
  76. public function jsonWhereNullDataProvider()
  77. {
  78. return [
  79. 'key not exists' => [true, 'invalid'],
  80. 'key exists and null' => [true, 'value', ['value' => null]],
  81. 'key exists and "null"' => [false, 'value', ['value' => 'null']],
  82. 'key exists and not null' => [false, 'value', ['value' => false]],
  83. 'nested key not exists' => [true, 'nested->invalid'],
  84. 'nested key exists and null' => [true, 'nested->value', ['nested' => ['value' => null]]],
  85. 'nested key exists and "null"' => [false, 'nested->value', ['nested' => ['value' => 'null']]],
  86. 'nested key exists and not null' => [false, 'nested->value', ['nested' => ['value' => false]]],
  87. ];
  88. }
  89. }