/** * @covers ::getObjectFromCache * @covers ::setObjectInCache * @covers ::fromObject */ public function testReadsObjectResultsFromAnInternalCache() { $target = new CallableMethodsListTest_Target1(); $expectedMethods = ['garbageMethod1' => 'staticMethod1']; // put the data into the cache InvokeMethod::onString(CallableMethodsList::class, 'setObjectInCache', [$target, $expectedMethods]); // ---------------------------------------------------------------- // perform the change $actualMethods2 = CallableMethodsList::fromObject($target); // ---------------------------------------------------------------- // test the results // this proves that the data we expect is in the internal cache $actualMethods1 = InvokeMethod::onString(CallableMethodsList::class, 'getObjectFromCache', [$target]); $this->assertEquals($expectedMethods, $actualMethods1); // this proves that CallableMethodsList uses the internal cache // when data is present $this->assertEquals($expectedMethods, $actualMethods2); }
/** * find the first method on $target that matches $data's data type * * @param mixed $data * the data we want to call $target with * @param string|object $target * the target that we want to call * @param string $methodPrefix * the prefix at the front of methods on $target * @return string|null * the method that suits $data the best */ private static function findFirstMatchingMethod($data, $target, $methodPrefix) { // no, so we need to build it $possibleTypes = AllMatchingTypesList::from($data); $possibleMethods = CallableMethodsList::from($target); foreach ($possibleTypes as $possibleType) { $targetMethodName = $methodPrefix . FilterNamespace::fromString($possibleType); if (isset($possibleMethods[$targetMethodName])) { // all done return $targetMethodName; } } // no match return null; }