DecimalInputBehaviorTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. $this->Model = ClassRegistry::init('PaymentMethod');
  9. $this->Model->Behaviors->load('Tools.DecimalInput', array('fields'=>array('rel_rate', 'set_rate'), 'output'=>true));
  10. }
  11. public function tearDown() {
  12. unset($this->Model);
  13. }
  14. public function testObject() {
  15. $this->assertTrue(is_a($this->Model->Behaviors->DecimalInput, 'DecimalInputBehavior'));
  16. }
  17. public function testBasic() {
  18. echo $this->_header(__FUNCTION__);
  19. $data = array(
  20. 'name' => 'some Name',
  21. 'set_rate' => '0,1',
  22. 'rel_rate' => '-0,02',
  23. );
  24. $this->Model->set($data);
  25. $res = $this->Model->validates();
  26. $this->assertTrue($res);
  27. $res = $this->Model->data;
  28. //debug($res);
  29. $this->assertSame($res[$this->Model->alias]['set_rate'], 0.1);
  30. $this->assertSame($res[$this->Model->alias]['rel_rate'], -0.02);
  31. }
  32. public function testValidates() {
  33. //echo $this->_header(__FUNCTION__);
  34. $data = array(
  35. 'name' => 'some Name',
  36. 'set_rate' => '0,1',
  37. 'rel_rate' => '-0,02',
  38. );
  39. $this->Model->set($data);
  40. $res = $this->Model->validates();
  41. $this->assertTrue($res);
  42. $res = $this->Model->data;
  43. //debug($res);
  44. $this->assertSame($res[$this->Model->alias]['set_rate'], 0.1);
  45. $this->assertSame($res[$this->Model->alias]['rel_rate'], -0.02);
  46. }
  47. public function testFind() {
  48. //echo $this->_header(__FUNCTION__);
  49. $data = array(
  50. 'name' => 'some Name',
  51. 'set_rate' => '0,1',
  52. 'rel_rate' => '-0,02',
  53. );
  54. $this->Model->create();
  55. $res = $this->Model->save($data);
  56. $this->assertTrue((bool)$res);
  57. # find all
  58. $res = $this->Model->find('all', array('order' => array('created' => 'DESC')));
  59. $this->assertTrue(!empty($res));
  60. $this->assertSame(substr($res[0][$this->Model->alias]['set_rate'], 0, 4), '0,10');
  61. $this->assertSame(substr($res[0][$this->Model->alias]['rel_rate'], 0, 5), '-0,02');
  62. # find first
  63. $res = $this->Model->find('first', array('order' => array('created' => 'DESC')));
  64. $this->assertTrue(!empty($res));
  65. $this->assertSame($res[$this->Model->alias]['set_rate'], '0,10');
  66. $this->assertSame($res[$this->Model->alias]['rel_rate'], '-0,0200');
  67. $res = $this->Model->find('count', array());
  68. $this->assertSame($res, 8);
  69. }
  70. public function testStrict() {
  71. $this->Model->Behaviors->unload('DecimalInput');
  72. $this->Model->Behaviors->load('Tools.DecimalInput', array('fields'=>array('rel_rate', 'set_rate'), 'strict'=>true));
  73. $data = array(
  74. 'name' => 'some Name',
  75. 'set_rate' => '0.1',
  76. 'rel_rate' => '-0,02',
  77. );
  78. $this->Model->set($data);
  79. $res = $this->Model->validates();
  80. $this->assertTrue($res);
  81. $res = $this->Model->data;
  82. //debug($res);
  83. $this->assertSame($res[$this->Model->alias]['set_rate'], '0#1');
  84. $this->assertSame($res[$this->Model->alias]['rel_rate'], -0.02);
  85. }
  86. public function testBeforeSave() {
  87. $this->Model->Behaviors->unload('DecimalInput');
  88. $this->Model->Behaviors->load('Tools.DecimalInput', array('fields'=>array('rel_rate', 'set_rate'), 'before'=>'save', 'output'=>false));
  89. $data = array(
  90. 'name' => 'some Name',
  91. 'set_rate' => '2,11',
  92. 'rel_rate' => '-1,22',
  93. );
  94. $this->Model->create();
  95. $res = $this->Model->save($data);
  96. $this->assertTrue((bool)$res);
  97. $res = $this->Model->find('first', array('order' => array('created' => 'DESC')));
  98. $this->assertTrue(!empty($res));
  99. $this->assertSame(substr($res[$this->Model->alias]['set_rate'], 0, 4), '2.11');
  100. $this->assertSame(substr($res[$this->Model->alias]['rel_rate'], 0, 5), '-1.22');
  101. }
  102. public function testLocaleConv() {
  103. $res = setlocale(LC_NUMERIC, 'de_DE.utf8', 'german');
  104. $this->assertTrue(!empty($res));
  105. $this->Model->Behaviors->unload('DecimalInput');
  106. $this->Model->Behaviors->load('Tools.DecimalInput', array('fields'=>array('rel_rate', 'set_rate'), 'localeconv'=>true, 'output'=>true));
  107. $data = array(
  108. 'name' => 'german',
  109. 'set_rate' => '3,11',
  110. 'rel_rate' => '-4,22',
  111. );
  112. $this->Model->create();
  113. $res = $this->Model->save($data);
  114. $this->assertTrue((bool)$res);
  115. $res = $this->Model->find('first', array('conditions' => array('name' => 'german')));
  116. $this->assertTrue(!empty($res));
  117. $this->assertSame(substr($res[$this->Model->alias]['set_rate'], 0, 4), '3,11');
  118. $this->assertSame(substr($res[$this->Model->alias]['rel_rate'], 0, 5), '-4,22');
  119. $res = setlocale(LC_NUMERIC, 'en_US.utf8', 'english');
  120. $this->assertTrue(!empty($res));
  121. $this->Model->Behaviors->unload('DecimalInput');
  122. $this->Model->Behaviors->load('Tools.DecimalInput', array('fields'=>array('rel_rate', 'set_rate'), 'localeconv'=>true, 'output'=>true));
  123. $data = array(
  124. 'name' => 'english',
  125. 'set_rate' => '3.21',
  126. 'rel_rate' => '-4.32',
  127. );
  128. $this->Model->create();
  129. $res = $this->Model->save($data);
  130. $this->assertTrue((bool)$res);
  131. $res = $this->Model->find('first', array('conditions' => array('name' => 'english')));
  132. //debug($res);
  133. $this->assertTrue(!empty($res));
  134. $this->assertSame(substr($res[$this->Model->alias]['set_rate'], 0, 4), '3.21');
  135. $this->assertSame(substr($res[$this->Model->alias]['rel_rate'], 0, 5), '-4.32');
  136. }
  137. public function testMultiply() {
  138. $this->Model->Behaviors->unload('DecimalInput');
  139. $this->Model->Behaviors->load('Tools.DecimalInput', array('fields'=>array('rel_rate', 'set_rate'), 'transform'=>array(), 'multiply'=>0.01, 'output'=>false));
  140. $data = array(
  141. 'name' => 'multiply',
  142. 'set_rate' => '122',
  143. 'rel_rate' => '-2',
  144. );
  145. $this->Model->create();
  146. $res = $this->Model->save($data);
  147. $this->assertTrue((bool)$res);
  148. $res = $this->Model->find('first', array('conditions' => array('name' => 'multiply')));
  149. //debug($res);
  150. $this->assertTrue(!empty($res));
  151. $this->assertSame(substr($res[$this->Model->alias]['set_rate'], 0, 4), '1.22');
  152. $this->assertSame(substr($res[$this->Model->alias]['rel_rate'], 0, 5), '-0.02');
  153. $this->Model->Behaviors->unload('DecimalInput');
  154. $this->Model->Behaviors->load('Tools.DecimalInput', array('fields'=>array('rel_rate', 'set_rate'), 'transform'=>array(), 'multiply'=>0.01, 'output'=>true));
  155. $res = $this->Model->find('first', array('conditions' => array('name' => 'multiply')));
  156. //debug($res);
  157. $this->assertTrue(!empty($res));
  158. $this->assertSame($res[$this->Model->alias]['set_rate'], '122');
  159. $this->assertSame($res[$this->Model->alias]['rel_rate'], '-2');
  160. }
  161. }