Skip to content

Commit

Permalink
Use a local web server to test Data::buildFromUrl() (prevent network …
Browse files Browse the repository at this point in the history
…errors in tests).
  • Loading branch information
gnutix committed Mar 31, 2020
1 parent 1d2f5e2 commit 5b10ac9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ sudo: false

before_script:
- composer install
- php -S localhost:8000 -t tests/

script:
- vendor/bin/phpunit
Expand Down
25 changes: 23 additions & 2 deletions tests/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,17 @@ public function testBuildFromFile()
*/
public function testBuildFromUrlShouldThrowFileNotFoundException()
{
$url = 'http://via.placeholder.com/x150.png';
$this->skipIfLocalWebServerDown();

$url = 'http://localhost:8000/unknown.png';
Data::buildFromUrl($url);
}

public function testBuildFromUrl()
{
$url = 'http://via.placeholder.com/350x150.png';
$this->skipIfLocalWebServerDown();

$url = 'http://localhost:8000/smile.png';
$dataURI = Data::buildFromUrl($url);
$this->assertInstanceOf('DataURI\Data', $dataURI);
$this->assertEquals('image/png', $dataURI->getMimeType());
Expand Down Expand Up @@ -185,4 +189,21 @@ private function createEmptyFile($filename)
fwrite($handle, '');
fclose($handle);
}

private function skipIfLocalWebServerDown()
{
$url = 'http://localhost:8000/';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if (0 === $httpcode) {
$this->markTestSkipped('The local web server is down. Run it with `php -S localhost:8000 -t tests/`.');
}
}
}

0 comments on commit 5b10ac9

Please sign in to comment.