summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorThomas Müller <thomas.mueller@tmit.eu>2013-05-21 15:21:25 -0700
committerThomas Müller <thomas.mueller@tmit.eu>2013-05-21 15:21:25 -0700
commite2444ec9c5a7c36f7cfaa200387fb8d16e5dedb1 (patch)
treef7793b0e5a746a2d4bdf7f05ef79205cc1a4e5ea /tests
parent4bb6ac3aa7746513031b138d38204b660662f959 (diff)
parent3c779b0632809718b3f39403fdb68b7e160b5a0b (diff)
downloadnextcloud-server-e2444ec9c5a7c36f7cfaa200387fb8d16e5dedb1.tar.gz
nextcloud-server-e2444ec9c5a7c36f7cfaa200387fb8d16e5dedb1.zip
Merge pull request #3266 from owncloud/hooks
New hook system
Diffstat (limited to 'tests')
-rw-r--r--tests/lib/hooks/basicemitter.php261
-rw-r--r--tests/lib/hooks/legacyemitter.php55
2 files changed, 316 insertions, 0 deletions
diff --git a/tests/lib/hooks/basicemitter.php b/tests/lib/hooks/basicemitter.php
new file mode 100644
index 00000000000..f48dc53c563
--- /dev/null
+++ b/tests/lib/hooks/basicemitter.php
@@ -0,0 +1,261 @@
+<?php
+/**
+ * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace Test\Hooks;
+
+/**
+ * Class DummyEmitter
+ *
+ * class to make BasicEmitter::emit publicly available
+ *
+ * @package Test\Hooks
+ */
+class DummyEmitter extends \OC\Hooks\BasicEmitter {
+ public function emitEvent($scope, $method, $arguments = array()) {
+ $this->emit($scope, $method, $arguments);
+ }
+}
+
+/**
+ * Class EmittedException
+ *
+ * a dummy exception so we can check if an event is emitted
+ *
+ * @package Test\Hooks
+ */
+class EmittedException extends \Exception {
+}
+
+class BasicEmitter extends \PHPUnit_Framework_TestCase {
+ /**
+ * @var \OC\Hooks\Emitter $emitter
+ */
+ protected $emitter;
+
+ public function setUp() {
+ $this->emitter = new DummyEmitter();
+ }
+
+ public function nonStaticCallBack() {
+ throw new EmittedException;
+ }
+
+ public static function staticCallBack() {
+ throw new EmittedException;
+ }
+
+ /**
+ * @expectedException \Test\Hooks\EmittedException
+ */
+ public function testAnonymousFunction() {
+ $this->emitter->listen('Test', 'test', function () {
+ throw new EmittedException;
+ });
+ $this->emitter->emitEvent('Test', 'test');
+ }
+
+ /**
+ * @expectedException \Test\Hooks\EmittedException
+ */
+ public function testStaticCallback() {
+ $this->emitter->listen('Test', 'test', array('\Test\Hooks\BasicEmitter', 'staticCallBack'));
+ $this->emitter->emitEvent('Test', 'test');
+ }
+
+ /**
+ * @expectedException \Test\Hooks\EmittedException
+ */
+ public function testNonStaticCallback() {
+ $this->emitter->listen('Test', 'test', array($this, 'nonStaticCallBack'));
+ $this->emitter->emitEvent('Test', 'test');
+ }
+
+ public function testOnlyCallOnce() {
+ $count = 0;
+ $listener = function () use (&$count) {
+ $count++;
+ };
+ $this->emitter->listen('Test', 'test', $listener);
+ $this->emitter->listen('Test', 'test', $listener);
+ $this->emitter->emitEvent('Test', 'test');
+ $this->assertEquals(1, $count, 'Listener called an invalid number of times (' . $count . ') expected 1');
+ }
+
+ public function testDifferentMethods() {
+ $count = 0;
+ $listener = function () use (&$count) {
+ $count++;
+ };
+ $this->emitter->listen('Test', 'test', $listener);
+ $this->emitter->listen('Test', 'foo', $listener);
+ $this->emitter->emitEvent('Test', 'test');
+ $this->emitter->emitEvent('Test', 'foo');
+ $this->assertEquals(2, $count, 'Listener called an invalid number of times (' . $count . ') expected 2');
+ }
+
+ public function testDifferentScopes() {
+ $count = 0;
+ $listener = function () use (&$count) {
+ $count++;
+ };
+ $this->emitter->listen('Test', 'test', $listener);
+ $this->emitter->listen('Bar', 'test', $listener);
+ $this->emitter->emitEvent('Test', 'test');
+ $this->emitter->emitEvent('Bar', 'test');
+ $this->assertEquals(2, $count, 'Listener called an invalid number of times (' . $count . ') expected 2');
+ }
+
+ public function testDifferentCallbacks() {
+ $count = 0;
+ $listener1 = function () use (&$count) {
+ $count++;
+ };
+ $listener2 = function () use (&$count) {
+ $count++;
+ };
+ $this->emitter->listen('Test', 'test', $listener1);
+ $this->emitter->listen('Test', 'test', $listener2);
+ $this->emitter->emitEvent('Test', 'test');
+ $this->assertEquals(2, $count, 'Listener called an invalid number of times (' . $count . ') expected 2');
+ }
+
+ /**
+ * @expectedException \Test\Hooks\EmittedException
+ */
+ public function testArguments() {
+ $this->emitter->listen('Test', 'test', function ($foo, $bar) {
+ if ($foo == 'foo' and $bar == 'bar') {
+ throw new EmittedException;
+ }
+ });
+ $this->emitter->emitEvent('Test', 'test', array('foo', 'bar'));
+ }
+
+ /**
+ * @expectedException \Test\Hooks\EmittedException
+ */
+ public function testNamedArguments() {
+ $this->emitter->listen('Test', 'test', function ($foo, $bar) {
+ if ($foo == 'foo' and $bar == 'bar') {
+ throw new EmittedException;
+ }
+ });
+ $this->emitter->emitEvent('Test', 'test', array('foo' => 'foo', 'bar' => 'bar'));
+ }
+
+ public function testRemoveAllSpecified() {
+ $listener = function () {
+ throw new EmittedException;
+ };
+ $this->emitter->listen('Test', 'test', $listener);
+ $this->emitter->removeListener('Test', 'test', $listener);
+ $this->emitter->emitEvent('Test', 'test');
+ }
+
+ public function testRemoveWildcardListener() {
+ $listener1 = function () {
+ throw new EmittedException;
+ };
+ $listener2 = function () {
+ throw new EmittedException;
+ };
+ $this->emitter->listen('Test', 'test', $listener1);
+ $this->emitter->listen('Test', 'test', $listener2);
+ $this->emitter->removeListener('Test', 'test');
+ $this->emitter->emitEvent('Test', 'test');
+ }
+
+ public function testRemoveWildcardMethod() {
+ $listener = function () {
+ throw new EmittedException;
+ };
+ $this->emitter->listen('Test', 'test', $listener);
+ $this->emitter->listen('Test', 'foo', $listener);
+ $this->emitter->removeListener('Test', null, $listener);
+ $this->emitter->emitEvent('Test', 'test');
+ $this->emitter->emitEvent('Test', 'foo');
+ }
+
+ public function testRemoveWildcardScope() {
+ $listener = function () {
+ throw new EmittedException;
+ };
+ $this->emitter->listen('Test', 'test', $listener);
+ $this->emitter->listen('Bar', 'test', $listener);
+ $this->emitter->removeListener(null, 'test', $listener);
+ $this->emitter->emitEvent('Test', 'test');
+ $this->emitter->emitEvent('Bar', 'test');
+ }
+
+ public function testRemoveWildcardScopeAndMethod() {
+ $listener = function () {
+ throw new EmittedException;
+ };
+ $this->emitter->listen('Test', 'test', $listener);
+ $this->emitter->listen('Test', 'foo', $listener);
+ $this->emitter->listen('Bar', 'foo', $listener);
+ $this->emitter->removeListener(null, null, $listener);
+ $this->emitter->emitEvent('Test', 'test');
+ $this->emitter->emitEvent('Test', 'foo');
+ $this->emitter->emitEvent('Bar', 'foo');
+ }
+
+ /**
+ * @expectedException \Test\Hooks\EmittedException
+ */
+ public function testRemoveKeepOtherCallback() {
+ $listener1 = function () {
+ throw new EmittedException;
+ };
+ $listener2 = function () {
+ throw new EmittedException;
+ };
+ $this->emitter->listen('Test', 'test', $listener1);
+ $this->emitter->listen('Test', 'test', $listener2);
+ $this->emitter->removeListener('Test', 'test', $listener1);
+ $this->emitter->emitEvent('Test', 'test');
+ }
+
+ /**
+ * @expectedException \Test\Hooks\EmittedException
+ */
+ public function testRemoveKeepOtherMethod() {
+ $listener = function () {
+ throw new EmittedException;
+ };
+ $this->emitter->listen('Test', 'test', $listener);
+ $this->emitter->listen('Test', 'foo', $listener);
+ $this->emitter->removeListener('Test', 'foo', $listener);
+ $this->emitter->emitEvent('Test', 'test');
+ }
+
+ /**
+ * @expectedException \Test\Hooks\EmittedException
+ */
+ public function testRemoveKeepOtherScope() {
+ $listener = function () {
+ throw new EmittedException;
+ };
+ $this->emitter->listen('Test', 'test', $listener);
+ $this->emitter->listen('Bar', 'test', $listener);
+ $this->emitter->removeListener('Bar', 'test', $listener);
+ $this->emitter->emitEvent('Test', 'test');
+ }
+
+ /**
+ * @expectedException \Test\Hooks\EmittedException
+ */
+ public function testRemoveNonExistingName() {
+ $listener = function () {
+ throw new EmittedException;
+ };
+ $this->emitter->listen('Test', 'test', $listener);
+ $this->emitter->removeListener('Bar', 'test', $listener);
+ $this->emitter->emitEvent('Test', 'test');
+ }
+}
diff --git a/tests/lib/hooks/legacyemitter.php b/tests/lib/hooks/legacyemitter.php
new file mode 100644
index 00000000000..a7bed879a72
--- /dev/null
+++ b/tests/lib/hooks/legacyemitter.php
@@ -0,0 +1,55 @@
+<?php
+/**
+ * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace Test\Hooks;
+
+/**
+ * Class DummyLegacyEmitter
+ *
+ * class to make LegacyEmitter::emit publicly available
+ *
+ * @package Test\Hooks
+ */
+class DummyLegacyEmitter extends \OC\Hooks\LegacyEmitter {
+ public function emitEvent($scope, $method, $arguments = array()) {
+ $this->emit($scope, $method, $arguments);
+ }
+}
+
+class LegacyEmitter extends BasicEmitter {
+
+ //we can't use exceptions here since OC_Hooks catches all exceptions
+ private static $emitted = false;
+
+ public function setUp() {
+ $this->emitter = new DummyLegacyEmitter();
+ self::$emitted = false;
+ \OC_Hook::clear('Test','test');
+ }
+
+ public static function staticLegacyCallBack() {
+ self::$emitted = true;
+ }
+
+ public static function staticLegacyArgumentsCallBack($arguments) {
+ if ($arguments['foo'] == 'foo' and $arguments['bar'] == 'bar')
+ self::$emitted = true;
+ }
+
+ public function testLegacyHook() {
+ \OC_Hook::connect('Test', 'test', '\Test\Hooks\LegacyEmitter', 'staticLegacyCallBack');
+ $this->emitter->emitEvent('Test', 'test');
+ $this->assertEquals(true, self::$emitted);
+ }
+
+ public function testLegacyArguments() {
+ \OC_Hook::connect('Test', 'test', '\Test\Hooks\LegacyEmitter', 'staticLegacyArgumentsCallBack');
+ $this->emitter->emitEvent('Test', 'test', array('foo' => 'foo', 'bar' => 'bar'));
+ $this->assertEquals(true, self::$emitted);
+ }
+}