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