Skip to content

Commit

Permalink
testCanSetCustomClassName
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotchance committed Sep 8, 2014
1 parent 2868bce commit a5b6802
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
20 changes: 19 additions & 1 deletion src/Concise/Mock/MockBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ class MockBuilder
*/
protected $isExpecting = false;

/**
* You may set a customer class name for this mock.
* @var string
*/
protected $customClassName = '';

/**
* @param string $className
* @param boolean $niceMock
Expand Down Expand Up @@ -137,6 +143,9 @@ public function stub($arg)
public function done()
{
$compiler = new ClassCompiler($this->className, $this->niceMock, $this->constructorArgs, $this->disableConstructor);
if ($this->customClassName) {
$compiler->setCustomClassName($this->customClassName);
}
$compiler->setRules($this->rules);
foreach ($this->expose as $method) {
$compiler->addExpose($method);
Expand Down Expand Up @@ -189,10 +198,11 @@ protected function methodIsNeverExpected()
*/
public function andReturn()
{
if($this->methodIsNeverExpected()) {
if ($this->methodIsNeverExpected()) {
throw new Exception("You cannot assign an action to '{$this->currentRule}()' when it is never expected.");
}
$values = func_get_args();

return $this->setAction(new Action\ReturnValueAction($values));
}

Expand Down Expand Up @@ -312,6 +322,7 @@ public function getRules()
protected function isInterface()
{
$refClass = new \ReflectionClass($this->className);

return $refClass->isInterface();
}

Expand Down Expand Up @@ -352,4 +363,11 @@ public function andDo(\Closure $action)
{
return $this->setAction(new Action\DoAction($action));
}

public function setCustomClassName($customClassName)
{
$this->customClassName = $customClassName;

return $this;
}
}
9 changes: 9 additions & 0 deletions tests/Concise/Mock/AbstractMockBuilderTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -572,4 +572,13 @@ public function testWillThrowExceptionIfTheCustomNameIsNotValid()
->setCustomClassName('123')
->done();
}

public function testCanSetCustomClassName()
{
$rand = "Concise\\Mock\\Temp" . md5(rand());
$mock = $this->mockBuilder()
->setCustomClassName($rand)
->done();
$this->assert(get_class($mock), equals, $rand);
}
}
4 changes: 2 additions & 2 deletions tests/Concise/Mock/ClassCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ public function testWillIgnorePreceedingBackslashForCustomClassName()

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage You cannot use 'MyCustomClass' because a class with that name already exists.
* @expectedExceptionMessage You cannot use 'DateTime' because a class with that name already exists.
*/
public function testCustomClassNameCannotBeUsedIfTheClassAlreadyExists()
{
$compiler = new ClassCompiler('Concise\Mock\ClassCompilerMock1');
$compiler->setCustomClassName('\MyCustomClass');
$compiler->setCustomClassName('\DateTime');
}
}
10 changes: 7 additions & 3 deletions tests/Concise/Mock/MockBuilderForFinalClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Concise\Mock;

use \Concise\TestCase;

final class MockFinalClass
{
public $constructorRun = false;
Expand Down Expand Up @@ -32,7 +30,7 @@ private function myPrivateMethod()
{
}

static public function myStaticMethod()
public static function myStaticMethod()
{
return 'foo';
}
Expand Down Expand Up @@ -372,4 +370,10 @@ public function testAbstractMethodsCanHaveRulesAttached()
$this->expectFailure('Class Concise\Mock\MockFinalClass is final so it cannot be mocked.');
parent::testAbstractMethodsCanHaveRulesAttached();
}

public function testCanSetCustomClassName()
{
$this->expectFailure('Class Concise\Mock\MockFinalClass is final so it cannot be mocked.');
parent::testCanSetCustomClassName();
}
}

0 comments on commit a5b6802

Please sign in to comment.