Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Reppair's avatar
Level 13

PHPUnit database assertion fails in PHP Storm but NOT in terminal

When I run phpunit from the terminal my assertion passes and when run in PHP Storm it fails? What am I missing?

Test

/** @test */
public function the_page_factory_is_working()
{
    $page = factory(Page::class)->create();

    $this->assertDatabaseHas('pages', $page->getAttributes());
}

Console

outland:base martin$ vendor/phpunit/phpunit/phpunit tests
PHPUnit 7.0.2 by Sebastian Bergmann and contributors.

.....                                                               5 / 5 (100%)

Time: 294 ms, Memory: 18.00MB

OK (5 tests, 13 assertions)

PHP Storm

Testing started at 0:25 ...
/usr/local/bin/php /Users/martin/dev/oxygen/base/vendor/phpunit/phpunit/phpunit --no-configuration Tests\Feature\PageTest /Users/martin/dev/oxygen/base/tests/Feature/PageTest.php --teamcity
PHPUnit 7.0.2 by Sebastian Bergmann and contributors.


Failed asserting that a row in the table [pages] matches the attributes {
    "slug": "{\"bg\":\"ducimus-et-ipsum-labore-non-voluptatem-mollitia-ullam\",\"en\":\"dolorem-ullam-rerum-nobis-amet-facere-veniam-qui\"}",
    "title": "{\"bg\":\"\u0411\u044a\u043b\u0433\u0430\u0440\u0441\u043a\u0438 - Aut autem voluptatum excepturi iusto excepturi odit id.\",\"en\":\"English - Est illo quo voluptatum.\"}",
    "summary": "{\"bg\":\"\u0411\u044a\u043b\u0433\u0430\u0440\u0441\u043a\u0438 - Magnam id molestiae aut debitis expedita. Aut ut illum quibusdam amet incidunt doloribus fugit. Libero provident quaerat aut sed et officia nihil perspiciatis.\",\"en\":\"English - Similique doloremque quia sequi non nam nostrum et quod. Ut doloribus ut laborum tempore aut quidem. A officiis aut nam ad voluptatibus cum perspiciatis.\"}",
    "body": "{\"bg\":\"\u0411\u044a\u043b\u0433\u0430\u0440\u0441\u043a\u0438 - Aperiam pariatur totam quam voluptas molestiae. Ipsa distinctio occaecati amet voluptates modi voluptatem. Unde numquam suscipit perferendis sapiente quod vero ea incidunt. Quidem minus architecto nulla rem error ducimus adipisci.\",\"en\":\"English - In quis facere ut. Qui consequatur ipsa praesentium. Suscipit et labore error earum dolore beatae eius.\"}",
    "updated_at": "2018-03-29 21:25:14",
    "created_at": "2018-03-29 21:25:14",
    "id": 1
}.

Found: [
    {
        "id": 1,
        "template": "default.blade.php",
        "slug": "{\"bg\": \"ducimus-et-ipsum-labore-non-voluptatem-mollitia-ullam\", \"en\": \"dolorem-ullam-rerum-nobis-amet-facere-veniam-qui\"}",
        "title": "{\"bg\": \"\u0411\u044a\u043b\u0433\u0430\u0440\u0441\u043a\u0438 - Aut autem voluptatum excepturi iusto excepturi odit id.\", \"en\": \"English - Est illo quo voluptatum.\"}",
        "summary": "{\"bg\": \"\u0411\u044a\u043b\u0433\u0430\u0440\u0441\u043a\u0438 - Magnam id molestiae aut debitis expedita. Aut ut illum quibusdam amet incidunt doloribus fugit. Libero provident quaerat aut sed et officia nihil perspiciatis.\", \"en\": \"English - Similique doloremque quia sequi non nam nostrum et quod. Ut doloribus ut laborum tempore aut quidem. A officiis aut nam ad voluptatibus cum perspiciatis.\"}",
        "body": "{\"bg\": \"\u0411\u044a\u043b\u0433\u0430\u0440\u0441\u043a\u0438 - Aperiam pariatur totam quam voluptas molestiae. Ipsa distinctio occaecati amet voluptates modi voluptatem. Unde numquam suscipit perferendis sapiente quod vero ea incidunt. Quidem minus architecto nulla rem error ducimus adipisci.\", \"en\": \"English - In quis facere ut. Qui consequatur ipsa praesentium. Suscipit et labore error earum dolore beatae eius.\"}",
        "created_at": "2018-03-29 21:25:14",
        "updated_at": "2018-03-29 21:25:14"
    }
].
 /Users/martin/dev/oxygen/base/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php:22
 /Users/martin/dev/oxygen/base/tests/Feature/PageTest.php:19
 


Time: 380 ms, Memory: 18.00MB


FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

Process finished with exit code 1
0 likes
5 replies
tykus's avatar

There is a space between the JSON keys and values in the found version, but not in the search version, e.g.

"summary": "{"bg": "\u... // found
"summary": "{"bg":"\u... //search

So, the strings are not the same.

1 like
Reppair's avatar
Level 13

OK fine but why it is passing when I run the test from the terminal then? same phpunit executable same environment..? I just don't get it.. if it fails it fails if it passes it passes.. why not failing in the terminal as well?

Reppair's avatar
Level 13

BTW another interesting thing is that if I change the order of the language keys in the factory so the the EN key is first and the BG key second the compared data is totally different e.g.

"summary": {"bg": "lorem", "en": "lorem"} // found
"summary": {"en": "lorem", "bg": "lorem"} // search

BUT it also passes to green when I execute phpunit from the the terminal..

shez1983's avatar

does it fail everytime? I ask because sometimes for me test sometimes passes and other times fails because of my incorrect SETUP routine for the test..

but it is indeed really puzzling.. maybe instead of creating, use one already in the db and echo it out and see what phpstorms dumps out and what terminal dumps out..

Please or to participate in this conversation.