/** * @covers ::mapTypeToMethodName */ public function test_can_change_the_default_fallback_when_no_match_found() { // ---------------------------------------------------------------- // setup your test $item = new stdClass(); $dispatchTable = []; $expectedResult = "nothingComparesToYou"; $typeMapper = new MapDuckTypeToMethodName(); $unit = new ObjectsOnlyDispatchTable($dispatchTable, $typeMapper, $expectedResult); // ---------------------------------------------------------------- // perform the change $actualResult = $unit->mapTypeToMethodName($item); // ---------------------------------------------------------------- // test the results $this->assertEquals($expectedResult, $actualResult); }
if (is_object($item)) { $type = get_class($item); if (isset($resultCache[$type]) === false) { $resultCache[$type] = MapStrictTypeToMethodName::using($item, $dispatchTable); } $method = $resultCache[$type]; } else { $method = MapStrictTypeToMethodName::using($item, $dispatchTable); } } $endTime = microtime(true); $totalTime = round($endTime - $startTime, 3); echo "[Manual cache 2 ] Total time: " . $totalTime . PHP_EOL; // let's compare that with our object-driven caching dispatch table $startTime = microtime(true); $cachingTable = new ObjectsOnlyDispatchTable($dispatchTable, new MapStrictTypeToMethodName()); for ($i = 0; $i < $maxIterations; $i++) { $method = $cachingTable->mapTypeToMethodName($item); } $endTime = microtime(true); $totalTime = round($endTime - $startTime, 3); echo "[Objects only ] Total time: " . $totalTime . PHP_EOL; // let's compare that with our type-driven caching dispatch table $startTime = microtime(true); $cachingTable = new TypeOnlyDispatchTable($dispatchTable, new MapStrictTypeToMethodName()); for ($i = 0; $i < $maxIterations; $i++) { $method = $cachingTable->mapTypeToMethodName($item); } $endTime = microtime(true); $totalTime = round($endTime - $startTime, 3); echo "[Type only ] Total time: " . $totalTime . PHP_EOL;