Skip to content

Commit

Permalink
Refs #4208 refactor some singletons
Browse files Browse the repository at this point in the history
  • Loading branch information
mattab committed Oct 10, 2013
1 parent 401597b commit 7da0007
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
9 changes: 9 additions & 0 deletions core/Singleton.php
Expand Up @@ -34,4 +34,13 @@ public static function unsetInstance()
$class = get_called_class();
unset(self::$instances[$class]);
}

/**
* Sets the singleton instance. For testing purposes.
*/
public static function setSingletonInstance($instance)
{
$class = get_called_class();
self::$instances[$class] = $instance;
}
}
11 changes: 4 additions & 7 deletions tests/PHPUnit/Plugins/MobileMessagingTest.php
Expand Up @@ -213,7 +213,7 @@ public function testSanitizePhoneNumber()
*/
public function testPhoneNumberIsSanitized()
{
$mobileMessagingAPI = new APIMobileMessaging();
$mobileMessagingAPI = APIMobileMessaging::getInstance();
$mobileMessagingAPI->setSMSAPICredential('StubbedProvider', '');
$mobileMessagingAPI->addPhoneNumber(' 6 76 93 26 47');
$this->assertEquals('676932647', key($mobileMessagingAPI->getPhoneNumbers()));
Expand Down Expand Up @@ -246,21 +246,18 @@ public function testSendReport($expectedReportContent, $expectedPhoneNumber, $ex
),
);

$stubbedAPIMobileMessaging = $this->getMock('\\Piwik\\Plugins\\MobileMessaging\\API');
$stubbedAPIMobileMessaging = $this->getMock('\\Piwik\\Plugins\\MobileMessaging\\API', array('sendSMS', 'getInstance'), $arguments = array(), $mockClassName = '', $callOriginalConstructor = false);
$stubbedAPIMobileMessaging->expects($this->once())->method('sendSMS')->with(
$this->equalTo($expectedReportContent, 0),
$this->equalTo($expectedPhoneNumber, 1),
$this->equalTo($expectedFrom, 2)
);

$stubbedAPIMobileMessagingClass = new ReflectionProperty('\\Piwik\\Plugins\\MobileMessaging\\API', 'instance');
$stubbedAPIMobileMessagingClass->setAccessible(true);
$stubbedAPIMobileMessagingClass->setValue($stubbedAPIMobileMessaging);
\Piwik\Plugins\MobileMessaging\API::setSingletonInstance($stubbedAPIMobileMessaging);

$mobileMessaging = new MobileMessaging();
$mobileMessaging->sendReport($notificationInfo);

// restore API
$stubbedAPIMobileMessagingClass->setValue(null);
\Piwik\Plugins\MobileMessaging\API::unsetInstance();
}
}
13 changes: 5 additions & 8 deletions tests/PHPUnit/Plugins/ScheduledReportsTest.php
Expand Up @@ -356,14 +356,11 @@ public function testGetScheduledTasks()
$report6['period'] = ScheduledTime::PERIOD_NEVER;
$report6['deleted'] = 0;

$stubbedAPIScheduledReports = $this->getMock('\\Piwik\\Plugins\\ScheduledReports\\API');
$stubbedAPIScheduledReports = $this->getMock('\\Piwik\\Plugins\\ScheduledReports\\API', array('getReports', 'getInstance'), $arguments = array(), $mockClassName = '', $callOriginalConstructor = false);
$stubbedAPIScheduledReports->expects($this->any())->method('getReports')->will($this->returnValue(
array($report1, $report2, $report3, $report4, $report5, $report6))
);

$stubbedAPIScheduledReportsClass = new ReflectionProperty('\\Piwik\\Plugins\\ScheduledReports\\API', 'instance');
$stubbedAPIScheduledReportsClass->setAccessible(true);
$stubbedAPIScheduledReportsClass->setValue($stubbedAPIScheduledReports);
\Piwik\Plugins\ScheduledReports\API::setSingletonInstance($stubbedAPIScheduledReports);

// initialize sites 1 and 2
Site::$infoSites = array(
Expand Down Expand Up @@ -396,8 +393,8 @@ public function testGetScheduledTasks()
$pdfReportPlugin->getScheduledTasks($tasks);
$this->assertEquals($expectedTasks, $tasks);

// restore API
$stubbedAPIScheduledReportsClass->setValue(null);
\Piwik\Plugins\ScheduledReports\API::unsetInstance();

}

/**
Expand All @@ -424,7 +421,7 @@ public function testGetReportSubjectAndReportTitle($expectedReportSubject, $expe
);
$getReportSubjectAndReportTitle->setAccessible(true);

list($reportSubject, $reportTitle) = $getReportSubjectAndReportTitle->invoke(new APIScheduledReports(), $websiteName, $reports);
list($reportSubject, $reportTitle) = $getReportSubjectAndReportTitle->invoke( APIScheduledReports::getInstance(), $websiteName, $reports);
$this->assertEquals($expectedReportSubject, $reportSubject);
$this->assertEquals($expectedReportTitle, $reportTitle);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPUnit/proxy/index.php
Expand Up @@ -21,7 +21,7 @@
define('PIWIK_ENABLE_DISPATCH', false);
include PIWIK_INCLUDE_PATH . '/index.php';

$controller = new \Piwik\FrontController;
$controller = \Piwik\FrontController::getInstance();
$controller->init();
$controller->dispatch();

Expand Down

0 comments on commit 7da0007

Please sign in to comment.