From c36dd24b59ce26d49d3c388436dc1e36194e46cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Sat, 20 Aug 2022 18:21:07 -0300 Subject: Add unit tests for Table\Structure\SpatialController class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MaurĂ­cio Meneghini Fauth --- .../Table/Structure/SpatialControllerTest.php | 131 +++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 test/classes/Controllers/Table/Structure/SpatialControllerTest.php (limited to 'test') diff --git a/test/classes/Controllers/Table/Structure/SpatialControllerTest.php b/test/classes/Controllers/Table/Structure/SpatialControllerTest.php new file mode 100644 index 0000000000..67c7266584 --- /dev/null +++ b/test/classes/Controllers/Table/Structure/SpatialControllerTest.php @@ -0,0 +1,131 @@ +createDbiDummy(); + $dbiDummy->addSelectDb('test_db'); + $dbiDummy->addResult('ALTER TABLE `test_table` ADD SPATIAL(`test_field`);', []); + $dbi = $this->createDatabaseInterface($dbiDummy); + $GLOBALS['dbi'] = $dbi; + $request = $this->createStub(ServerRequest::class); + $controllerStub = $this->createMock(StructureController::class); + $controllerStub->expects($this->once())->method('__invoke')->with($request); + + $controller = new SpatialController(new ResponseRenderer(), new Template(), $dbi, $controllerStub); + $controller($request); + + $this->assertEquals(Message::success(), $GLOBALS['message']); + /** @psalm-suppress TypeDoesNotContainType */ + $this->assertSame('ALTER TABLE `test_table` ADD SPATIAL(`test_field`);', $GLOBALS['sql_query']); + $dbiDummy->assertAllSelectsConsumed(); + $dbiDummy->assertAllQueriesConsumed(); + } + + public function testAddSpatialKeyToMultipleFields(): void + { + $GLOBALS['db'] = 'test_db'; + $GLOBALS['table'] = 'test_table'; + $GLOBALS['message'] = null; + $GLOBALS['sql_query'] = null; + $_POST['selected_fld'] = ['test_field1', 'test_field2']; + + $dbiDummy = $this->createDbiDummy(); + $dbiDummy->addSelectDb('test_db'); + $dbiDummy->addResult('ALTER TABLE `test_table` ADD SPATIAL(`test_field1`, `test_field2`);', []); + $dbi = $this->createDatabaseInterface($dbiDummy); + $GLOBALS['dbi'] = $dbi; + $request = $this->createStub(ServerRequest::class); + $controllerStub = $this->createMock(StructureController::class); + $controllerStub->expects($this->once())->method('__invoke')->with($request); + + $controller = new SpatialController(new ResponseRenderer(), new Template(), $dbi, $controllerStub); + $controller($request); + + $this->assertEquals(Message::success(), $GLOBALS['message']); + /** @psalm-suppress TypeDoesNotContainType */ + $this->assertSame('ALTER TABLE `test_table` ADD SPATIAL(`test_field1`, `test_field2`);', $GLOBALS['sql_query']); + $dbiDummy->assertAllSelectsConsumed(); + $dbiDummy->assertAllQueriesConsumed(); + } + + public function testNoColumnsSelected(): void + { + $GLOBALS['db'] = 'test_db'; + $GLOBALS['table'] = 'test_table'; + $GLOBALS['message'] = null; + $GLOBALS['sql_query'] = null; + $_POST['selected_fld'] = null; + + $dbi = $this->createDatabaseInterface(); + $GLOBALS['dbi'] = $dbi; + $request = $this->createStub(ServerRequest::class); + $controllerStub = $this->createMock(StructureController::class); + $controllerStub->expects($this->never())->method('__invoke'); + $response = new ResponseRenderer(); + + $controller = new SpatialController($response, new Template(), $dbi, $controllerStub); + $controller($request); + + $this->assertFalse($response->hasSuccessState()); + $this->assertSame(['message' => 'No column selected.'], $response->getJSONResult()); + /** @psalm-suppress RedundantCondition */ + $this->assertNull($GLOBALS['message']); + /** @psalm-suppress RedundantCondition */ + $this->assertNull($GLOBALS['sql_query']); + } + + public function testAddSpatialKeyWithError(): void + { + $GLOBALS['db'] = 'test_db'; + $GLOBALS['table'] = 'test_table'; + $GLOBALS['message'] = null; + $GLOBALS['sql_query'] = null; + $_POST['selected_fld'] = ['test_field']; + + $dbiDummy = $this->createDbiDummy(); + $dbiDummy->addSelectDb('test_db'); + $dbiDummy->addResult('ALTER TABLE `test_table` ADD SPATIAL(`test_field`);', false); + $dbiDummy->addErrorCode('#1210 - Incorrect arguments to SPATIAL INDEX'); + $dbi = $this->createDatabaseInterface($dbiDummy); + $GLOBALS['dbi'] = $dbi; + $request = $this->createStub(ServerRequest::class); + $controllerStub = $this->createMock(StructureController::class); + $controllerStub->expects($this->once())->method('__invoke')->with($request); + + $controller = new SpatialController(new ResponseRenderer(), new Template(), $dbi, $controllerStub); + $controller($request); + + $this->assertEquals( + Message::error('#1210 - Incorrect arguments to SPATIAL INDEX'), + $GLOBALS['message'] + ); + /** @psalm-suppress TypeDoesNotContainType */ + $this->assertSame('ALTER TABLE `test_table` ADD SPATIAL(`test_field`);', $GLOBALS['sql_query']); + $dbiDummy->assertAllSelectsConsumed(); + $dbiDummy->assertAllQueriesConsumed(); + $dbiDummy->assertAllErrorCodesConsumed(); + } +} -- cgit v1.2.3