NumberFormatBehaviorTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. App::uses('NumberFormatBehavior', 'Tools.Model/Behavior');
  3. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  4. class NumberFormatBehaviorTest extends MyCakeTestCase {
  5. public $fixtures = array('plugin.tools.payment_method');
  6. public $Model;
  7. public function setUp() {
  8. parent::setUp();
  9. Configure::delete('Localization');
  10. $this->Model = ClassRegistry::init('PaymentMethod');
  11. $this->Model->Behaviors->load('Tools.NumberFormat', array('fields' => array('rel_rate', 'set_rate'), 'output' => true));
  12. }
  13. public function tearDown() {
  14. parent::tearDown();
  15. unset($this->Model);
  16. }
  17. public function testObject() {
  18. $this->assertInstanceOf('NumberFormatBehavior', $this->Model->Behaviors->NumberFormat);
  19. }
  20. public function testBasic() {
  21. //echo $this->_header(__FUNCTION__);
  22. $data = array(
  23. 'name' => 'some Name',
  24. 'set_rate' => '0,1',
  25. 'rel_rate' => '-0,02',
  26. );
  27. $this->Model->set($data);
  28. $res = $this->Model->validates();
  29. $this->assertTrue($res);
  30. $res = $this->Model->data;
  31. //debug($res);
  32. $this->assertSame($res[$this->Model->alias]['set_rate'], 0.1);
  33. $this->assertSame($res[$this->Model->alias]['rel_rate'], -0.02);
  34. }
  35. public function testValidates() {
  36. //echo $this->_header(__FUNCTION__);
  37. $data = array(
  38. 'name' => 'some Name',
  39. 'set_rate' => '0,1',
  40. 'rel_rate' => '-0,02',
  41. );
  42. $this->Model->set($data);
  43. $res = $this->Model->validates();
  44. $this->assertTrue($res);
  45. $res = $this->Model->data;
  46. //debug($res);
  47. $this->assertSame($res[$this->Model->alias]['set_rate'], 0.1);
  48. $this->assertSame($res[$this->Model->alias]['rel_rate'], -0.02);
  49. }
  50. /**
  51. * NumberFormatBehaviorTest::testFind()
  52. *
  53. * @return void
  54. */
  55. public function testFind() {
  56. $data = array(
  57. 'name' => 'some Name',
  58. 'set_rate' => '0,1',
  59. 'rel_rate' => '-0,02',
  60. );
  61. $this->Model->create();
  62. $res = $this->Model->save($data);
  63. $this->assertTrue((bool)$res);
  64. // find all
  65. $res = $this->Model->find('all', array('order' => array('created' => 'DESC')));
  66. $this->assertTrue(!empty($res));
  67. $this->assertSame('0,10', substr($res[0][$this->Model->alias]['set_rate'], 0, 4));
  68. $this->assertSame('-0,02', substr($res[0][$this->Model->alias]['rel_rate'], 0, 5));
  69. // find first
  70. $res = $this->Model->find('first', array('order' => array('created' => 'DESC')));
  71. $this->assertTrue(!empty($res));
  72. $this->assertSame('0,10', $res[$this->Model->alias]['set_rate']);
  73. $this->assertSame('-0,0200', $res[$this->Model->alias]['rel_rate']);
  74. $res = $this->Model->find('count', array());
  75. $this->assertSame(8, $res);
  76. }
  77. /**
  78. * NumberFormatBehaviorTest::testStrict()
  79. *
  80. * @return void
  81. */
  82. public function testStrict() {
  83. $this->Model->Behaviors->unload('NumberFormat');
  84. $this->Model->Behaviors->load('Tools.NumberFormat', array('fields' => array('rel_rate', 'set_rate'), 'strict' => true));
  85. $data = array(
  86. 'name' => 'some Name',
  87. 'set_rate' => '0.1',
  88. 'rel_rate' => '-0,02',
  89. );
  90. $this->Model->set($data);
  91. $res = $this->Model->validates();
  92. $this->assertTrue($res);
  93. $res = $this->Model->data;
  94. //debug($res);
  95. $this->assertSame('0#1', $res[$this->Model->alias]['set_rate']);
  96. $this->assertSame(-0.02, $res[$this->Model->alias]['rel_rate']);
  97. }
  98. /**
  99. * NumberFormatBehaviorTest::testBeforeSave()
  100. *
  101. * @return void
  102. */
  103. public function testBeforeSave() {
  104. $this->Model->Behaviors->unload('NumberFormat');
  105. $this->Model->Behaviors->load('Tools.NumberFormat', array('fields' => array('rel_rate', 'set_rate'), 'before' => 'save', 'output' => false));
  106. $data = array(
  107. 'name' => 'some Name',
  108. 'set_rate' => '2,11',
  109. 'rel_rate' => '-1,22',
  110. );
  111. $this->Model->create();
  112. $res = $this->Model->save($data);
  113. $this->assertTrue((bool)$res);
  114. $res = $this->Model->find('first', array('order' => array('created' => 'DESC')));
  115. $this->assertTrue(!empty($res));
  116. $this->assertSame('2.11', substr($res[$this->Model->alias]['set_rate'], 0, 4));
  117. $this->assertSame('-1.22', substr($res[$this->Model->alias]['rel_rate'], 0, 5));
  118. }
  119. /**
  120. * NumberFormatBehaviorTest::testLocaleConv()
  121. *
  122. * @return void
  123. */
  124. public function testLocaleConv() {
  125. $res = setlocale(LC_NUMERIC, 'de_DE.utf8', 'german');
  126. $this->skipIf(empty($res));
  127. debug($res);ob_flush();
  128. $this->assertTrue(!empty($res));
  129. $this->Model->Behaviors->unload('NumberFormat');
  130. $this->Model->Behaviors->load('Tools.NumberFormat', array('fields' => array('rel_rate', 'set_rate'), 'localeconv' => true, 'output' => true));
  131. $data = array(
  132. 'name' => 'german',
  133. 'set_rate' => '3,11',
  134. 'rel_rate' => '-4,22',
  135. );
  136. $this->Model->create();
  137. $res = $this->Model->save($data);
  138. $this->assertTrue((bool)$res);
  139. $res = $this->Model->find('first', array('conditions' => array('name' => 'german')));
  140. $this->assertTrue(!empty($res));
  141. //debug($res);ob_flush();
  142. $this->assertSame('3,11', substr($res[$this->Model->alias]['set_rate'], 0, 4));
  143. $this->assertSame('-4,22', substr($res[$this->Model->alias]['rel_rate'], 0, 5));
  144. $res = setlocale(LC_NUMERIC, 'en_US.utf8', 'english');
  145. $this->assertTrue(!empty($res));
  146. $this->Model->Behaviors->unload('NumberFormat');
  147. $this->Model->Behaviors->load('Tools.NumberFormat', array('fields' => array('rel_rate', 'set_rate'), 'localeconv' => true, 'output' => true));
  148. $data = array(
  149. 'name' => 'english',
  150. 'set_rate' => '3.21',
  151. 'rel_rate' => '-4.32',
  152. );
  153. $this->Model->create();
  154. $res = $this->Model->save($data);
  155. $this->assertTrue((bool)$res);
  156. $res = $this->Model->find('first', array('conditions' => array('name' => 'english')));
  157. //debug($res);
  158. $this->assertTrue(!empty($res));
  159. $this->assertSame('3.21', substr($res[$this->Model->alias]['set_rate'], 0, 4));
  160. $this->assertSame('-4.32', substr($res[$this->Model->alias]['rel_rate'], 0, 5));
  161. }
  162. /**
  163. * NumberFormatBehaviorTest::testMultiply()
  164. *
  165. * @return void
  166. */
  167. public function testMultiply() {
  168. $this->Model->Behaviors->unload('NumberFormat');
  169. $this->Model->Behaviors->load('Tools.NumberFormat', array('fields' => array('rel_rate', 'set_rate'), 'transform' => array(), 'multiply' => 0.01, 'output' => false));
  170. $data = array(
  171. 'name' => 'multiply',
  172. 'set_rate' => '122',
  173. 'rel_rate' => '-2',
  174. );
  175. $this->Model->create();
  176. $res = $this->Model->save($data);
  177. $this->assertTrue((bool)$res);
  178. $res = $this->Model->find('first', array('conditions' => array('name' => 'multiply')));
  179. //debug($res);
  180. $this->assertTrue(!empty($res));
  181. $this->assertSame('1.22', substr($res[$this->Model->alias]['set_rate'], 0, 4));
  182. $this->assertSame('-0.02', substr($res[$this->Model->alias]['rel_rate'], 0, 5));
  183. $this->Model->Behaviors->unload('NumberFormat');
  184. $this->Model->Behaviors->load('Tools.NumberFormat', array('fields' => array('rel_rate', 'set_rate'), 'transform' => array(), 'multiply' => 0.01, 'output' => true));
  185. $res = $this->Model->find('first', array('conditions' => array('name' => 'multiply')));
  186. //debug($res);
  187. $this->assertTrue(!empty($res));
  188. $this->assertSame('122', $res[$this->Model->alias]['set_rate']);
  189. $this->assertSame('-2', $res[$this->Model->alias]['rel_rate']);
  190. }
  191. }