diff options
author | Maurício Meneghini Fauth <mauricio@fauth.dev> | 2022-08-21 00:21:07 +0300 |
---|---|---|
committer | Maurício Meneghini Fauth <mauricio@fauth.dev> | 2022-08-21 00:21:07 +0300 |
commit | c36dd24b59ce26d49d3c388436dc1e36194e46cf (patch) | |
tree | c78f633203f67c1d1d88d8e7a67c57bc3253cd51 /test | |
parent | ec03bac629dc410c61a7dfd87c1019ea13f592f5 (diff) |
Add unit tests for Table\Structure\SpatialController class
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
Diffstat (limited to 'test')
-rw-r--r-- | test/classes/Controllers/Table/Structure/SpatialControllerTest.php | 131 |
1 files changed, 131 insertions, 0 deletions
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 @@ +<?php + +declare(strict_types=1); + +namespace PhpMyAdmin\Tests\Controllers\Table\Structure; + +use PhpMyAdmin\Controllers\Table\Structure\SpatialController; +use PhpMyAdmin\Controllers\Table\StructureController; +use PhpMyAdmin\Http\ServerRequest; +use PhpMyAdmin\Message; +use PhpMyAdmin\Template; +use PhpMyAdmin\Tests\AbstractTestCase; +use PhpMyAdmin\Tests\Stubs\ResponseRenderer; + +/** + * @covers \PhpMyAdmin\Controllers\Table\Structure\SpatialController + */ +class SpatialControllerTest extends AbstractTestCase +{ + public function testAddSpatialKeyToSingleField(): 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`);', []); + $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(); + } +} |