Skip to content

Commit

Permalink
Enable Simpletest mocks to manage namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
pharazon committed Jun 11, 2017
1 parent 31c720a commit d8b1f13
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions mock_objects.php
Expand Up @@ -1290,6 +1290,7 @@ class MockGenerator
private $mock_class;
private $mock_base;
private $reflection;
protected $namespace;

/**
* Builds initial reflection object.
Expand All @@ -1300,13 +1301,16 @@ class MockGenerator
*/
public function __construct($class, $mock_class)
{
$this->class = $class;
$reflectionClass = new \ReflectionClass($class);
$this->namespace = $reflectionClass->getNamespaceName();
$this->class = $reflectionClass->getShortName();

$this->mock_class = $mock_class;
if (! $this->mock_class) {
$this->mock_class = 'Mock' . $this->class;
$this->mock_class .= 'Mock' . $this->class;
}
$this->mock_base = SimpleTest::getMockBaseClass();
$this->reflection = new SimpleReflection($this->class);
$this->reflection = new SimpleReflection($class);
}

/**
Expand Down Expand Up @@ -1348,7 +1352,12 @@ public function generateSubclass($methods)
if (! $this->reflection->classOrInterfaceExists()) {
return false;
}
$mock_reflection = new SimpleReflection($this->mock_class);
$mock_class = "";
if (!empty($this->namespace)) {
$mock_class .= $this->namespace . '\\';
}
$mock_class .= $this->mock_class;
$mock_reflection = new SimpleReflection($mock_class);
if ($mock_reflection->classExistsWithoutAutoload()) {
return false;
}
Expand Down Expand Up @@ -1430,12 +1439,16 @@ protected function createCodeForClass($methods)
*/
protected function createCodeForSubclass($methods)
{
$code = 'class ' . $this->mock_class . ' extends ' . $this->class . " {\n";
$code = "";
if (!empty($this->namespace)) {
$code .= 'namespace ' . $this->namespace . ";\n";
}
$code .= 'class ' . $this->mock_class . ' extends ' . $this->class . " {\n";
$code .= " public \$mock;\n";
$code .= $this->addMethodList(array_merge($methods, $this->reflection->getMethods()));
$code .= "\n";
$code .= " function __construct() {\n";
$code .= ' $this->mock = new ' . $this->mock_base . "();\n";
$code .= ' $this->mock = new \\' . $this->mock_base . "();\n";
$code .= " \$this->mock->disableExpectationNameChecks();\n";
$code .= " }\n";
$code .= $this->createCodeForConstructor();
Expand Down

0 comments on commit d8b1f13

Please sign in to comment.