Skip to content

Commit 3a5a385

Browse files
committed
Refactor ExportSqlTest to remove withConsecutive method
The PHPUnit's InvocationMocker::withConsecutive() has been deprecated. Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent d10b5d9 commit 3a5a385

2 files changed

Lines changed: 52 additions & 144 deletions

File tree

psalm-baseline.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17523,10 +17523,6 @@
1752317523
</PossiblyInvalidArrayOffset>
1752417524
</file>
1752517525
<file src="test/classes/Plugins/Export/ExportSqlTest.php">
17526-
<DeprecatedMethod>
17527-
<code>withConsecutive</code>
17528-
<code>withConsecutive</code>
17529-
</DeprecatedMethod>
1753017526
<InvalidArrayOffset>
1753117527
<code><![CDATA[$GLOBALS['no_constraints_comments']]]></code>
1753217528
<code><![CDATA[$GLOBALS['no_constraints_comments']]]></code>

test/classes/Plugins/Export/ExportSqlTest.php

Lines changed: 52 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -780,111 +780,64 @@ public function testGetTableDef(): void
780780
unset($GLOBALS['no_constraints_comments']);
781781
}
782782

783-
$resultStub = $this->createMock(DummyResult::class);
784-
785-
$dbi = $this->getMockBuilder(DatabaseInterface::class)
786-
->disableOriginalConstructor()
787-
->getMock();
788-
789-
$dbi->expects($this->any())
790-
->method('query')
791-
->will($this->returnValue($resultStub));
792-
793-
$dbi->expects($this->never())
794-
->method('fetchSingleRow');
795-
796-
$resultStub->expects($this->once())
797-
->method('numRows')
798-
->will($this->returnValue(1));
799-
800-
$dbi->expects($this->any())
801-
->method('fetchValue')
802-
->will($this->returnValue(false));
803-
804-
$tmpres = [
805-
'Auto_increment' => 1,
806-
'Create_time' => '2000-01-01 10:00:00',
807-
'Update_time' => '2000-01-02 12:00:00',
808-
'Check_time' => '2000-01-02 13:00:00',
809-
];
810-
811-
$resultStub->expects($this->once())
812-
->method('fetchAssoc')
813-
->will($this->returnValue($tmpres));
814-
815-
$dbi->expects($this->exactly(3))
816-
->method('tryQuery')
817-
->withConsecutive(
818-
["SHOW TABLE STATUS FROM `db` WHERE Name = 'table'"],
819-
['USE `db`'],
820-
['SHOW CREATE TABLE `db`.`table`']
821-
)
822-
->willReturnOnConsecutiveCalls($resultStub, $resultStub, $resultStub);
823-
824-
$row = [
825-
'',
826-
"CREATE TABLE `table` (\n" .
827-
"`payment_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,\n" .
828-
"`customer_id` smallint(5) unsigned NOT NULL,\n" .
829-
"`staff_id` tinyint(3) unsigned NOT NULL,\n" .
830-
"`rental_id` int(11) DEFAULT NULL,\n" .
831-
"`amount` decimal(5,2) NOT NULL,\n" .
832-
"`payment_date` datetime NOT NULL,\n" .
833-
"`last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n" .
834-
"PRIMARY KEY (`payment_id`),\n" .
835-
"KEY `idx_fk_staff_id` (`staff_id`),\n" .
836-
"KEY `idx_fk_customer_id` (`customer_id`),\n" .
837-
"KEY `fk_payment_rental` (`rental_id`),\n" .
838-
'CONSTRAINT `fk_payment_customer` FOREIGN KEY (`customer_id`) REFERENCES' .
839-
" `customer` (`customer_id`) ON UPDATE CASCADE,\n" .
840-
'CONSTRAINT `fk_payment_rental` FOREIGN KEY (`rental_id`) REFERENCES' .
841-
" `rental` (`rental_id`) ON DELETE SET NULL ON UPDATE CASCADE,\n" .
842-
'CONSTRAINT `fk_payment_staff` FOREIGN KEY (`staff_id`) REFERENCES' .
843-
" `staff` (`staff_id`) ON UPDATE CASCADE\n" .
844-
") ENGINE=InnoDB AUTO_INCREMENT=16050 DEFAULT CHARSET=utf8\n",
845-
];
846-
847-
$resultStub->expects($this->exactly(1))
848-
->method('fetchRow')
849-
->will($this->returnValue($row));
850-
851-
$dbi->expects($this->exactly(2))
852-
->method('getTable')
853-
->will($this->returnValue(new Table('table', 'db', $dbi)));
854-
$dbi->expects($this->any())->method('quoteString')
855-
->will($this->returnCallback(static function (string $string) {
856-
return "'" . $string . "'";
857-
}));
783+
$createTableStatement = <<<'SQL'
784+
CREATE TABLE `table` (
785+
`payment_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
786+
`customer_id` smallint(5) unsigned NOT NULL,
787+
`staff_id` tinyint(3) unsigned NOT NULL,
788+
`rental_id` int(11) DEFAULT NULL,
789+
`amount` decimal(5,2) NOT NULL,
790+
`payment_date` datetime NOT NULL,
791+
`last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
792+
PRIMARY KEY (`payment_id`),
793+
KEY `idx_fk_staff_id` (`staff_id`),
794+
KEY `idx_fk_customer_id` (`customer_id`),
795+
KEY `fk_payment_rental` (`rental_id`),
796+
CONSTRAINT `fk_payment_customer`
797+
FOREIGN KEY (`customer_id`) REFERENCES `customer` (`customer_id`) ON UPDATE CASCADE,
798+
CONSTRAINT `fk_payment_rental`
799+
FOREIGN KEY (`rental_id`) REFERENCES `rental` (`rental_id`) ON DELETE SET NULL ON UPDATE CASCADE,
800+
CONSTRAINT `fk_payment_staff`
801+
FOREIGN KEY (`staff_id`) REFERENCES `staff` (`staff_id`) ON UPDATE CASCADE
802+
) ENGINE=InnoDB AUTO_INCREMENT=16050 DEFAULT CHARSET=utf8
803+
SQL;
804+
$isViewQuery = 'SELECT 1 FROM information_schema.VIEWS WHERE TABLE_SCHEMA = \'db\' AND TABLE_NAME = \'table\'';
805+
806+
$dbiDummy = $this->createDbiDummy();
807+
// phpcs:disable Generic.Files.LineLength.TooLong
808+
$dbiDummy->addResult(
809+
'SHOW TABLE STATUS FROM `db` WHERE Name = \'table\'',
810+
[['table', 'InnoDB', '10', 'Dynamic', '3', '5461', '16384', '0', '0', '0', '1', '2000-01-01 10:00:00', '2000-01-02 12:00:00', '2000-01-02 13:00:00', 'utf8mb4_general_ci', null, '', '', '0', 'N']],
811+
['Name', 'Engine', 'Version', 'Row_format', 'Rows', 'Avg_row_length', 'Data_length', 'Max_data_length', 'Index_length', 'Data_free', 'Auto_increment', 'Create_time', 'Update_time', 'Check_time', 'Collation', 'Checksum', 'Create_options', 'Comment', 'Max_index_length', 'Temporary'],
812+
);
813+
// phpcs:enable
814+
$dbiDummy->addResult($isViewQuery, []);
815+
$dbiDummy->addResult($isViewQuery, []);
816+
$dbiDummy->addResult('USE `db`', []);
817+
$dbiDummy->addResult(
818+
'SHOW CREATE TABLE `db`.`table`',
819+
[['table', $createTableStatement]],
820+
['Table', 'Create Table'],
821+
);
858822

