Skip to content

Commit

Permalink
Merge branch '2.1-helper-refactor2' into 2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
rchavik committed Feb 14, 2012
2 parents 25ee90a + d6fd051 commit 5d95986
Show file tree
Hide file tree
Showing 10 changed files with 215 additions and 132 deletions.
5 changes: 5 additions & 0 deletions lib/Cake/Test/Case/Utility/CakeTimeTest.php
Expand Up @@ -328,6 +328,11 @@ public function testNice() {

$this->Time->niceFormat = '%Y-%d-%m';
$this->assertEquals(date('Y-d-m', $time), $this->Time->nice($time));
$this->assertEquals('%Y-%d-%m', $this->Time->niceFormat);

CakeTime::$niceFormat = '%Y-%d-%m %H:%M:%S';
$this->assertEquals(date('Y-d-m H:i:s', $time), $this->Time->nice($time));
$this->assertEquals('%Y-%d-%m %H:%M:%S', $this->Time->niceFormat);
}

/**
Expand Down
22 changes: 14 additions & 8 deletions lib/Cake/Test/Case/View/Helper/NumberHelperTest.php
Expand Up @@ -25,12 +25,18 @@
*/
class NumberHelperTestObject extends NumberHelper {

public function attach(CakeNumber $cakeNumber) {
public function attach(CakeNumberMock $cakeNumber) {
$this->_CakeNumber = $cakeNumber;
}

}

/**
* CakeNumberMock class
*/
class CakeNumberMock {
}

/**
* NumberHelperTest class
*
Expand All @@ -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);
}

/**
Expand All @@ -58,7 +61,7 @@ public function setUp() {
*/
public function tearDown() {
parent::tearDown();
unset($this->Number);
unset($this->View);
}


Expand All @@ -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');
}
}

Expand Down
26 changes: 16 additions & 10 deletions lib/Cake/Test/Case/View/Helper/TextHelperTest.php
Expand Up @@ -22,12 +22,18 @@

class TextHelperTestObject extends TextHelper {

public function attach(String $string) {
public function attach(StringMock $string) {
$this->_String = $string;
}

}

/**
* StringMock class
*/
class StringMock {
}

/**
* TextHelperTest class
*
Expand All @@ -41,8 +47,8 @@ class TextHelperTest extends CakeTestCase {
* @return void
*/
public function setUp() {
$controller = null;
$this->View = new View($controller);
parent::setUp();
$this->View = new View(null);
$this->Text = new TextHelper($this->View);
}

Expand All @@ -52,23 +58,23 @@ public function setUp() {
* @return void
*/
public function tearDown() {
unset($this->View, $this->Text);
unset($this->View);
parent::tearDown();
}

/**
* test String class methods are called correctly
*/
public function testTextHelperProxyMethodCalls() {
$this->String = $this->getMock('String');
unset($this->Text);
$this->Text = new TextHelperTestObject($this->View);
$this->Text->attach($this->String);
$methods = array(
'highlight', 'stripLinks', 'truncate', 'excerpt', 'toList',
);
$String = $this->getMock('StringMock', $methods);
$Text = new TextHelperTestObject($this->View, array('engine' => 'StringMock'));
$Text->attach($String);
foreach ($methods as $method) {
$this->String->expects($this->at(0))->method($method);
$this->Text->{$method}('who', 'what', 'when', 'where', 'how');
$String->expects($this->at(0))->method($method);
$Text->{$method}('who', 'what', 'when', 'where', 'how');
}
}

Expand Down
25 changes: 16 additions & 9 deletions lib/Cake/Test/Case/View/Helper/TimeHelperTest.php
Expand Up @@ -25,12 +25,18 @@
*/
class TimeHelperTestObject extends TimeHelper {

public function attach(CakeTime $cakeTime) {
public function attach(CakeTimeMock $cakeTime) {
$this->_CakeTime = $cakeTime;
}

}

/**
* CakeTimeMock class
*/
class CakeTimeMock {
}

/**
* TimeHelperTest class
*
Expand All @@ -48,10 +54,8 @@ class TimeHelperTest extends CakeTestCase {
* @return void
*/
public function setUp() {
$View = new View(null);
$this->CakeTime = $this->getMock('CakeTime');
$this->Time = new TimeHelperTestObject($View);
$this->Time->attach($this->CakeTime);
parent::setUp();
$this->View = new View(null);
}

/**
Expand All @@ -60,8 +64,8 @@ public function setUp() {
* @return void
*/
public function tearDown() {
unset($this->Time);
unset($this->CakeTime);
unset($this->View);
parent::tearDown();
}

/**
Expand All @@ -75,9 +79,12 @@ public function testTimeHelperProxyMethodCalls() {
'isTomorrow', 'toQuarter', 'toUnix', 'toAtom', 'toRSS',
'timeAgoInWords', 'wasWithinLast', 'gmt', 'format', 'i18nFormat',
);
$CakeTime = $this->getMock('CakeTimeMock', $methods);
$Time = new TimeHelperTestObject($this->View, array('engine' => 'CakeTimeMock'));
$Time->attach($CakeTime);
foreach ($methods as $method) {
$this->CakeTime->expects($this->at(0))->method($method);
$this->Time->{$method}('who', 'what', 'when', 'where', 'how');
$CakeTime->expects($this->at(0))->method($method);
$Time->{$method}('who', 'what', 'when', 'where', 'how');
}
}

Expand Down
24 changes: 12 additions & 12 deletions lib/Cake/Utility/CakeNumber.php
Expand Up @@ -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);
}

Expand All @@ -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));
}
}

Expand All @@ -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) . '%';
}

/**
Expand All @@ -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;
Expand Down Expand Up @@ -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])) {
Expand Down Expand Up @@ -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'] == '()') {
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 5d95986

Please sign in to comment.