public function setUp() { Phake::initAnnotations($this); $this->registry = new Phake_Mock_InfoRegistry(); $this->registry->addInfo($this->info1); $this->registry->addInfo($this->info2); $this->registry->addInfo($this->info3); }
/** * Generates a new class with the given class name * * @param string $newClassName - The name of the new class * @param string $mockedClassName - The name of the class being mocked * @param Phake_Mock_InfoRegistry $infoRegistry * @return NULL */ public function generate($newClassName, $mockedClassName, Phake_Mock_InfoRegistry $infoRegistry) { $extends = ''; $implements = ''; $interfaces = array(); $constructor = ''; $mockedClass = new ReflectionClass($mockedClassName); if (!$mockedClass->isInterface()) { $extends = "extends {$mockedClassName}"; } elseif ($mockedClassName != 'Phake_IMock') { $implements = ", {$mockedClassName}"; if ($mockedClass->implementsInterface('Traversable') && !$mockedClass->implementsInterface('Iterator') && !$mockedClass->implementsInterface('IteratorAggregate')) { if ($mockedClass->getName() == 'Traversable') { $implements = ', Iterator'; } else { $implements = ', Iterator' . $implements; } $interfaces = array('Iterator'); } } $classDef = "\nclass {$newClassName} {$extends}\n\timplements Phake_IMock {$implements}\n{\n public \$__PHAKE_info;\n\n public static \$__PHAKE_staticInfo;\n\n\tconst __PHAKE_name = '{$mockedClassName}';\n\n\tpublic \$__PHAKE_constructorArgs;\n\n\t{$constructor}\n\n\t/**\n\t * @return void\n\t */\n\tpublic function __destruct() {}\n\n \t{$this->generateSafeConstructorOverride($mockedClass)}\n\n\t{$this->generateMockedMethods($mockedClass, $interfaces)}\n}\n"; $this->loader->loadClassByString($newClassName, $classDef); $newClassName::$__PHAKE_staticInfo = $this->createMockInfo($mockedClassName, new Phake_CallRecorder_Recorder(), new Phake_Stubber_StubMapper(), new Phake_Stubber_Answers_NoAnswer()); $infoRegistry->addInfo($newClassName::$__PHAKE_staticInfo); }
/** * Generates a new class with the given class name * * @param string $newClassName - The name of the new class * @param string $mockedClassName - The name of the class being mocked * @param Phake_Mock_InfoRegistry $infoRegistry * @return NULL */ public function generate($newClassName, $mockedClassName, Phake_Mock_InfoRegistry $infoRegistry) { $extends = ''; $implements = ''; $interfaces = array(); $parent = null; $constructor = ''; $mockedClassNames = (array) $mockedClassName; $mockedClasses = array(); foreach ($mockedClassNames as $mockedClassName) { $mockedClass = new ReflectionClass($mockedClassName); $mockedClasses[] = $mockedClass; if (!$mockedClass->isInterface()) { if (!empty($parent)) { throw new RuntimeException("You cannot use two classes in the same mock: {$parent->getName()}, {$mockedClass->getName()}. Use interfaces instead."); } $parent = $mockedClass; } else { if ($mockedClass->implementsInterface('Traversable') && !$mockedClass->implementsInterface('Iterator') && !$mockedClass->implementsInterface('IteratorAggregate')) { $interfaces[] = new ReflectionClass('Iterator'); if ($mockedClass->getName() != 'Traversable') { $interfaces[] = $mockedClass; } } else { $interfaces[] = $mockedClass; } } } $interfaces = array_unique($interfaces); if (!empty($parent)) { $extends = "extends {$parent->getName()}"; } $interfaceNames = array_map(function (ReflectionClass $c) { return $c->getName(); }, $interfaces); if (($key = array_search('Phake_IMock', $interfaceNames)) !== false) { unset($interfaceNames[$key]); } if (!empty($interfaceNames)) { $implements = ', ' . implode(',', $interfaceNames); } if (empty($parent)) { $mockedClass = array_shift($interfaces); } else { $mockedClass = $parent; } $classDef = "\nclass {$newClassName} {$extends}\n\timplements Phake_IMock {$implements}\n{\n public \$__PHAKE_info;\n\n public static \$__PHAKE_staticInfo;\n\n\tconst __PHAKE_name = '{$mockedClassName}';\n\n\tpublic \$__PHAKE_constructorArgs;\n\n\t{$constructor}\n\n\t/**\n\t * @return void\n\t */\n\tpublic function __destruct() {}\n\n \t{$this->generateSafeConstructorOverride($mockedClasses)}\n\n\t{$this->generateMockedMethods($mockedClass, $interfaces)}\n}\n"; $this->loader->loadClassByString($newClassName, $classDef); $newClassName::$__PHAKE_staticInfo = $this->createMockInfo($mockedClassName, new Phake_CallRecorder_Recorder(), new Phake_Stubber_StubMapper(), new Phake_Stubber_Answers_NoAnswer()); $infoRegistry->addInfo($newClassName::$__PHAKE_staticInfo); }