859-
$GLOBALS['dbi'] = $dbi;
823+
$GLOBALS['dbi'] = $this->createDatabaseInterface($dbiDummy);
860824
$GLOBALS['cfg']['Server']['DisableIS'] = false;
861825

862826
$result = $this->object->getTableDef('db', 'table', true, true, false);
863827

828+
$dbiDummy->assertAllQueriesConsumed();
864829
$this->assertStringContainsString('-- Creation: Jan 01, 2000 at 10:00 AM', $result);
865-
866830
$this->assertStringContainsString('-- Last update: Jan 02, 2000 at 12:00 PM', $result);
867-
868831
$this->assertStringContainsString('-- Last check: Jan 02, 2000 at 01:00 PM', $result);
869-
870832
$this->assertStringContainsString('DROP TABLE IF EXISTS `table`;', $result);
871-
872833
$this->assertStringContainsString('CREATE TABLE `table`', $result);
873-
874834
$this->assertStringContainsString('-- Constraints for dumped tables', $GLOBALS['sql_constraints']);
875-
876835
$this->assertStringContainsString('-- Constraints for table "table"', $GLOBALS['sql_constraints']);
877-
878836
$this->assertStringContainsString('ALTER TABLE "table"', $GLOBALS['sql_constraints']);
879-
880837
$this->assertStringContainsString('ADD CONSTRAINT', $GLOBALS['sql_constraints']);
881-
882838
$this->assertStringContainsString('ALTER TABLE "table"', $GLOBALS['sql_constraints_query']);
883-
884839
$this->assertStringContainsString('ADD CONSTRAINT', $GLOBALS['sql_constraints_query']);
885-
886840
$this->assertStringContainsString('ALTER TABLE "table"', $GLOBALS['sql_drop_foreign_keys']);
887-
888841
$this->assertStringContainsString('DROP FOREIGN KEY', $GLOBALS['sql_drop_foreign_keys']);
889842
}
890843

