From ddd7aac6f2d1e3b1660f38045cab4214a26f2325 Mon Sep 17 00:00:00 2001 From: Benaka Moorthi Date: Fri, 5 Jul 2013 19:44:45 -0400 Subject: Implemented custom event dispatching system for Piwik (replaced event dispatcher lib in libs/Event). Notes: - New dispatcher can execute callbacks before or after other callbacks. - It is also possible to dispatch events only to a specific set of plugins instead of all plugins. - Moved Piwik::unprefixClass to Piwik_Common::unprefixClass - Added visibility to some event handlers that were missing it. - Allowed two unit tests to fail w/ better diagnostic messages. --- tests/PHPUnit/Core/JsProxyTest.php | 7 +++++-- tests/PHPUnit/Core/PiwikTest.php | 4 ++++ tests/PHPUnit/Core/TaskSchedulerTest.php | 16 ++++++++++++---- tests/PHPUnit/MockEventDispatcher.php | 23 ----------------------- tests/PHPUnit/Plugins/MobileMessagingTest.php | 20 ++++++++------------ tests/PHPUnit/Plugins/PDFReportsTest.php | 2 +- tests/PHPUnit/Plugins/PrivacyManagerTest.php | 4 +--- tests/PHPUnit/bootstrap.php | 1 - 8 files changed, 31 insertions(+), 46 deletions(-) delete mode 100644 tests/PHPUnit/MockEventDispatcher.php (limited to 'tests') diff --git a/tests/PHPUnit/Core/JsProxyTest.php b/tests/PHPUnit/Core/JsProxyTest.php index 295cf5a682..2efbcdae74 100644 --- a/tests/PHPUnit/Core/JsProxyTest.php +++ b/tests/PHPUnit/Core/JsProxyTest.php @@ -35,8 +35,11 @@ class Test_Piwik_JsProxy extends PHPUnit_Framework_TestCase curl_close($curlHandle); $this->assertEquals($responseInfo["http_code"], 200, 'Ok response'); - $ok = $fullResponse == base64_decode("R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="); - $this->assertTrue($ok, '1*1 pixel was expected, got: "'.$fullResponse.'"'); + $this->assertEquals( + "R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==", + base64_encode($fullResponse), + 'checking for image content' + ); } /** diff --git a/tests/PHPUnit/Core/PiwikTest.php b/tests/PHPUnit/Core/PiwikTest.php index ade7e91778..1d8f075c2b 100644 --- a/tests/PHPUnit/Core/PiwikTest.php +++ b/tests/PHPUnit/Core/PiwikTest.php @@ -106,6 +106,10 @@ class PiwikTest extends DatabaseTestCase */ public function testGetPrettyTimeFromSeconds($seconds, $expected) { + if (($seconds * 100) > PHP_INT_MAX) { + $this->markTestSkipped("Will not pass on 32-bit machine."); + } + Piwik_Translate::getInstance()->loadEnglishTranslation(); $sentenceExpected = str_replace(' ', ' ', $expected[0]); diff --git a/tests/PHPUnit/Core/TaskSchedulerTest.php b/tests/PHPUnit/Core/TaskSchedulerTest.php index a774666e5a..f168abc6ad 100644 --- a/tests/PHPUnit/Core/TaskSchedulerTest.php +++ b/tests/PHPUnit/Core/TaskSchedulerTest.php @@ -277,8 +277,15 @@ class TaskSchedulerTest extends PHPUnit_Framework_TestCase */ public function testRunTasks($expectedTimetable, $expectedExecutedTasks, $timetableBeforeTaskExecution, $configuredTasks) { - // stub the event dispatcher so we can control the returned event notification - Piwik_PluginsManager::getInstance()->dispatcher = new MockEventDispatcher($configuredTasks); + // temporarily unload plugins + $plugins = Piwik_PluginsManager::getInstance()->getLoadedPlugins(); + $plugins = array_map(function ($p) { return $p->getPluginName(); }, $plugins); + Piwik_PluginsManager::getInstance()->unloadPlugins(); + + // make sure the get tasks event returns our configured tasks + Piwik_AddAction(Piwik_TaskScheduler::GET_TASKS_EVENT, function(&$tasks) use($configuredTasks) { + $tasks = $configuredTasks; + }); // stub the piwik option object to control the returned option value self::stubPiwikOption(serialize($timetableBeforeTaskExecution)); @@ -299,8 +306,9 @@ class TaskSchedulerTest extends PHPUnit_Framework_TestCase $getTimetableFromOptionTable->setAccessible(TRUE); $this->assertEquals($expectedTimetable, $getTimetableFromOptionTable->invoke(new Piwik_TaskScheduler())); - // restore event dispatcher & piwik options - Piwik_PluginsManager::getInstance()->dispatcher = Event_Dispatcher::getInstance(); + // restore loaded plugins & piwik options + Piwik_EventDispatcher::getInstance()->clearObservers(Piwik_TaskScheduler::GET_TASKS_EVENT); + Piwik_PluginsManager::getInstance()->loadPlugins($plugins); self::resetPiwikOption(); } diff --git a/tests/PHPUnit/MockEventDispatcher.php b/tests/PHPUnit/MockEventDispatcher.php deleted file mode 100644 index a5528e61e0..0000000000 --- a/tests/PHPUnit/MockEventDispatcher.php +++ /dev/null @@ -1,23 +0,0 @@ -forcedNotificationObject = $forcedNotificationObject; - } - - function &postNotification(&$notification, $pending = true, $bubble = true) - { - $notification->_notificationObject = $this->forcedNotificationObject; - - return $notification; - } -} diff --git a/tests/PHPUnit/Plugins/MobileMessagingTest.php b/tests/PHPUnit/Plugins/MobileMessagingTest.php index 04689274c0..8843b3e2c6 100644 --- a/tests/PHPUnit/Plugins/MobileMessagingTest.php +++ b/tests/PHPUnit/Plugins/MobileMessagingTest.php @@ -230,17 +230,13 @@ class MobileMessagingTest extends DatabaseTestCase */ public function testSendReport($expectedReportContent, $expectedPhoneNumber, $expectedFrom, $reportContent, $phoneNumber, $reportSubject) { - $eventNotification = new Piwik_Event_Notification( - $this, - null, - array( - Piwik_PDFReports_API::REPORT_CONTENT_KEY => $reportContent, - Piwik_PDFReports_API::REPORT_SUBJECT_KEY => $reportSubject, - Piwik_PDFReports_API::REPORT_TYPE_INFO_KEY => Piwik_MobileMessaging::MOBILE_TYPE, - Piwik_PDFReports_API::REPORT_KEY => array( - 'parameters' => array(Piwik_MobileMessaging::PHONE_NUMBERS_PARAMETER => array($phoneNumber)), - ), - ) + $notificationInfo = array( + Piwik_PDFReports_API::REPORT_CONTENT_KEY => $reportContent, + Piwik_PDFReports_API::REPORT_SUBJECT_KEY => $reportSubject, + Piwik_PDFReports_API::REPORT_TYPE_INFO_KEY => Piwik_MobileMessaging::MOBILE_TYPE, + Piwik_PDFReports_API::REPORT_KEY => array( + 'parameters' => array(Piwik_MobileMessaging::PHONE_NUMBERS_PARAMETER => array($phoneNumber)), + ), ); $stubbedMobileMessagingAPI = $this->getMock('Piwik_MobileMessaging_API'); @@ -255,7 +251,7 @@ class MobileMessagingTest extends DatabaseTestCase $stubbedMobileMessagingAPIClass->setValue($stubbedMobileMessagingAPI); $mobileMessaging = new Piwik_MobileMessaging(); - $mobileMessaging->sendReport($eventNotification); + $mobileMessaging->sendReport($notificationInfo); // restore Piwik_MobileMessaging_API $stubbedMobileMessagingAPIClass->setValue(null); diff --git a/tests/PHPUnit/Plugins/PDFReportsTest.php b/tests/PHPUnit/Plugins/PDFReportsTest.php index 23777914db..3ddb0c1915 100644 --- a/tests/PHPUnit/Plugins/PDFReportsTest.php +++ b/tests/PHPUnit/Plugins/PDFReportsTest.php @@ -381,7 +381,7 @@ class PDFReportsTest extends DatabaseTestCase $pdfReportPlugin = new Piwik_PDFReports(); $tasks = array(); - $pdfReportPlugin->getScheduledTasks(new Piwik_Event_Notification($tasks, 'fakeEvent')); + $pdfReportPlugin->getScheduledTasks($tasks); $this->assertEquals($expectedTasks, $tasks); // restore Piwik_PDFReports_API diff --git a/tests/PHPUnit/Plugins/PrivacyManagerTest.php b/tests/PHPUnit/Plugins/PrivacyManagerTest.php index d227c951d7..1d4b235c79 100755 --- a/tests/PHPUnit/Plugins/PrivacyManagerTest.php +++ b/tests/PHPUnit/Plugins/PrivacyManagerTest.php @@ -804,10 +804,8 @@ class PrivacyManagerTest extends IntegrationTestCase * does the insert into the temporary table. When log_actions are deleted, this idaction should still * be kept. w/ the wrong strategy, it won't be and there will be a dangling reference * in the log_link_visit_action table. - * - * @param Piwik_Event_Notification $notification notification object */ - public function addReferenceToUnusedAction($notification) + public function addReferenceToUnusedAction() { $unusedIdAction = $this->unusedIdAction; if (empty($unusedIdAction)) // make sure we only do this for one test case diff --git a/tests/PHPUnit/bootstrap.php b/tests/PHPUnit/bootstrap.php index 53a09c14db..e31ac8cc72 100644 --- a/tests/PHPUnit/bootstrap.php +++ b/tests/PHPUnit/bootstrap.php @@ -60,7 +60,6 @@ require_once PIWIK_INCLUDE_PATH . '/tests/PHPUnit/DatabaseTestCase.php'; require_once PIWIK_INCLUDE_PATH . '/tests/PHPUnit/IntegrationTestCase.php'; require_once PIWIK_INCLUDE_PATH . '/tests/PHPUnit/FakeAccess.php'; require_once PIWIK_INCLUDE_PATH . '/tests/PHPUnit/MockPiwikOption.php'; -require_once PIWIK_INCLUDE_PATH . '/tests/PHPUnit/MockEventDispatcher.php'; require_once PIWIK_INCLUDE_PATH . '/vendor/autoload.php'; // required to build code coverage for uncovered files -- cgit v1.2.3