NumberFormatBehaviorTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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), 'No valid locale found.');
  127. $this->assertTrue(!empty($res));
  128. $conv = localeconv();
  129. $this->skipIf(empty($conv['thousands_sep']), 'No thousands separator in this locale.');
  130. $this->Model->Behaviors->unload('NumberFormat');
  131. $this->Model->Behaviors->load('Tools.NumberFormat', array('fields' => array('rel_rate', 'set_rate'), 'localeconv' => true, 'output' => true));
  132. $data = array(
  133. 'name' => 'german',
  134. 'set_rate' => '3,11',
  135. 'rel_rate' => '-4,22',
  136. );
  137. $this->Model->create();
  138. $res = $this->Model->save($data);
  139. $this->assertTrue((bool)$res);
  140. $res = $this->Model->find('first', array('conditions' => array('name' => 'german')));
  141. $this->assertTrue(!empty($res));
  142. //debug($res);ob_flush();
  143. $this->assertSame('3,11', substr($res[$this->Model->alias]['set_rate'], 0, 4));
  144. $this->assertSame('-4,22', substr($res[$this->Model->alias]['rel_rate'], 0, 5));
  145. $res = setlocale(LC_NUMERIC, 'en_US.utf8', 'english');
  146. $this->assertTrue(!empty($res));
  147. $this->Model->Behaviors->unload('NumberFormat');
  148. $this->Model->Behaviors->load('Tools.NumberFormat', array('fields' => array('rel_rate', 'set_rate'), 'localeconv' => true, 'output' => true));
  149. $data = array(
  150. 'name' => 'english',
  151. 'set_rate' => '3.21',
  152. 'rel_rate' => '-4.32',
  153. );
  154. $this->Model->create();
  155. $res = $this->Model->save($data);
  156. $this->assertTrue((bool)$res);
  157. $res = $this->Model->find('first', array('conditions' => array('name' => 'english')));
  158. //debug($res);
  159. $this->assertTrue(!empty($res));
  160. $this->assertSame('3.21', substr($res[$this->Model->alias]['set_rate'], 0, 4));
  161. $this->assertSame('-4.32', substr($res[$this->Model->alias]['rel_rate'], 0, 5));
  162. }
  163. /**
  164. * NumberFormatBehaviorTest::testMultiply()
  165. *
  166. * @return void
  167. */
  168. public function testMultiply() {
  169. $this->Model->Behaviors->unload('NumberFormat');
  170. $this->Model->Behaviors->load('Tools.NumberFormat', array('fields' => array('rel_rate', 'set_rate'), 'transform' => array(), 'multiply' => 0.01, 'output' => false));
  171. $data = array(
  172. 'name' => 'multiply',
  173. 'set_rate' => '122',
  174. 'rel_rate' => '-2',
  175. );
  176. $this->Model->create();
  177. $res = $this->Model->save($data);
  178. $this->assertTrue((bool)$res);
  179. $res = $this->Model->find('first', array('conditions' => array('name' => 'multiply')));
  180. //debug($res);
  181. $this->assertTrue(!empty($res));
  182. $this->assertSame('1.22', substr($res[$this->Model->alias]['set_rate'], 0, 4));
  183. $this->assertSame('-0.02', substr($res[$this->Model->alias]['rel_rate'], 0, 5));
  184. $this->Model->Behaviors->unload('NumberFormat');
  185. $this->Model->Behaviors->load('Tools.NumberFormat', array('fields' => array('rel_rate', 'set_rate'), 'transform' => array(), 'multiply' => 0.01, 'output' => true));
  186. $res = $this->Model->find('first', array('conditions' => array('name' => 'multiply')));
  187. //debug($res);
  188. $this->assertTrue(!empty($res));
  189. $this->assertSame('122', $res[$this->Model->alias]['set_rate']);
  190. $this->assertSame('-2', $res[$this->Model->alias]['rel_rate']);
  191. }
  192. }