cakephp: Fixture insertion errors need better messaging
Description
When running unit tests with a table that has foreign key constraints (CakeDC/Users) the tests fail because it is unable to truncate the tables with foreign keys defined.
44) App\Test\TestCase\Model\Table\MyUsersTableTest::testBeforeSave
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`authenticon_test`.`social_accounts`, CONSTRAINT `social_accounts_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
/var/www/html/vendor/cakephp/cakephp/src/Database/Statement/MysqlStatement.php:39
/var/www/html/vendor/cakephp/cakephp/src/Database/Connection.php:337
/var/www/html/vendor/cakephp/cakephp/src/Core/Retry/CommandRetry.php:70
/var/www/html/vendor/cakephp/cakephp/src/Database/Connection.php:340
/var/www/html/vendor/cakephp/cakephp/src/Database/Query.php:250
/var/www/html/vendor/cakephp/cakephp/src/TestSuite/Fixture/TestFixture.php:343
/var/www/html/vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureHelper.php:139
/var/www/html/vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureHelper.php:121
/var/www/html/vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureHelper.php:134
/var/www/html/vendor/cakephp/cakephp/src/TestSuite/Fixture/TruncateStrategy.php:52
/var/www/html/vendor/cakephp/cakephp/src/TestSuite/TestCase.php:301
/var/www/html/vendor/cakephp/cakephp/src/TestSuite/TestCase.php:249
/var/www/html/tests/TestCase/Model/Table/MyUsersTableTest.php:41
CakePHP Version
4.3.1
PHP Version
8.0.12
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 24 (24 by maintainers)
Thanks @othercorey I probably would have never caught it with the SQL error message it presented. The funny thing is without the new Migrator the records are fine but thats due to the old schema not using FKs. Should I close this out since it’s more of a user issue than a framework issue.
It would be helpful to have more useful error messages.
@markstory We could catch PDO exceptions when inserting and add the name of the fixture to the error.
This issue is caused by an invalid user_id entry in the SocialAccountsFixture:
This user id is not in the UsersFixture records. if you replace it with the first user id from UsersFixture, the tests pass.
The example repo I setup is https://github.com/challgren/cakephp-16147