/** * @covers ::fromObject * @covers ::setObjectInCache * @covers ::getObjectCacheName */ public function testWritesToInternalCacheForObjects() { // ---------------------------------------------------------------- // setup your test $obj = new AllMatchingTypesList(); // our setup() method guarantees that we start with an empty cache $this->assertTrue(empty(InvokeMethod::onObject($obj, 'getCache'))); // ---------------------------------------------------------------- // perform the change $result = $obj($obj); // ---------------------------------------------------------------- // test the results $cache = InvokeMethod::onObject($obj, 'getCache'); $this->assertFalse(empty($cache)); $this->assertEquals([get_class($obj) . '::object' => $result], $cache); }
/** * @covers ::resetData */ public function testNothingHappensWhenRemovingDataThatDoesNotExist() { // ---------------------------------------------------------------- // setup your test $obj = new BaseContainer(); $dataName = 'Value1'; // ---------------------------------------------------------------- // perform the change InvokeMethod::onObject($obj, 'resetData', [$dataName]); // ---------------------------------------------------------------- // test the results $actualData = InvokeMethod::onObject($obj, 'hasData', [$dataName]); $this->assertFalse($actualData); }
/** * @covers ::onObject */ public function testCanStaticallyCallPublicMethods() { // ---------------------------------------------------------------- // setup your test $target = new InvokeMethodTest_TargetClass(); $expectedResult = 40; // ---------------------------------------------------------------- // perform the change $actualResult = InvokeMethod::onObject($target, 'publicMethod', [10]); // ---------------------------------------------------------------- // test the results $this->assertEquals($expectedResult, $actualResult); }
/** * @covers ::processTokens * @covers ::processTokensUsingProcessor */ public function testSupportsMultipleTokenProcessors() { // ---------------------------------------------------------------- // setup your test $state = new StreamState(); $tokeniser = new StreamHeadTest_Tokeniser(); $writer = new StreamHeadTest_TokenWriter(); $processor1 = new StreamHeadTest_TokenDuplicator(); $processor2 = new StreamHeadTest_TokenCounter(); $stream = new BasicStreamHeadTest_HeadWithStringSupport($state, $tokeniser, [$processor1, $processor2], $writer); $expectedResult = "I have a string typesafe writer!"; $token = new StreamHeadTest_StringToken($expectedResult); $expectedTokens = [$token, $token, $token, $token]; // ---------------------------------------------------------------- // perform the change $actualTokens = InvokeMethod::onObject($stream, 'processTokens', [[$token, $token]]); // ---------------------------------------------------------------- // test the results $this->assertEquals($expectedTokens, $actualTokens); $this->assertEquals(4, $state->tokensSeen); }
/** * @covers ::getFromCache * @dataProvider provideDataForTheCache */ public function testReturnsNullIfNotInCache($key, $expectedValue) { // ---------------------------------------------------------------- // setup your test $cache = new CacheTypeA(); // make sure the underlying shared cache is EMPTY InvokeMethod::onString(CacheTypeA::class, 'resetCache'); // ---------------------------------------------------------------- // perform the change // ---------------------------------------------------------------- // test the results $this->assertNull(InvokeMethod::onObject($cache, 'getFromCache', [$key])); }
/** * @covers ::from * @covers ::getMethodFromCache * @covers ::setMethodInCache */ public function testReusesResultsFromStaticDataCache() { // ---------------------------------------------------------------- // setup your test $obj = new FirstMethodMatchingType(); $data = new \stdClass(); $targetObj = new FirstMethodMatchingTypeTest_Target1(); $expectedResult = "noSuchMethod"; InvokeMethod::onObject($obj, 'setMethodInCache', [$data, $targetObj, $expectedResult]); // prove that the data is in the cache $cachedResult = InvokeMethod::onObject($obj, 'getMethodFromCache', [$data, $targetObj]); // ---------------------------------------------------------------- // perform the change // $actualResult can only have the value we expect if $obj is // reading the data we inserted into the cache $actualResult = $obj($data, $targetObj, 'from'); // ---------------------------------------------------------------- // test the results $this->assertEquals($expectedResult, $actualResult); }