DecimalInputBehaviorTest.php 2.8 KB

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