php - How to test this function? -
public static function flush_cache() { static::$_cached_objects[get_class()] = array(); }
i don't know how test phpunit? project fuelphp framework . can give advice instead of trampling on question? lot
there no "correct" way test this. reason is: have global state!
testing static values difficult because share state between unit-tests (during 1 test-run). 1 unit-test can interfere , execution-order of unit tests becomes important.
instead of using static value (which nothing else global state), should pass cache function (like dependency injection). case, have this:
public static function flush_cache($cache) { $cache = array(); } // test function public function testflushcache() { $mycache = $array(); // uses cache, filling values. flush_cache($mycache); $this->assertequals(count($mycache), 0); }
of course, have still take care instantiating cache in application.
but suggest avoid global state, static cache (having static function okay though). having global state makes testing pita, , can source of weird bugs because each function tapping global variable becomes non-deterministic.
Comments
Post a Comment