Permalink
Browse files

changing methods in CakeNumber class to static

  • Loading branch information...
1 parent abd6d22 commit 9624c27ef47937948da14977fc78b605496062e7 @rchavik rchavik committed Feb 13, 2012
@@ -25,13 +25,19 @@
*/
class NumberHelperTestObject extends NumberHelper {
- public function attach(CakeNumber $cakeNumber) {
+ public function attach(CakeNumberMock $cakeNumber) {
$this->_CakeNumber = $cakeNumber;
}
}
/**
+ * CakeNumberMock class
+ */
+class CakeNumberMock {
+}
+
+/**
* NumberHelperTest class
*
* @package Cake.Test.Case.View.Helper
@@ -45,10 +51,7 @@ class NumberHelperTest extends CakeTestCase {
*/
public function setUp() {
parent::setUp();
- $view = $this->getMock('View', array(), array(), '', false);
- $this->CakeNumber = $this->getMock('CakeNumber');
- $this->Number = new NumberHelperTestObject($view);
- $this->Number->attach($this->CakeNumber);
+ $this->View = new View(null);
}
/**
@@ -58,7 +61,7 @@ public function setUp() {
*/
public function tearDown() {
parent::tearDown();
- unset($this->Number);
+ unset($this->View);
}
@@ -70,9 +73,12 @@ public function testNumberHelperProxyMethodCalls() {
'precision', 'toReadableSize', 'toPercentage', 'format',
'currency', 'addFormat',
);
+ $CakeNumber = $this->getMock('CakeNumberMock', $methods);
+ $Number = new NumberHelperTestObject($this->View, array('engine' => 'CakeNumberMock'));
+ $Number->attach($CakeNumber);
foreach ($methods as $method) {
- $this->CakeNumber->expects($this->at(0))->method($method);
- $this->Number->{$method}('who', 'what', 'when', 'where', 'how');
+ $CakeNumber->expects($this->at(0))->method($method);
+ $Number->{$method}('who', 'what', 'when', 'where', 'how');
}
}
@@ -68,7 +68,7 @@ class CakeNumber {
* @return float Formatted float.
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::precision
*/
- public function precision($number, $precision = 3) {
+ public static function precision($number, $precision = 3) {
return sprintf("%01.{$precision}f", $number);
}
@@ -79,18 +79,18 @@ public function precision($number, $precision = 3) {
* @return string Human readable size
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toReadableSize
*/
- public function toReadableSize($size) {
+ public static function toReadableSize($size) {
switch (true) {
case $size < 1024:
return __dn('cake', '%d Byte', '%d Bytes', $size, $size);
case round($size / 1024) < 1024:
- return __d('cake', '%d KB', $this->precision($size / 1024, 0));
+ return __d('cake', '%d KB', self::precision($size / 1024, 0));
case round($size / 1024 / 1024, 2) < 1024:
- return __d('cake', '%.2f MB', $this->precision($size / 1024 / 1024, 2));
+ return __d('cake', '%.2f MB', self::precision($size / 1024 / 1024, 2));
case round($size / 1024 / 1024 / 1024, 2) < 1024:
- return __d('cake', '%.2f GB', $this->precision($size / 1024 / 1024 / 1024, 2));
+ return __d('cake', '%.2f GB', self::precision($size / 1024 / 1024 / 1024, 2));
default:
- return __d('cake', '%.2f TB', $this->precision($size / 1024 / 1024 / 1024 / 1024, 2));
+ return __d('cake', '%.2f TB', self::precision($size / 1024 / 1024 / 1024 / 1024, 2));
}
}
@@ -102,8 +102,8 @@ public function toReadableSize($size) {
* @return string Percentage string
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toPercentage
*/
- public function toPercentage($number, $precision = 2) {
- return $this->precision($number, $precision) . '%';
+ public static function toPercentage($number, $precision = 2) {
+ return self::precision($number, $precision) . '%';
}
/**
@@ -115,7 +115,7 @@ public function toPercentage($number, $precision = 2) {
* @return string formatted number
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::format
*/
- public function format($number, $options = false) {
+ public static function format($number, $options = false) {
$places = 0;
if (is_int($options)) {
$places = $options;
@@ -172,7 +172,7 @@ public function format($number, $options = false) {
* @return string Number formatted as a currency.
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::currency
*/
- public function currency($number, $currency = 'USD', $options = array()) {
+ public static function currency($number, $currency = 'USD', $options = array()) {
$default = self::$_currencyDefaults;
if (isset(self::$_currencies[$currency])) {
@@ -210,7 +210,7 @@ public function currency($number, $currency = 'USD', $options = array()) {
$options[$position] = $options[$symbolKey.'Symbol'];
$abs = abs($number);
- $result = $this->format($abs, $options);
+ $result = self::format($abs, $options);
if ($number < 0 ) {
if ($options['negative'] == '()') {
@@ -247,7 +247,7 @@ public function currency($number, $currency = 'USD', $options = array()) {
* @see NumberHelper::currency()
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::addFormat
*/
- public function addFormat($formatName, $options) {
+ public static function addFormat($formatName, $options) {
self::$_currencies[$formatName] = $options + self::$_currencyDefaults;
}
@@ -43,14 +43,21 @@ class NumberHelper extends AppHelper {
* @param array $settings Configuration settings for the helper
*/
function __construct(View $View, $settings = array()) {
+ $settings = Set::merge(array('engine' => 'CakeNumber'), $settings);
parent::__construct($View, $settings);
- $this->_CakeNumber = new CakeNumber();
+ $engineClass = $settings['engine'];
+ App::uses($engineClass, 'Utility');
+ if (class_exists($engineClass)) {
+ $this->_CakeNumber = new $engineClass($settings);
+ } else {
+ throw new CakeException(__d('cake_dev', '%s could not be found', $engineClass));
+ }
}
/**
* Call methods from CakeNumber utility class
*/
- function __call($method, $params) {
+ public function __call($method, $params) {
return call_user_func_array(array($this->_CakeNumber, $method), $params);
}

0 comments on commit 9624c27

Please sign in to comment.