Summer Sale! All accounts are 50% off this week.

navidbakhtiary's avatar

The same tests were successful in one project and failed in another

Hello friends. I created a custom Laravel package. Feature tests are used in the package. When running the tests on a Laravel project, all were successful and there were no failures or errors. In another project, the exact same package was installed using composer. Both Laravel projects are version 7.30.6 and both packages are the same. In the second project, some tests failed because the expected result and the actual result are slightly different.

in this failure 'tasks count' is integer in the expected result and in actual result is a string

Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'{"data":{"task":{"created at":"2022-06-14T08:41:27.000000Z","description":"Asperiores sed aspernatur quod. Pariatur sapiente voluptas ipsam consequatur ratione. Molestias harum quibusdam sint.","id":2,"labels":[{"id":1,"name":"Orlando Hessel Sr.","tasks count":2},{"id":2,"name":"Ms. Lupe Upton","tasks count":1}],"last updated at":"2022-06-14T08:41:27.000000Z","status":"Open","title":"Animi officiis modi qui maiores molestiae quibusdam cumque voluptatum."}}}'
+'{"data":{"task":{"created at":"2022-06-14T08:41:27.000000Z","description":"Asperiores sed aspernatur quod. Pariatur sapiente voluptas ipsam consequatur ratione. Molestias harum quibusdam sint.","id":2,"labels":[{"id":1,"name":"Orlando Hessel Sr.","tasks count":"2"},{"id":2,"name":"Ms. Lupe Upton","tasks count":"1"}],"last updated at":"2022-06-14T08:41:27.000000Z","status":"Open","title":"Animi officiis modi qui maiores molestiae quibusdam cumque voluptatum."}}}'

in another failure 'user_id' has a problem. again integer versus string happened.

Failed asserting that a row in the table [tasks] matches the attributes {
    "user_id": 2,
    "title": "Ullam explicabo aut quia necessitatibus.",
    "description": "Doloremque voluptatem dolorem delectus eligendi provident reiciendis quidem eum. Eum maxime dignissimos et et voluptas mollitia ratione.",    
    "status": "Open",
    "updated_at": "2022-06-14T08:41:27.000000Z",
    "created_at": "2022-06-14T08:41:27.000000Z",
    "id": 1
}.

Found similar results: [
    {
        "id": "1",
        "user_id": "2",
        "title": "Ullam explicabo aut quia necessitatibus.",
        "description": "Doloremque voluptatem dolorem delectus eligendi provident reiciendis quidem eum. Eum maxime dignissimos et et voluptas mollitia ratione.",        "status": "Open",
        "created_at": "2022-06-14 08:41:27",
        "updated_at": "2022-06-14 08:41:27"
    }
].

composer.json, composer.lock, phpunit.xml and ... are the same in two projects and packages. i used BeyondCompare software for comparing projects.

why in one project failure occurred and in another not happened. What can be the problem? how to solve it? thanks for your help.

0 likes
11 replies
navidbakhtiary's avatar

@sr57 Database structures are the same, and the same migrations are used to create tables in both.

sr57's avatar

How expected result is created?

Can you do the same test with expected result number quoted?

What differences in composer.json? composer-lock.json?

sr57's avatar

@navidbakhtiary

tried to do the test but, using Php 8, got too many error to continue. Sorry. Can you try on Php 8?

Warning:       Your XML configuration validates against a deprecated schema.
Suggestion:    Migrate your XML configuration using "--migrate-configuration"!

EEEEEEEEEEEEEEEEEEEEEEEEEEE                                       27 / 27 (100%)

Time: 00:00.181, Memory: 30.00 MB

There were 27 errors:

1) NavidBakhtiary\ToDo\Tests\Feature\LabelTest::testCreateLabelByAuthenticatedUser
Illuminate\Database\QueryException: could not find driver (SQL: PRAGMA foreign_keys = ON;)
...
navidbakhtiary's avatar

@sr57 Laravel and PHP both are version 7.* in my project and package. I think if changed to laravel 8 and PHP 8 some of the codes must be refactored.

devingray_'s avatar

Your PHP unit XML uses a different Database connection than the tests, consider updating to use a database the same as the project instead of sqlite

<php>
        <server name="APP_ENV" value="testing"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
        <server name="DB_CONNECTION" value="sqlite"/>
        <server name="DB_DATABASE" value=":memory:"/>
        <server name="MAIL_MAILER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
    </php>
sr57's avatar

@devingray_

Good point, but OP wrote that it was the same test in 2 two different projects with the same package and same composer.lock.

navidbakhtiary's avatar

Thanks to @sr57 and @devingray_ for helping me.

I installed the ToDo package on the fresh laravel 7. * project. I created a new MySQL database. Ran tests and the same failures occurred and other tests were passed. I changed the value of DB_DATABASE in .env. ran the php artisan config:cache command. All tests failed because no new database was created in MySQL. I changed the value of DB_DATABASE to the previous value again. The php artisan config:cache command was ran again. This time all the tests were passed.

what does it mean? For what? I'm confused.

sr57's avatar

@navidbakhtiary

what does it mean?

No idea ...

Next time you get'll a "strange pb", do a complete backup of your app (codes & dbs) to be able to :

  • redo anytime the pb
  • simplify your app to isolate the pb and to remove any private date
  • be able to share this image
1 like

Please or to participate in this conversation.