DecimalInputBehaviorTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. App::uses('DecimalInputBehavior', 'Tools.Model/Behavior');
  3. App::uses('MyCakeTestCase', 'Tools.Lib');
  4. class DecimalInputBehaviorTest extends MyCakeTestCase {
  5. public function startTest() {
  6. //$this->Comment = ClassRegistry::init('Comment');
  7. $this->Comment = new DecimalInputTestModel();
  8. $this->Comment->Behaviors->attach('Tools.DecimalInput', array('fields'=>array('rel_rate', 'set_rate'), 'output'=>true));
  9. }
  10. public function setUp() {
  11. }
  12. public function tearDown() {
  13. }
  14. public function testObject() {
  15. $this->assertTrue(is_a($this->Comment->Behaviors->DecimalInput, 'DecimalInputBehavior'));
  16. }
  17. public function testBasic() {
  18. echo $this->_header(__FUNCTION__);
  19. // accuracy >= 5
  20. $data = array(
  21. 'name' => 'some Name',
  22. 'set_rate' => '0,1',
  23. 'rel_rate' => '-0,02',
  24. );
  25. $this->Comment->set($data);
  26. $res = $this->Comment->validates();
  27. $this->assertTrue($res);
  28. $res = $this->Comment->data;
  29. echo returns($res);
  30. $this->assertSame($res['TestModel']['set_rate'], 0.1);
  31. $this->assertSame($res['TestModel']['rel_rate'], -0.02);
  32. }
  33. public function testValidates() {
  34. echo $this->_header(__FUNCTION__);
  35. // accuracy >= 5
  36. $data = array(
  37. 'name' => 'some Name',
  38. 'set_rate' => '0,1',
  39. 'rel_rate' => '-0,02',
  40. );
  41. $this->Comment->set($data);
  42. $res = $this->Comment->validates();
  43. $this->assertTrue($res);
  44. $res = $this->Comment->data;
  45. echo returns($res);
  46. $this->assertSame($res['TestModel']['set_rate'], 0.1);
  47. $this->assertSame($res['TestModel']['rel_rate'], -0.02);
  48. }
  49. public function testFind() {
  50. echo $this->_header(__FUNCTION__);
  51. $res = $this->Comment->find('all', array());
  52. $this->assertTrue(!empty($res));
  53. echo returns($res);
  54. $this->assertSame($res[0]['TestModel']['set_rate'], '0,1');
  55. $this->assertSame($res[0]['TestModel']['rel_rate'], '-0,02');
  56. echo BR.BR;
  57. $res = $this->Comment->find('first', array());
  58. $this->assertTrue(!empty($res));
  59. echo returns($res);
  60. $this->assertSame($res['TestModel']['set_rate'], '0,1');
  61. $this->assertSame($res['TestModel']['rel_rate'], '-0,02');
  62. $res = $this->Comment->find('count', array());
  63. echo returns($res);
  64. $this->assertSame($res[0][0]['count'], 2);
  65. }
  66. public function testStrict() {
  67. $this->Comment->Behaviors->detach('DecimalInput');
  68. $this->Comment->Behaviors->attach('Tools.DecimalInput', array('fields'=>array('rel_rate', 'set_rate'), 'strict'=>true));
  69. $data = array(
  70. 'name' => 'some Name',
  71. 'set_rate' => '0.1',
  72. 'rel_rate' => '-0,02',
  73. );
  74. $this->Comment->set($data);
  75. $res = $this->Comment->validates();
  76. $this->assertTrue($res);
  77. $res = $this->Comment->data;
  78. echo returns($res);
  79. $this->assertSame($res['TestModel']['set_rate'], '0#1');
  80. $this->assertSame($res['TestModel']['rel_rate'], -0.02);
  81. }
  82. }
  83. /** other files **/
  84. class DecimalInputTestModel extends CakeTestModel {
  85. public $alias = 'TestModel';
  86. public $useTable = false;
  87. public $displayField = 'title';
  88. public function find($type = null, $options = array(), $customData = null) {
  89. $data = array(
  90. 'name' => 'some Name',
  91. 'set_rate' => 0.1,
  92. 'rel_rate' => -0.02,
  93. );
  94. if ($customData !== null) {
  95. $data = $customData;
  96. }
  97. if ($type == 'count') {
  98. $results = array(0=>array(0=>array('count'=>2)));
  99. } else {
  100. $results = array(0=>array($this->alias=>$data));
  101. }
  102. $results = $this->_filterResults($results);
  103. if ($type == 'first') {
  104. $results = $this->_findFirst('after', null, $results);
  105. }
  106. return $results;
  107. }
  108. }