getMockBuilder(Table::class) ->disableOriginalConstructor() ->getMock(); $dbi = $this->getMockBuilder(DatabaseInterface::class) ->disableOriginalConstructor() ->getMock(); $dbi->expects($this->any())->method('getTable') ->will($this->returnValue($table)); $GLOBALS['dbi'] = $dbi; $this->_response = new ResponseStub(); $this->template = new Template(); } /** * Tests for getKeyForTablePrimary() * * Case one: there are no primary key in the table * * @return void * * @test */ public function testGetKeyForTablePrimaryOne() { $GLOBALS['dbi']->expects($this->any())->method('fetchAssoc') ->will($this->returnValue(null)); $class = new ReflectionClass(StructureController::class); $method = $class->getMethod('getKeyForTablePrimary'); $method->setAccessible(true); $relation = new Relation($GLOBALS['dbi'], $this->template); $ctrl = new StructureController( $this->_response, $GLOBALS['dbi'], $this->template, $GLOBALS['db'], $GLOBALS['table'], $relation, new Transformations(), new CreateAddField($GLOBALS['dbi']), new RelationCleanup($GLOBALS['dbi'], $relation) ); // No primary key in db.table2 $this->assertEquals( '', $method->invoke($ctrl) ); } /** * Tests for getKeyForTablePrimary() * * Case two: there are a primary key in the table * * @return void * * @test */ public function testGetKeyForTablePrimaryTwo() { $GLOBALS['dbi']->expects($this->any()) ->method('fetchAssoc') ->will( $this->returnCallback( function () { static $callCount = 0; if ($callCount == 0) { $callCount++; return [ 'Key_name' => 'PRIMARY', 'Column_name' => 'column', ]; } else { return null; } } ) ); $class = new ReflectionClass(StructureController::class); $method = $class->getMethod('getKeyForTablePrimary'); $method->setAccessible(true); $relation = new Relation($GLOBALS['dbi'], $this->template); $ctrl = new StructureController( $this->_response, $GLOBALS['dbi'], $this->template, $GLOBALS['db'], $GLOBALS['table'], $relation, new Transformations(), new CreateAddField($GLOBALS['dbi']), new RelationCleanup($GLOBALS['dbi'], $relation) ); // With db.table, it has a primary key `column` $this->assertEquals( 'column, ', $method->invoke($ctrl) ); } /** * Tests for adjustColumnPrivileges() * * @return void * * @test */ public function testAdjustColumnPrivileges() { $class = new ReflectionClass(StructureController::class); $method = $class->getMethod('adjustColumnPrivileges'); $method->setAccessible(true); $relation = new Relation($GLOBALS['dbi'], $this->template); $ctrl = new StructureController( $this->_response, $GLOBALS['dbi'], $this->template, $GLOBALS['db'], $GLOBALS['table'], $relation, new Transformations(), new CreateAddField($GLOBALS['dbi']), new RelationCleanup($GLOBALS['dbi'], $relation) ); $this->assertEquals( false, $method->invokeArgs($ctrl, [[]]) ); } /** * Tests for getMultipleFieldCommandType() * * @return void * * @test */ public function testGetMultipleFieldCommandType() { $class = new ReflectionClass(StructureController::class); $method = $class->getMethod('getMultipleFieldCommandType'); $method->setAccessible(true); $relation = new Relation($GLOBALS['dbi'], $this->template); $ctrl = new StructureController( $this->_response, $GLOBALS['dbi'], $this->template, $GLOBALS['db'], $GLOBALS['table'], $relation, new Transformations(), new CreateAddField($GLOBALS['dbi']), new RelationCleanup($GLOBALS['dbi'], $relation) ); $this->assertEquals( null, $method->invoke($ctrl) ); $_POST['submit_mult'] = 'create'; $this->assertEquals( 'create', $method->invoke($ctrl) ); unset($_POST['submit_mult']); $_POST['mult_btn'] = __('Yes'); $this->assertEquals( 'row_delete', $method->invoke($ctrl) ); $_POST['selected'] = [ 'a', 'b', ]; $method->invoke($ctrl); $this->assertEquals( $_POST['selected'], $_POST['selected_fld'] ); } }