Convert existing mocks to Mockery-mocks (Part 2)

refs #4639
This commit is contained in:
Johannes Meyer 2014-04-14 10:57:48 +02:00
parent a844d33735
commit 1df4d9022b
2 changed files with 27 additions and 98 deletions

View File

@ -4,6 +4,7 @@
namespace Tests\Icinga\Web; namespace Tests\Icinga\Web;
use \Mockery;
use Icinga\Web\Hook; use Icinga\Web\Hook;
use Icinga\Test\BaseTestCase; use Icinga\Test\BaseTestCase;
@ -27,26 +28,6 @@ class ErrorProneHookImplementation
} }
} }
class ObjectHookImplementation
{
private $test;
public function setTest($test)
{
$this->test = $test;
}
public function getTest()
{
return $this->test;
}
public function __toString()
{
return $this->getTest();
}
}
class HookTest extends BaseTestCase class HookTest extends BaseTestCase
{ {
public function setUp() public function setUp()
@ -127,22 +108,20 @@ class HookTest extends BaseTestCase
public function testRegisterObject() public function testRegisterObject()
{ {
$o1 = new ObjectHookImplementation(); $o1 = Mockery::mock('Some\\Name\\Space\\ObjectHook');
$o1->setTest('$123123'); $o1->test = '$123123';
$o2 = Mockery::mock('Some\\Name\\Space\\ObjectHook');
$o2->test = '#456456';
Hook::registerObject('Test', 'o1', $o1); Hook::registerObject('Test', 'o1', $o1);
$o2 = new ObjectHookImplementation();
$o2->setTest('#456456');
Hook::registerObject('Test', 'o2', $o2); Hook::registerObject('Test', 'o2', $o2);
$this->assertInstanceOf('Tests\\Icinga\\Web\\ObjectHookImplementation', Hook::createInstance('Test', 'o1')); $this->assertInstanceOf('Some\\Name\\Space\\ObjectHook', Hook::createInstance('Test', 'o1'));
$this->assertInstanceOf('Tests\\Icinga\\Web\\ObjectHookImplementation', Hook::createInstance('Test', 'o2')); $this->assertInstanceOf('Some\\Name\\Space\\ObjectHook', Hook::createInstance('Test', 'o2'));
$string = ""; $string = "";
foreach (Hook::all('Test') as $hook) { foreach (Hook::all('Test') as $hook) {
$string .= (string)$hook; $string .= $hook->test;
} }
$this->assertEquals('$123123#456456', $string); $this->assertEquals('$123123#456456', $string);
} }
@ -156,7 +135,7 @@ class HookTest extends BaseTestCase
Hook::registerObject('Test', 'e1', 'STRING'); Hook::registerObject('Test', 'e1', 'STRING');
} }
public function testGetNullHooks() public function testGetZeroHooks()
{ {
$nh = Hook::all('DOES_NOT_EXIST'); $nh = Hook::all('DOES_NOT_EXIST');
$this->assertInternalType('array', $nh); $this->assertInternalType('array', $nh);

View File

@ -8,55 +8,14 @@ namespace Tests\Icinga\Web\Paginator\ScrollingStyle;
require_once realpath(ICINGA_LIBDIR . '/Icinga/Web/Paginator/ScrollingStyle/SlidingWithBorder.php'); require_once realpath(ICINGA_LIBDIR . '/Icinga/Web/Paginator/ScrollingStyle/SlidingWithBorder.php');
// @codingStandardsIgnoreEnd // @codingStandardsIgnoreEnd
use \Mockery;
use \Zend_Config; use \Zend_Config;
use \Zend_Paginator_Adapter_Interface; use \Zend_Paginator;
use Icinga\Test\BaseTestCase; use Icinga\Test\BaseTestCase;
use Icinga\Protocol\Statusdat\Reader; use Icinga\Protocol\Statusdat\Reader;
use Icinga\Web\Paginator\Adapter\QueryAdapter; use Icinga\Web\Paginator\Adapter\QueryAdapter;
use Icinga\Module\Monitoring\Backend; use Icinga\Module\Monitoring\Backend;
class TestPaginatorAdapter implements Zend_Paginator_Adapter_Interface
{
private $items = array();
public function __construct()
{
for ($i=0; $i<1000; $i++) {
$this->items[] = array(
'a' => mt_rand(0, 100),
'b' => mt_rand(0, 100)
);
}
}
/**
* Returns an collection of items for a page.
*
* @param integer $offset Page offset
* @param integer $itemCountPerPage Number of items per page
* @return array
*/
public function getItems($offset, $itemCountPerPage)
{
$out = array_slice($this->items, $offset, $itemCountPerPage, true);
}
/**
* (PHP 5 &gt;= 5.1.0)<br/>
* Count elements of an object
* @link http://php.net/manual/en/countable.count.php
* @return int The custom count as an integer.
* </p>
* <p>
* The return value is cast to an integer.
*/
public function count()
{
return count($this->items);
}
}
class SlidingwithborderTest extends BaseTestCase class SlidingwithborderTest extends BaseTestCase
{ {
private $cacheDir; private $cacheDir;
@ -85,7 +44,7 @@ class SlidingwithborderTest extends BaseTestCase
$this->resourceConfig = new Zend_Config( $this->resourceConfig = new Zend_Config(
array( array(
'status_file' => $statusdatFile, 'status_file' => $statusdatFile,
'object_file' => $cacheFile, 'object_file' => $cacheFile,
'type' => 'statusdat' 'type' => 'statusdat'
) )
); );
@ -94,53 +53,44 @@ class SlidingwithborderTest extends BaseTestCase
public function testGetPages1() public function testGetPages1()
{ {
$backend = new Backend($this->backendConfig, $this->resourceConfig); $backend = new Backend($this->backendConfig, $this->resourceConfig);
$query = $backend->select()->from('status'); $adapter = new QueryAdapter($backend->select()->from('status'));
$adapter = new QueryAdapter($query);
$this->assertEquals(30, $adapter->count()); $this->assertEquals(30, $adapter->count());
$scrolingStyle = new \Icinga_Web_Paginator_ScrollingStyle_SlidingWithBorder(); $scrollingStyle = new \Icinga_Web_Paginator_ScrollingStyle_SlidingWithBorder();
$paginator = new Zend_Paginator($adapter);
$paginator = new \Zend_Paginator($adapter);
$pages = $scrolingStyle->getPages($paginator);
$pages = $scrollingStyle->getPages($paginator);
$this->assertInternalType('array', $pages); $this->assertInternalType('array', $pages);
$this->assertCount(3, $pages); $this->assertCount(3, $pages);
} }
public function testGetPages2() public function testGetPages2()
{ {
$scrolingStyle = new \Icinga_Web_Paginator_ScrollingStyle_SlidingWithBorder(); $scrollingStyle = new \Icinga_Web_Paginator_ScrollingStyle_SlidingWithBorder();
$paginator = new Zend_Paginator($this->getPaginatorAdapter());
$adapter = new TestPaginatorAdapter();
$paginator = new \Zend_Paginator($adapter);
$pages = $scrolingStyle->getPages($paginator);
$pages = $scrollingStyle->getPages($paginator);
$this->assertInternalType('array', $pages); $this->assertInternalType('array', $pages);
$this->assertCount(13, $pages); $this->assertCount(13, $pages);
$this->assertEquals('...', $pages[11]); $this->assertEquals('...', $pages[11]);
} }
public function testGetPages3() public function testGetPages3()
{ {
$scrolingStyle = new \Icinga_Web_Paginator_ScrollingStyle_SlidingWithBorder(); $scrollingStyle = new \Icinga_Web_Paginator_ScrollingStyle_SlidingWithBorder();
$paginator = new Zend_Paginator($this->getPaginatorAdapter());
$adapter = new TestPaginatorAdapter();
$paginator = new \Zend_Paginator($adapter);
$paginator->setCurrentPageNumber(9); $paginator->setCurrentPageNumber(9);
$pages = $scrolingStyle->getPages($paginator); $pages = $scrollingStyle->getPages($paginator);
$this->assertInternalType('array', $pages); $this->assertInternalType('array', $pages);
$this->assertCount(16, $pages); $this->assertCount(16, $pages);
$this->assertEquals('...', $pages[3]); $this->assertEquals('...', $pages[3]);
$this->assertEquals('...', $pages[14]); $this->assertEquals('...', $pages[14]);
} }
protected function getPaginatorAdapter()
{
return Mockery::mock('\Zend_Paginator_Adapter_Interface')->shouldReceive('count')->andReturn(1000)->getMock();
}
} }