From 0c5880558423db9bb49fd4095e1efc9f6bfd5007 Mon Sep 17 00:00:00 2001 From: Bart Vanhoutte Date: Mon, 11 Jul 2022 11:21:20 +0200 Subject: [PATCH] Flattened promises in tests. --- tests/ConnectionTest.php | 172 +++++++++++++++++++-------------------- 1 file changed, 84 insertions(+), 88 deletions(-) diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 7c1c82c..89d54c4 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -162,25 +162,25 @@ public function testQuery() 'id' => '1', 'field1' => 'val1', 'field2' => 'val2', - ]) - ->then(function () use ($connection) { - return $connection - ->query($connection - ->createQueryBuilder() - ->select('*') - ->from('test', 't') - ->where('t.id = ?') - ->setParameters(['1']) - ->setMaxResults(1) - ); - }) - ->then(function (Result $result) { - $this->assertEquals($result->fetchFirstRow(), [ - 'id' => '1', - 'field1' => 'val1', - 'field2' => 'val2', - ]); - }); + ]); + }) + ->then(function () use ($connection) { + return $connection + ->query($connection + ->createQueryBuilder() + ->select('*') + ->from('test', 't') + ->where('t.id = ?') + ->setParameters(['1']) + ->setMaxResults(1) + ); + }) + ->then(function (Result $result) { + $this->assertEquals($result->fetchFirstRow(), [ + 'id' => '1', + 'field1' => 'val1', + 'field2' => 'val2', + ]); }); await($promise, $loop, self::MAX_TIMEOUT); @@ -196,26 +196,24 @@ public function testMultipleRows() $promise = $this ->resetInfrastructure($connection) ->then(function (Connection $connection) { - return $connection - ->insert('test', [ - 'id' => '1', - 'field1' => 'val11', - 'field2' => 'val12', - ]) - ->then(function () use ($connection) { - return $connection->insert('test', [ - 'id' => '2', - 'field1' => 'val21', - 'field2' => 'val22', - ]); - }) - ->then(function () use ($connection) { - return $connection->insert('test', [ - 'id' => '3', - 'field1' => 'val31', - 'field2' => 'val32', - ]); - }); + return $connection->insert('test', [ + 'id' => '1', + 'field1' => 'val11', + 'field2' => 'val12', + ]); + })->then(function () use ($connection) { + return $connection->insert('test', [ + 'id' => '2', + 'field1' => 'val21', + 'field2' => 'val22', + ]); + }) + ->then(function () use ($connection) { + return $connection->insert('test', [ + 'id' => '3', + 'field1' => 'val31', + 'field2' => 'val32', + ]); }) ->then(function () use ($connection) { $queryBuilder = $connection->createQueryBuilder(); @@ -287,46 +285,46 @@ public function testFindShortcut() 'field1' => 'valX', 'field2' => 'val2', ]), - ]) - ->then(function () use ($connection) { - return all([ - $connection->findOneBy('test', [ - 'id' => '1', - ]), - $connection->findOneBy('test', [ - 'id' => '999', - ]), - $connection->findBy('test', [ - 'field1' => 'val1', - ]), - ]); - }) - ->then(function (array $results) { - $this->assertEquals($results[0], [ + ]); + }) + ->then(function () use ($connection) { + return all([ + $connection->findOneBy('test', [ 'id' => '1', + ]), + $connection->findOneBy('test', [ + 'id' => '999', + ]), + $connection->findBy('test', [ 'field1' => 'val1', - 'field2' => 'val1', - ]); - - $this->assertNull($results[1]); - $listResults = $results[2]; - usort($listResults, function ($a1, $a2) { - return $a1['id'] > $a2['id']; - }); + ]), + ]); + }) + ->then(function (array $results) { + $this->assertEquals($results[0], [ + 'id' => '1', + 'field1' => 'val1', + 'field2' => 'val1', + ]); - $this->assertSame($listResults, [ - [ - 'id' => '1', - 'field1' => 'val1', - 'field2' => 'val1', - ], - [ - 'id' => '2', - 'field1' => 'val1', - 'field2' => 'val2', - ], - ]); + $this->assertNull($results[1]); + $listResults = $results[2]; + usort($listResults, function ($a1, $a2) { + return $a1['id'] > $a2['id']; }); + + $this->assertSame($listResults, [ + [ + 'id' => '1', + 'field1' => 'val1', + 'field2' => 'val1', + ], + [ + 'id' => '2', + 'field1' => 'val1', + 'field2' => 'val2', + ], + ]); }); await($promise, $loop, self::MAX_TIMEOUT); @@ -376,19 +374,17 @@ public function testInsertTwice() $promise = $this ->resetInfrastructure($connection) ->then(function (Connection $connection) { - return $connection - ->insert('test', [ - 'id' => '1', - 'field1' => 'val1', - 'field2' => 'val1', - ]) - ->then(function () use ($connection) { - return $connection->insert('test', [ - 'id' => '1', - 'field1' => 'val1', - 'field2' => 'val1', - ]); - }); + return $connection->insert('test', [ + 'id' => '1', + 'field1' => 'val1', + 'field2' => 'val1', + ]); + })->then(function () use ($connection) { + return $connection->insert('test', [ + 'id' => '1', + 'field1' => 'val1', + 'field2' => 'val1', + ]); }); $this->expectException(UniqueConstraintViolationException::class);