Failed unit test

I want to test my Route class
This is the test that I failed
``

/**
 * @param  string $pattern The valid pattern for url route
 * @dataProvider providerTestValidPattern
 */

 public function testValidPattern($pattern)
{
	$routeMock = $this->getMockBuilder('Route')->setMethods('__construct')->getMock();
	$bool = $this->invokeMethod($routeMock, 'isPatternValid', array($pattern));
	$this->assertTrue($bool);
}

public function providerTestValidPattern()
{
	return array(
		array('/'),
		array('/test'),
		array('/trailing/slash/'),
		array('/[]/;,.*&^%324/|*&~@!'),
		array('/multiple/trailing/slash/will/be/trimmed/////////'),
		array('/test/double//')
		);
}

public function invokeMethod(&$object, $methodName, array $parameters = array())
{
	$reflection = new \ReflectionClass(get_class($object));
	$method = $reflection->getMethod($methodName);
	$method->setAccessible(true);

	return $method->invokeArgs($object, $parameters);
}

``

And, this is the isPatternValid() method which I test
``

protected function isPatternValid(&$pattern)
{
	if (!is_string($pattern))
	{
		throw new \InvalidArgumentException(sprintf('Invalid type for $pattern; string expected, given : %s', gettype($pattern)));
	}
	$pattern = rtrim($pattern, '/');
	if (preg_match('#^/(?:([^/]+/)*[^/]+)?$#', $pattern)) {
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

``

And, this is the constructor that I stub, so I don’t need to specify that pattern and the callback
``

public function __construct($pattern, $callback, $httpMethod = 'GET', $translator = null)
{
	if (!is_object($callback) || !method_exists($callback, '__invoke'))
	{
		throw new \InvalidArgumentException(sprintf('The parameter $callback must be a closure or invokable object, given a  %s', gettype($callback)));
	}
	if (isset($translator))
	{
		$this->setTranslator($translator);
	}
	else
	{
		$this->setTranslator(new Translator);
	}
	$this->setPattern($pattern);
	$this->setHttpMethod($httpMethod);
	$this->setCallback($callback);
}

``

I’ve tried $routeMock = $this->getMockBuilder('class_name') ->disableOriginalConstructor() ->getMock(); , but it just gets worse since it says that method isPatternValid doesn’t exist

Please help

EDIT::

Oops, I noticed that the argument for setMethods() must be array, so I fix this to
$routeMock = $this->getMockBuilder('Route')->setMethods(array('__construct'))->getMock();
But, it says that Method isPatternValid doesnot exist :frowning:

Are you using PHPUnit?
does your test class extend PHPUnit_Framework_TestCase

public function testValidPattern($pattern) extends \PHPUnit_Framework_TestCase

And have you included the original class in your bootstrap, or is it available from the Test class?

Of course…
Everything runs fine if I don’t use the mock (just instantiating the object)
But, I can’t figure out why using mock will cause error here… Why the method isPatternValid removed in the mock object?

You can’t stub the constructor as it returns null and the class will not init, you will have to include the html needed and test that,

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.