lithium\test\Mocker::create()

public static method

The main entrance to create a new Mock class.

Parameters

  • string $mockee

    The fully namespaced \Mock class

Returns

void

Source

	public static function create($mockee) {
		if (!static::_validateMockee($mockee)) {
			return;
		}

		$mocker = static::_mocker($mockee);
		$isStatic = is_subclass_of($mocker, 'lithium\core\StaticObjectDeprecated');

		$tokens = [
			'namespace' => static::_namespace($mockee),
			'mocker' => $mocker,
			'mockee' => 'MockDelegate',
			'static' => $isStatic ? 'static' : '',
		];
		$mockDelegate = static::_dynamicCode('mockDelegate', 'startClass', $tokens);
		$mock = static::_dynamicCode('mock', 'startClass', $tokens);

		$reflectedClass = new ReflectionClass($mocker);
		$reflecedMethods = $reflectedClass->getMethods();
		$getByReference = false;
		$staticApplyFilter = true;
		$constructor = false;
		foreach ($reflecedMethods as $methodId => $method) {
			if (!in_array($method->name, static::$_blackList)) {
				$key = $method->isStatic() ? 'staticMethod' : 'method';
				if ($method->name === '__construct') {
					$key = 'constructor';
					$constructor = true;
				}
				$reflected = new ReflectionMethod($mocker, $method->name);
				$docs = (string) $reflected;
				if (preg_match('/&' . $method->name . '/', $docs) === 1) {
					continue;
				}
				$tokens = [
					'namespace' => static::_namespace($mockee),
					'method' => $method->name,
					'modifiers' => static::_methodModifiers($method),
					'args' => static::_methodParams($method),
					'stringArgs' => static::_stringMethodParams($method),
					'mocker' => $mocker,
				];
				$mockDelegate .= static::_dynamicCode('mockDelegate', $key, $tokens);
				$mock .= static::_dynamicCode('mock', $key, $tokens);
			} elseif ($method->name === '__get') {
				$reflected = new ReflectionMethod($mocker, '__get');
				$docs = (string) $reflected;
				$getByReference = preg_match('/&__get/', $docs) === 1;
			} elseif ($method->name === 'applyFilter') {
				$staticApplyFilter = $method->isStatic();
			}
		}

		if (!$constructor) {
			$tokens = [
				'namespace' => static::_namespace($mockee),
				'modifiers' => 'public',
				'args' => null,
				'stringArgs' => 'array()',
				'mocker' => $mocker,
			];
			$mock .= static::_dynamicCode('mock', 'constructor', $tokens);
			$mockDelegate .= static::_dynamicCode('mockDelegate', 'constructor', $tokens);
		}

		$mockDelegate .= static::_dynamicCode('mockDelegate', 'endClass');
		$mock .= static::_dynamicCode('mock', 'get', [
			'reference' => $getByReference ? '&' : '',
		]);
		$mock .= static::_dynamicCode('mock', 'set');
		$mock .= static::_dynamicCode('mock', 'isset');
		$mock .= static::_dynamicCode('mock', 'unset');
		$mock .= static::_dynamicCode('mock', 'applyFilter', [
			'static' => $staticApplyFilter ? 'static' : '',
		]);
		$mock .= static::_dynamicCode('mock', 'destructor');
		$mock .= static::_dynamicCode('mock', 'endClass');

		eval($mockDelegate . $mock);
	}