@@ -905,64 +858,23 @@ public function testGetTableDefWithError(): void
905858
unset($GLOBALS['no_constraints_comments']);
906859
}
907860

908-
$resultStub = $this->createMock(DummyResult::class);
909-
910-
$dbi = $this->getMockBuilder(DatabaseInterface::class)
911-
->disableOriginalConstructor()
912-
->getMock();
913-
914-
$dbi->expects($this->any())
915-
->method('query')
916-
->will($this->returnValue($resultStub));
917-
918-
$dbi->expects($this->never())
919-
->method('fetchSingleRow');
920-
921-
$resultStub->expects($this->once())
922-
->method('numRows')
923-
->will($this->returnValue(2));
924-
925-
$dbi->expects($this->any())
926-
->method('fetchValue')
927-
->will($this->returnValue(false));
861+
$isViewQuery = 'SELECT 1 FROM information_schema.VIEWS WHERE TABLE_SCHEMA = \'db\' AND TABLE_NAME = \'table\'';
928862

929-
$tmpres = [
930-
'Auto_increment' => 1,
931-
'Create_time' => '2000-01-01 10:00:00',
932-
'Update_time' => '2000-01-02 12:00:00',
933-
'Check_time' => '2000-01-02 13:00:00',
934-
];
863+
$dbiDummy = $this->createDbiDummy();
864+
$dbiDummy->addResult('SHOW TABLE STATUS FROM `db` WHERE Name = \'table\'', []);
865+
$dbiDummy->addResult($isViewQuery, []);
866+
$dbiDummy->addResult($isViewQuery, []);
867+
$dbiDummy->addResult('USE `db`', []);
868+
$dbiDummy->addResult('SHOW CREATE TABLE `db`.`table`', []);
869+
$dbiDummy->addErrorCode('error occurred');
935870

936-
$resultStub->expects($this->once())
937-
->method('fetchAssoc')
938-
->will($this->returnValue($tmpres));
939-
940-
$dbi->expects($this->exactly(3))
941-
->method('tryQuery')
942-
->withConsecutive(
943-
["SHOW TABLE STATUS FROM `db` WHERE Name = 'table'"],
944-
['USE `db`'],
945-
['SHOW CREATE TABLE `db`.`table`']
946-
)
947-
->willReturnOnConsecutiveCalls($resultStub, $resultStub, $resultStub);
948-
949-
$dbi->expects($this->once())
950-
->method('getError')
951-
->will($this->returnValue('error occurred'));
952-
953-
$dbi->expects($this->exactly(2))
954-
->method('getTable')
955-
->will($this->returnValue(new Table('table', 'db', $dbi)));
956-
$dbi->expects($this->any())->method('quoteString')
957-
->will($this->returnCallback(static function (string $string) {
958-
return "'" . $string . "'";
959-
}));
960-
961-
$GLOBALS['dbi'] = $dbi;
871+
$GLOBALS['dbi'] = $this->createDatabaseInterface($dbiDummy);
962872
$GLOBALS['cfg']['Server']['DisableIS'] = false;
963873

964874
$result = $this->object->getTableDef('db', 'table', true, true, false);
965875

876+
$dbiDummy->assertAllQueriesConsumed();
877+
$dbiDummy->assertAllErrorCodesConsumed();
966878
$this->assertStringContainsString('-- Error reading structure for table db.table: error occurred', $result);
967879
}
968880

0 commit comments

Comments
 (0)