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

helin16's avatar

Segmentation Fault, when phpunit with coverage

I had this piece of code, that trying to test of create an asset as a file:

/**
     * Testing creating a new asset from file.
     *
     * GIVEN a user is logged in with a valid token
     * WHEN a request is submitted to /api/asset/ with file
     * THEN the expected json should be returned.
     *
     * @test
     */
    public function createAssetSuccessfully()
    {
        //removing from database;
        factory(AssetType::class)->create($this->fake_asset_type);
        $this->assertCount(0, Asset::all());

        $expected_file_name = 'some_fake_file_name.txt';
        $upload_file = new UploadedFile($this->fake_tmp_file_absolute_path, $expected_file_name);

        // Trigger the upload
        $expected_type_id = $this->fake_asset_type['id'];
        $headers = ['authorization' => 'MY_AUTH_KEY ' . $this->fake_token];
        $server = $this->transformHeadersToServerVars($headers);
        $this->call('POST', '/api/asset/', ['type_id' => $expected_type_id], [], ['files' => [$upload_file]], $server);

        $this->assertResponseStatus(200);
        // Check in Database
        $assets = Asset::all();
        $this->assertCount(1, $assets);
        $this->seeInDatabase('assets', [
            'type_id' => $expected_type_id,
            'filename' => $expected_file_name,
        ]);
        // Check return
        $asset = $assets->first();
        $this->assertTrue(Storage::exists($asset->absolute_hashed_path));
        $this->assertEquals([[
            "filename" => $expected_file_name,
            "mimeType" => $asset->mimeType,
            "url" => $asset->url,
            'type_id' => $expected_type_id,
            'type' => $asset->type->toArray(),
        ]], json_decode($this->response->content(), true));
    }

When I run the test only, it's all good, like below:

# phpunit tests/API/AssetControllerTest.php 
PHPUnit 5.2.1 by Sebastian Bergmann and contributors.

.......                                                             7 / 7 (100%)

Time: 2.05 seconds, Memory: 28.00Mb

OK (7 tests, 17 assertions)

But when run with coverage, it always gives "Segmentation fault" like below:

phpunit tests/API/AssetControllerTest.php --coverage-html /tmp/test
PHPUnit 5.2.1 by Sebastian Bergmann and contributors.

....../usr/local/bin/phpunit: line 2: 58723 Segmentation fault: 11  /usr/bin/env php -d allow_url_fopen=On -d detect_unicode=Off /usr/local/Cellar/phpunit/5.2.1/libexec/phpunit-5.2.1.phar $*

Anyone had this before, or it's just me doing it wrong ?

0 likes
1 reply
ifpingram's avatar

@helin16 not sure what the issue could be, though in my experience SegFaults caused by code (and not extension bugs etc.) can be difficult to trace. I often just do a line by line debugging until I find the cause. Failing this, you can look into more advance debugging; here is a blog post that should help...

Please or to participate in this conversation.