DecimalInputBehaviorTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. App::uses('DecimalInputBehavior', 'Tools.Model/Behavior');
  3. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  4. class DecimalInputBehaviorTest 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.DecimalInput', 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('DecimalInputBehavior', $this->Model->Behaviors->DecimalInput);
  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. public function testFind() {
  51. //echo $this->_header(__FUNCTION__);
  52. $data = array(
  53. 'name' => 'some Name',
  54. 'set_rate' => '0,1',
  55. 'rel_rate' => '-0,02',
  56. );
  57. $this->Model->create();
  58. $res = $this->Model->save($data);
  59. $this->assertTrue((bool)$res);
  60. // find all
  61. $res = $this->Model->find('all', array('order' => array('created' => 'DESC')));
  62. $this->assertTrue(!empty($res));
  63. $this->assertSame(substr($res[0][$this->Model->alias]['set_rate'], 0, 4), '0,10');
  64. $this->assertSame(substr($res[0][$this->Model->alias]['rel_rate'], 0, 5), '-0,02');
  65. // find first
  66. $res = $this->Model->find('first', array('order' => array('created' => 'DESC')));
  67. $this->assertTrue(!empty($res));
  68. $this->assertSame($res[$this->Model->alias]['set_rate'], '0,10');
  69. $this->assertSame($res[$this->Model->alias]['rel_rate'], '-0,0200');
  70. $res = $this->Model->find('count', array());
  71. $this->assertSame($res, 8);
  72. }
  73. public function testStrict() {
  74. $this->Model->Behaviors->unload('DecimalInput');
  75. $this->Model->Behaviors->load('Tools.DecimalInput', array('fields' => array('rel_rate', 'set_rate'), 'strict' => true));
  76. $data = array(
  77. 'name' => 'some Name',
  78. 'set_rate' => '0.1',
  79. 'rel_rate' => '-0,02',
  80. );
  81. $this->Model->set($data);
  82. $res = $this->Model->validates();
  83. $this->assertTrue($res);
  84. $res = $this->Model->data;
  85. //debug($res);
  86. $this->assertSame($res[$this->Model->alias]['set_rate'], '0#1');
  87. $this->assertSame($res[$this->Model->alias]['rel_rate'], -0.02);
  88. }
  89. public function testBeforeSave() {
  90. $this->Model->Behaviors->unload('DecimalInput');
  91. $this->Model->Behaviors->load('Tools.DecimalInput', array('fields' => array('rel_rate', 'set_rate'), 'before' => 'save', 'output' => false));
  92. $data = array(
  93. 'name' => 'some Name',
  94. 'set_rate' => '2,11',
  95. 'rel_rate' => '-1,22',
  96. );
  97. $this->Model->create();
  98. $res = $this->Model->save($data);
  99. $this->assertTrue((bool)$res);
  100. $res = $this->Model->find('first', array('order' => array('created' => 'DESC')));
  101. $this->assertTrue(!empty($res));
  102. $this->assertSame(substr($res[$this->Model->alias]['set_rate'], 0, 4), '2.11');
  103. $this->assertSame(substr($res[$this->Model->alias]['rel_rate'], 0, 5), '-1.22');
  104. }
  105. public function testLocaleConv() {
  106. $res = setlocale(LC_NUMERIC, 'de_DE.utf8', 'german');
  107. $this->skipIf(empty($res), 'No valid locale found.');
  108. $this->assertTrue(!empty($res));
  109. $conv = localeconv();
  110. $this->skipIf(empty($conv['thousands_sep']), 'No thousands separator in this locale.');
  111. $this->Model->Behaviors->unload('DecimalInput');
  112. $this->Model->Behaviors->load('Tools.DecimalInput', array('fields' => array('rel_rate', 'set_rate'), 'localeconv' => true, 'output' => true));
  113. $data = array(
  114. 'name' => 'german',
  115. 'set_rate' => '3,11',
  116. 'rel_rate' => '-4,22',
  117. );
  118. $this->Model->create();
  119. $res = $this->Model->save($data);
  120. $this->assertTrue((bool)$res);
  121. $res = $this->Model->find('first', array('conditions' => array('name' => 'german')));
  122. $this->assertTrue(!empty($res));
  123. $this->assertSame(substr($res[$this->Model->alias]['set_rate'], 0, 4), '3,11');
  124. $this->assertSame(substr($res[$this->Model->alias]['rel_rate'], 0, 5), '-4,22');
  125. $res = setlocale(LC_NUMERIC, 'en_US.utf8', 'english');
  126. $this->assertTrue(!empty($res));
  127. $this->Model->Behaviors->unload('DecimalInput');
  128. $this->Model->Behaviors->load('Tools.DecimalInput', array('fields' => array('rel_rate', 'set_rate'), 'localeconv' => true, 'output' => true));
  129. $data = array(
  130. 'name' => 'english',
  131. 'set_rate' => '3.21',
  132. 'rel_rate' => '-4.32',
  133. );
  134. $this->Model->create();
  135. $res = $this->Model->save($data);
  136. $this->assertTrue((bool)$res);
  137. $res = $this->Model->find('first', array('conditions' => array('name' => 'english')));
  138. //debug($res);
  139. $this->assertTrue(!empty($res));
  140. $this->assertSame(substr($res[$this->Model->alias]['set_rate'], 0, 4), '3.21');
  141. $this->assertSame(substr($res[$this->Model->alias]['rel_rate'], 0, 5), '-4.32');
  142. }
  143. public function testMultiply() {
  144. $this->Model->Behaviors->unload('DecimalInput');
  145. $this->Model->Behaviors->load('Tools.DecimalInput', array('fields' => array('rel_rate', 'set_rate'), 'transform' => array(), 'multiply' => 0.01, 'output' => false));
  146. $data = array(
  147. 'name' => 'multiply',
  148. 'set_rate' => '122',
  149. 'rel_rate' => '-2',
  150. );
  151. $this->Model->create();
  152. $res = $this->Model->save($data);
  153. $this->assertTrue((bool)$res);
  154. $res = $this->Model->find('first', array('conditions' => array('name' => 'multiply')));
  155. //debug($res);
  156. $this->assertTrue(!empty($res));
  157. $this->assertSame(substr($res[$this->Model->alias]['set_rate'], 0, 4), '1.22');
  158. $this->assertSame(substr($res[$this->Model->alias]['rel_rate'], 0, 5), '-0.02');
  159. $this->Model->Behaviors->unload('DecimalInput');
  160. $this->Model->Behaviors->load('Tools.DecimalInput', array('fields' => array('rel_rate', 'set_rate'), 'transform' => array(), 'multiply' => 0.01, 'output' => true));
  161. $res = $this->Model->find('first', array('conditions' => array('name' => 'multiply')));
  162. //debug($res);
  163. $this->assertTrue(!empty($res));
  164. $this->assertSame($res[$this->Model->alias]['set_rate'], '122');
  165. $this->assertSame($res[$this->Model->alias]['rel_rate'], '-2');
  166. }
  167. }