Skip to content

Commit

Permalink
Fixes matomo-org#6651 Adding better health checks to the tests when d…
Browse files Browse the repository at this point in the history
…ownloading/creating files
  • Loading branch information
mattab committed Dec 2, 2014
1 parent c091edf commit 1393a9c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
38 changes: 25 additions & 13 deletions tests/PHPUnit/Fixtures/SqlDump.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,12 @@ public function setUp()
$dumpPath = $this->dumpUrl;
} else {
$dumpPath = PIWIK_INCLUDE_PATH . '/tmp/logdump.sql.gz';
$bufferSize = 1024 * 1024;
$bytesRead = $this->downloadDumpInPath($dumpPath);

$dump = fopen($this->dumpUrl, 'rb');
$outfile = fopen($dumpPath, 'wb');
$bytesRead = 0;
while (!feof($dump)) {
fwrite($outfile, fread($dump, $bufferSize), $bufferSize);
$bytesRead += $bufferSize;
}
fclose($dump);
fclose($outfile);

if ($bytesRead <= 40 * 1024 * 1024) { // sanity check
throw new Exception("Could not download sql dump!");
// sanity check
if ($bytesRead <= 40 * 1024 * 1024) {
$str = "Could not download sql dump! You can manually download %s into %s";
throw new Exception(sprintf($str, $this->dumpUrl, $dumpPath));
}
}

Expand Down Expand Up @@ -93,4 +85,24 @@ public function tearDown()
{
// empty
}

/**
* @param $dumpPath
* @return int
*/
protected function downloadDumpInPath($dumpPath)
{
$bufferSize = 1024 * 1024;

$dump = fopen($this->dumpUrl, 'rb');
$outfile = fopen($dumpPath, 'wb');
$bytesRead = 0;
while (!feof($dump)) {
fwrite($outfile, fread($dump, $bufferSize), $bufferSize);
$bytesRead += $bufferSize;
}
fclose($dump);
fclose($outfile);
return $bytesRead;
}
}
4 changes: 4 additions & 0 deletions tests/PHPUnit/Framework/Fixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,10 @@ public static function downloadAndUnzip($url, $outputDir, $filename)

$dump = fopen($url, 'rb');
$outfile = fopen($outfileName, 'wb');
if(!$outfile) {
throw new Exception("Failed to create file $outfileName - please check permissions");
}

while (!feof($dump)) {
fwrite($outfile, fread($dump, $bufferSize), $bufferSize);
}
Expand Down

0 comments on commit 1393a9c

Please sign in to comment.