| 1 |
<?php |
| 2 |
|
| 3 |
namespace Minify\Test; |
| 4 |
|
| 5 |
use Minify_CacheInterface; |
| 6 |
|
| 7 |
abstract class TestCase extends \PHPUnit\Framework\TestCase |
| 8 |
{ |
| 9 |
/** @var string */ |
| 10 |
protected static $document_root; |
| 11 |
/** @var string */ |
| 12 |
protected static $test_files; |
| 13 |
|
| 14 |
public static function setupBeforeClass() |
| 15 |
{ |
| 16 |
self::$document_root = __DIR__; |
| 17 |
self::$test_files = __DIR__ . '/_test_files'; |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Get number of bytes in a string regardless of mbstring.func_overload |
| 22 |
* |
| 23 |
* @param string $str |
| 24 |
* @return int |
| 25 |
*/ |
| 26 |
protected function countBytes($str) |
| 27 |
{ |
| 28 |
return (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) |
| 29 |
? mb_strlen($str, '8bit') |
| 30 |
: strlen($str); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Common assertion for cache tests. |
| 35 |
* |
| 36 |
* @param Minify_CacheInterface $cache |
| 37 |
* @param string $id |
| 38 |
* @param string $data |
| 39 |
*/ |
| 40 |
protected function assertTestCache(Minify_CacheInterface $cache, $id, $data) |
| 41 |
{ |
| 42 |
$this->assertTrue($cache->store($id, $data), "$id store"); |
| 43 |
$this->assertEquals($cache->getSize($id), $this->countBytes($data), "$id getSize"); |
| 44 |
$this->assertTrue($cache->isValid($id, $_SERVER['REQUEST_TIME'] - 10), "$id isValid"); |
| 45 |
|
| 46 |
ob_start(); |
| 47 |
$cache->display($id); |
| 48 |
$displayed = ob_get_contents(); |
| 49 |
ob_end_clean(); |
| 50 |
|
| 51 |
$this->assertSame($data, $displayed, "$id display"); |
| 52 |
$this->assertEquals($data, $cache->fetch($id), "$id fetch"); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Read data file, assert that it exists and is not empty. |
| 57 |
* As a side effect calls trim() to fight against different Editors that insert or strip final newline. |
| 58 |
* |
| 59 |
* @param string $filename |
| 60 |
* @return string |
| 61 |
*/ |
| 62 |
protected function getDataFile($filename) |
| 63 |
{ |
| 64 |
$path = self::$test_files . '/' . $filename; |
| 65 |
$this->assertFileExists($path); |
| 66 |
$contents = file_get_contents($path); |
| 67 |
$this->assertNotEmpty($contents); |
| 68 |
$contents = trim($contents); |
| 69 |
$this->assertNotEmpty($contents); |
| 70 |
|
| 71 |
return $contents; |
| 72 |
} |
| 73 |
} |
| 74 |
|