Friday, March 17, 2017

Test if PHP `copy` can download file from internet

I have written a function, with an unit test, to download an image using PHP

// ...
if (!copy($url, $imagePath)) {
    return null;
}
// ...

It works locally but in Bitbucket Pipelines the unit test fails. The file couldn't be downloaded (file not found in storage).

It might has been disabled on purpose. So I would like to run this unit test only if copy() can download external files.

I tried this but didn't work:

public function test_downloadImage()
{
    if (!ini_get('allow_url_fopen') || in_array('copy', explode(',', ini_get('disable_functions')))) {
        return;
    }
    // download the image...
    // assert file exists...
}

How can I test if copy() can download external files?

Thank you.

Problem solved

Sorry for this but the problem didn't came from PHP copy().

It was trying to download an image to a non-existing directory. In fact, I forgot to setup the Laravel Public directory symbolic link. It was already setup on my computer.



via David

Advertisement