Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added HooksTest

Signed-off-by: Darren Hill <dchill42@gmail.com>
  • Loading branch information...
commit 7e339759a4baf58fe659b66cb570f6c526db40d7 1 parent 8df6d9b
@dchill42 dchill42 authored
Showing with 204 additions and 3 deletions.
  1. +5 −3 system/core/Hooks.php
  2. +199 −0 tests/Xylophone/core/HooksTest.php
View
8 system/core/Hooks.php
@@ -10,7 +10,7 @@
* Licensed under the Open Software License version 3.0
*
* This source file is subject to the Open Software License (OSL 3.0) that is
- * bundled with this package in the files license.txt / license.rst. It is
+ * bundled with this package in the files license.txt / license.rst. It is
* also available through the world wide web at this URL:
* http://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to obtain it
@@ -36,7 +36,7 @@
*
* @package Xylophone
* @subpackage core
- * @link http://xylophone.io/user_guide/libraries/encryption.html
+ * @link http://xylophone.io/user_guide/general/hooks.html
*/
class Hooks
{
@@ -74,7 +74,9 @@ public function __construct()
/**
* Call Hook
*
- * @used-by Xylophone::play()
+ * @used-by Xylophone::playBridge()
+ * @used-by Xylophone::playCoda()
+ * @used-by Xylophone::playVerse()
*
* @param string $which Hook name
* @return bool TRUE on success, otherwise FALSE
View
199 tests/Xylophone/core/HooksTest.php
@@ -0,0 +1,199 @@
+<?php
+/**
+ * Xylophone
+ *
+ * An open source HMVC application development framework for PHP 5.3 or newer
+ * Derived from CodeIgniter, Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/)
+ *
+ * NOTICE OF LICENSE
+ *
+ * Licensed under the Open Software License version 3.0
+ *
+ * This source file is subject to the Open Software License (OSL 3.0) that is
+ * bundled with this package in the files license.txt / license.rst. It is
+ * also available through the world wide web at this URL:
+ * http://opensource.org/licenses/OSL-3.0
+ * If you did not receive a copy of the license and are unable to obtain it
+ * through the world wide web, please send an email to licensing@xylophone.io
+ * so we can send you a copy immediately.
+ *
+ * @package Xylophone
+ * @author Xylophone Dev Team, EllisLab Dev Team
+ * @copyright Copyright (c) 2014, Xylophone Team (http://xylophone.io/)
+ * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
+ * @link http://xylophone.io
+ * @since Version 1.0
+ * @filesource
+ */
+
+/**
+ * Hooks Unit Test
+ *
+ * @package Xylophone
+ */
+class HooksTest extends XyTestCase
+{
+ /**
+ * Test __construct()
+ */
+ public function testConstruct()
+ {
+ global $XY;
+
+ // Set up args
+ $hks = array('first_hook' => 'some/function', 'second_hook' => 'other/function');
+
+ // Mock Xylophone, Hooks, Config, and Logger
+ $XY = new stdClass();
+ $XY->config = $this->getMock('Xylophone\core\Config', array('offsetGet', 'get'), array(), '', false);
+ $XY->logger = $this->getMock('Xylophone\core\Logger', array('debug'), array(), '', false);
+ $hooks = $this->getMock('Xylophone\core\Hooks', null, array(), '', false);
+
+ // Set up calls
+ $XY->config->expects($this->once())->method('offsetGet')->with($this->equalTo('enable_hooks'))->
+ will($this->returnValue(true));
+ $XY->config->expects($this->once())->method('get')->with($this->equalTo('hooks.php'), $this->equalTo('hook'))->
+ will($this->returnValue($hks));
+ $XY->logger->expects($this->once())->method('debug');
+
+ // Call __construct() and verify results
+ $hooks->__construct();
+ $this->assertEquals($hks, $hooks->hooks);
+ $this->assertTrue($hooks->enabled);
+ }
+
+ /**
+ * Test __construct() disabled
+ */
+ public function testConstructDisabled()
+ {
+ global $XY;
+
+ // Mock Xylophone, Hooks, Config, and Logger
+ $XY = new stdClass();
+ $XY->config = $this->getMock('Xylophone\core\Config', array('offsetGet'), array(), '', false);
+ $XY->logger = $this->getMock('Xylophone\core\Logger', array('debug'), array(), '', false);
+ $hooks = $this->getMock('Xylophone\core\Hooks', null, array(), '', false);
+
+ // Set up calls
+ $XY->config->expects($this->once())->method('offsetGet')->with($this->equalTo('enable_hooks'))->
+ will($this->returnValue(false));
+ $XY->logger->expects($this->once())->method('debug');
+
+ // Call __construct() and verify results
+ $hooks->__construct();
+ $this->assertEmpty($hooks->hooks);
+ $this->assertFalse($hooks->enabled);
+ }
+
+ /**
+ * Test __construct() with no hooks
+ */
+ public function testConstructNone()
+ {
+ global $XY;
+
+ // Mock Xylophone, Hooks, Config, and Logger
+ $XY = new stdClass();
+ $XY->config = $this->getMock('Xylophone\core\Config', array('offsetGet', 'get'), array(), '', false);
+ $XY->logger = $this->getMock('Xylophone\core\Logger', array('debug'), array(), '', false);
+ $hooks = $this->getMock('Xylophone\core\Hooks', null, array(), '', false);
+
+ // Set up calls
+ $XY->config->expects($this->once())->method('offsetGet')->with($this->equalTo('enable_hooks'))->
+ will($this->returnValue(true));
+ $XY->config->expects($this->once())->method('get')->with($this->equalTo('hooks.php'), $this->equalTo('hook'))->
+ will($this->returnValue(false));
+ $XY->logger->expects($this->once())->method('debug');
+
+ // Call __construct() and verify results
+ $hooks->__construct();
+ $this->assertEmpty($hooks->hooks);
+ $this->assertFalse($hooks->enabled);
+ }
+
+ /**
+ * Test callHook() while disabled
+ */
+ public function testCallHookDisabled()
+ {
+ // Mock Hooks
+ $hooks = $this->getMock('Xylophone\core\Hooks', null, array(), '', false);
+ $hooks->enabled = false;
+
+ // Call callHook() and verify result
+ $this->assertFalse($hooks->callHook('some_hook'));
+ }
+
+ /**
+ * Test callHook() with a hook not set
+ */
+ public function testCallHookNotSet()
+ {
+ // Mock Hooks
+ $hooks = $this->getMock('Xylophone\core\Hooks', null, array(), '', false);
+ $hooks->enabled = true;
+
+ // Call callHook() and verify result
+ $this->assertFalse($hooks->callHook('not_hook'));
+ }
+
+ /**
+ * Test callHook() while in progress
+ */
+ public function testCallHookInProgress()
+ {
+ global $XY;
+
+ // Set up args
+ $name = 'double_hook';
+ $hook = 'hook/func';
+ $retval = true;
+
+ // Mock Xylophone, Loader, and Hooks
+ $XY = new stdClass();
+ $XY->load = $this->getMock('Xylophone\core\Loader', array('controller'), array(), '', false);
+ $hooks = $this->getMock('Xylophone\core\Hooks', null, array(), '', false);
+
+ // Set up call and hook
+ $XY->load->expects($this->once())->method('controller')->with($this->equalTo($hook))->will(
+ $this->returnCallback(function ($arg) use ($hooks, $name, &$retval) {
+ // Call recursively and capture return while in progress
+ $retval = $hooks->callHook($name);
+ }));
+ $hooks->enabled = true;
+ $hooks->hooks[$name] = $hook;
+
+ // Call callHook() and verify result
+ $this->assertTrue($hooks->callHook($name));
+ $this->assertFalse($retval);
+ }
+
+ /**
+ * Test callHook() with a multi-hook
+ */
+ public function testCallHookMulti()
+ {
+ global $XY;
+
+ // Set up args
+ $name = 'multi_hook';
+ $hook1 = 'left';
+ $hook2 = 'right';
+
+ // Mock Xylophone, Loader, and Hooks
+ $XY = new stdClass();
+ $XY->load = $this->getMock('Xylophone\core\Loader', array('controller'), array(), '', false);
+ $hooks = $this->getMock('Xylophone\core\Hooks', null, array(), '', false);
+
+ // Set up calls and hooks
+ $XY->load->expects($this->at(0))->method('controller')->with($this->equalTo($hook1));
+ $XY->load->expects($this->at(1))->method('controller')->with($this->equalTo($hook2));
+ $hooks->enabled = true;
+ $hooks->hooks[$name] = array($hook1, $hook2);
+
+ // Call callHook() and verify result
+ $this->assertTrue($hooks->callHook($name));
+ }
+}
+
Please sign in to comment.
Something went wrong with that request. Please try again.