Skip to content

Commit

Permalink
updated unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kokspflanze committed Mar 8, 2016
1 parent 7aaf590 commit 1a8ac13
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 123 deletions.
15 changes: 1 addition & 14 deletions tests/Bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
<?php

include __DIR__ . '/../vendor/autoload.php';

use ZfcBBCodeTest\Util\ServiceManagerFactory;

ini_set('error_reporting', E_ALL);

if (file_exists(__DIR__ . '/TestConfig.php')) {
$config = require __DIR__ . '/TestConfig.php';
} else {
throw new \Exception('text config missing');
}

ServiceManagerFactory::setConfig($config);
unset($config);

ini_set('error_reporting', E_ALL);
13 changes: 0 additions & 13 deletions tests/TestConfig.php

This file was deleted.

69 changes: 63 additions & 6 deletions tests/ZfcBBCodeTest/Service/SBBCodeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,21 @@

class SBBCodeParserTest extends TestBase
{
/** @var string */
protected $className = 'ZfcBBCode\Service\SBBCodeParser';

/** @var array */
protected $mockedConstructorArgList = [
[
'emoticons' => [
'active' => false,
'path' => [

],
],
]
];

/**
* @param $expected
* @param $string
Expand All @@ -29,9 +42,46 @@ public function testGetParsedText( $expected, $string )
*/
public function testGetParsedTextEmoticons( $expected, $string )
{
$config = $this->serviceManager->get('Config');
$config['zfc-bbcode']['emoticons']['active'] = true;
$this->serviceManager->setAllowOverride(true)->setService('Config', $config);
$serverName = 'http://foo.bar';
$this->mockedConstructorArgList = [
[
'emoticons' => [
'active' => true,
'path' => [
':)' => $serverName . '/minified/emoticons/smile.png',
':angel:' => $serverName . '/minified/emoticons/angel.png',
':angry:' => $serverName . '/minified/emoticons/angry.png',
'8-)' => $serverName . '/minified/emoticons/cool.png',
":'(" => $serverName . '/minified/emoticons/cwy.png',
':ermm:' => $serverName . '/minified/emoticons/ermm.png',
':D' => $serverName . '/minified/emoticons/grin.png',
'<3' => $serverName . '/minified/emoticons/heart.png',
':(' => $serverName . '/minified/emoticons/sad.png',
':O' => $serverName . '/minified/emoticons/shocked.png',
':P' => $serverName . '/minified/emoticons/tongue.png',
';)' => $serverName . '/minified/emoticons/wink.png',
':alien:' => $serverName . '/minified/emoticons/alien.png',
':blink:' => $serverName . '/minified/emoticons/blink.png',
':blush:' => $serverName . '/minified/emoticons/blush.png',
':cheerful:' => $serverName . '/minified/emoticons/cheerful.png',
':devil:' => $serverName . '/minified/emoticons/devil.png',
':dizzy:' => $serverName . '/minified/emoticons/dizzy.png',
':getlost:' => $serverName . '/minified/emoticons/getlost.png',
':kissing:' => $serverName . '/minified/emoticons/kissing.png',
':ninja:' => $serverName . '/minified/emoticons/ninja.png',
':pinch:' => $serverName . '/minified/emoticons/pinch.png',
':pouty:' => $serverName . '/minified/emoticons/pouty.png',
':sick:' => $serverName . '/minified/emoticons/sick.png',
':sideways:' => $serverName . '/minified/emoticons/sideways.png',
':silly:' => $serverName . '/minified/emoticons/silly.png',
':sleeping:' => $serverName . '/minified/emoticons/sleeping.png',
':unsure:' => $serverName . '/minified/emoticons/unsure.png',
':woot:' => $serverName . '/minified/emoticons/woot.png',
':wassat:' => $serverName . '/minified/emoticons/wassat.png'
],
],
]
];

$this->testGetParsedText($expected, $string);
}
Expand All @@ -57,9 +107,16 @@ public function testIsTextValid( $expected, $string )
*/
public function testIsTextValidEmoticons( $expected, $string )
{
$config = $this->serviceManager->get('Config');
$config['zfc-bbcode']['emoticons']['active'] = true;
$this->serviceManager->setAllowOverride(true)->setService('Config', $config);
$this->mockedConstructorArgList = [
[
'emoticons' => [
'active' => true,
'path' => [

],
],
]
];

$this->testIsTextValid($expected, $string);
}
Expand Down
45 changes: 0 additions & 45 deletions tests/ZfcBBCodeTest/Util/ServiceManagerFactory.php

This file was deleted.

48 changes: 34 additions & 14 deletions tests/ZfcBBCodeTest/Util/TestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,44 @@

class TestBase extends TestCase
{
/** @var \Zend\ServiceManager\ServiceManager */
protected $serviceManager;
/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $class;

/** @var string */
protected $className;

public function setUp()
/** @var array|null */
protected $mockedMethodList = null;

/** @var array */
protected $mockedConstructorArgList = [];

/**
* @param bool $newInstance
* @return \PHPUnit_Framework_MockObject_MockObject
*/
protected function getClass($newInstance = false)
{
parent::setUp();
$this->serviceManager = ServiceManagerFactory::getServiceManager();
if (!$this->class || $newInstance) {
$class = $this->getMockBuilder($this->className);
if ($this->mockedConstructorArgList) {
$class->setConstructorArgs($this->mockedConstructorArgList);
} else {
$class->disableOriginalConstructor();
}
$this->class = $class->setMethods($this->mockedMethodList)
->getMock();
}

return $this->class;
}

/**
* @param $methodName
* @return \ReflectionMethod
*/
protected function getMethod($methodName) {
protected function getMethod($methodName)
{
$reflection = new \ReflectionClass($this->getClass());
$method = $reflection->getMethod($methodName);
$method->setAccessible(true);
Expand All @@ -31,16 +53,14 @@ protected function getMethod($methodName) {
}

/**
* @param null $className
* @return object
* @param $name
* @return mixed
*/
protected function getClass( $className = null )
protected function getProperty($name)
{
$class = $className?$className:$this->className;
/** @var \Zend\ServiceManager\ServiceManagerAwareInterface $class */
$class = new $class;
$class->setServiceManager($this->serviceManager);
$reflection = new \ReflectionProperty($this->getClass(), $name);
$reflection->setAccessible(true);

return $class;
return $reflection->getValue($this->getClass());
}
}
40 changes: 25 additions & 15 deletions tests/ZfcBBCodeTest/Validator/BBCodeValidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,37 @@ class BBCodeValidTest extends TestBase
{
protected $className = 'ZfcBBCode\Validator\BBCodeValid';

protected function setUp()
{
parent::setUp();

$parser = $this->getMockBuilder('ZfcBBCode\Service\SBBCodeParser')
->setConstructorArgs([
[
'emoticons' => [
'active' => false,
'path' => [

],
],
],
])
->setMethods(null)
->getMock();

$this->mockedConstructorArgList = [
$parser
];
}

/**
* @param $expected
* @param $string
* @dataProvider dataProviderTestIsValid
*/
public function testIsValid( $expected, $string )
{
/** @var \ZfcBBCode\Validator\BBCodeValid $class */
$class = $this->getClass();
$method = $this->getMethod('isValid');
$result = $method->invokeArgs($class, array($string));
Expand All @@ -32,9 +56,7 @@ public function testIsValid( $expected, $string )

public function testGetBBCodeParser()
{
$class = $this->getClass();
$method = $this->getMethod('getBBCoderParser');
$result = $method->invokeArgs($class, array());
$result = $this->getProperty('bbCodeParser');

$this->assertInstanceOf('\ZfcBBCode\Service\ParserInterface', $result);
}
Expand Down Expand Up @@ -85,16 +107,4 @@ public function dataProviderTestIsValid()
);
}

/**
* @param null $className
* @return object
*/
protected function getClass( $className = null )
{
$class = $className?$className:$this->className;
/** @var \Zend\ServiceManager\ServiceLocatorInterface $class */
$class = new $class($this->serviceManager);

return $class;
}
}
34 changes: 23 additions & 11 deletions tests/ZfcBBCodeTest/View/Helper/BBCodeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,29 @@ class BBCodeParserTest extends TestBase
{
protected $className = 'ZfcBBCode\View\Helper\BBCodeParser';

protected function setUp()
{
parent::setUp();

$parser = $this->getMockBuilder('ZfcBBCode\Service\SBBCodeParser')
->setConstructorArgs([
[
'emoticons' => [
'active' => false,
'path' => [

],
],
],
])
->setMethods(null)
->getMock();

$this->mockedConstructorArgList = [
$parser
];
}

/**
* @param $expected
* @param $string
Expand Down Expand Up @@ -57,16 +80,5 @@ public function dataProviderTestInvoke()
);
}

/**
* @param null $className
* @return object
*/
protected function getClass( $className = null )
{
$class = $className?$className:$this->className;
/** @var \Zend\ServiceManager\ServiceLocatorInterface $class */
$class = new $class($this->serviceManager);

return $class;
}
}
5 changes: 0 additions & 5 deletions tests/testing.config.php

This file was deleted.

0 comments on commit 1a8ac13

Please sign in to comment.