From 11b51fa35630e9bd2fa8fe3e8d871f0950ecf1df Mon Sep 17 00:00:00 2001 From: Chekote Date: Sun, 12 Jan 2020 11:09:27 -0600 Subject: [PATCH] Add integration tests --- .../Database/EloquentMySqlConnectionTest.php | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100755 tests/Integration/Database/EloquentMySqlConnectionTest.php diff --git a/tests/Integration/Database/EloquentMySqlConnectionTest.php b/tests/Integration/Database/EloquentMySqlConnectionTest.php new file mode 100755 index 000000000000..1628f66930ca --- /dev/null +++ b/tests/Integration/Database/EloquentMySqlConnectionTest.php @@ -0,0 +1,91 @@ +set('app.debug', 'true'); + + // Database configuration + $app['config']->set('database.default', 'testbench'); + + $app['config']->set('database.connections.testbench', [ + 'driver' => 'mysql', + 'host' => env('DB_HOST', '127.0.0.1'), + 'username' => 'root', + 'password' => '', + 'database' => 'forge', + 'prefix' => '', + ]); + } + + protected function setUp(): void + { + parent::setUp(); + + if (! Schema::hasTable(self::TABLE)) { + Schema::create(self::TABLE, function (Blueprint $table) { + $table->json(self::JSON_COL)->nullable(); + $table->float(self::FLOAT_COL)->nullable(); + }); + } + } + + protected function tearDown(): void + { + DB::table(self::TABLE)->truncate(); + + parent::tearDown(); + } + + /** + * @dataProvider floatComparisonsDataProvider + * + * @param float $value the value to compare against the JSON value + * @param string $operator the comparison operator to use. e.g. '<', '>', '=' + * @param bool $shouldMatch true if the comparison should match, false if not + */ + public function testJsonFloatComparison(float $value, string $operator, bool $shouldMatch): void + { + DB::table(self::TABLE)->insert([self::JSON_COL => '{"rank":'.self::FLOAT_VAL.'}']); + $this->assertSame( + $shouldMatch, + DB::table(self::TABLE)->where(self::JSON_COL . '->rank', $operator, $value)->exists(), + self::JSON_COL . '->rank should ' . ($shouldMatch ? '' : 'not ') . "be $operator $value" + ); + } + + public function floatComparisonsDataProvider(): array + { + return [ + [0.2, '=', true], + [0.2, '>', false], + [0.2, '<', false], + [0.1, '=', false], + [0.1, '<', false], + [0.1, '>', true], + [0.3, '=', false], + [0.3, '<', true], + [0.3, '>', false], + ]; + } + + public function testFloatValueStoredCorrectly(): void + { + DB::table(self::TABLE)->insert([self::FLOAT_COL => self::FLOAT_VAL]); + $this->assertEquals(self::FLOAT_VAL, DB::table(self::TABLE)->value(self::FLOAT_COL)); + } +}