This php paste was created by melkorm on 2012-01-20 05:12:16.
  1. <?php
  2.  
  3. namespace app\tests\cases\models;
  4.  
  5. use app\models\Questions;
  6. use lithium\core\Environment;
  7. class QuestionsTest extends \lithium\test\Unit {
  8.  
  9. /**
  10. * @var array
  11. */
  12. private $_correctQuestionData = array(
  13. 'title' => 'My titttle',
  14. 'content' => 'some questions content ...',
  15. 'views' => 12,
  16. 'score' => 456,
  17. 'answers' => 45,
  18. 'status' => 1,
  19. 'edited' => 1321920000,
  20. );
  21.  
  22. /**
  23. * @var array
  24. */
  25. private $_incorrectQuestionData = array(
  26. 'someDataWhichSHouldntPass' => 'I shouldnt ever pass'
  27. );
  28.  
  29. public function setUp() {
  30. Questions::remove();
  31. }
  32.  
  33. public function tearDown() {
  34. Questions::remove();
  35. }
  36.  
  37. public function test_Save_With_Cortrect_Data()
  38. {
  39. $question = Questions::create($this->_correctQuestionData);
  40. $result = $question->save();
  41. $this->assertTrue($result);
  42. }
  43.  
  44. public function test_Save_With_Incorrect_Data()
  45. {
  46. $this->expectException(true);
  47. $question = Questions::create();
  48. $result = $question->save($this->_incorrectQuestionData);
  49. }
  50.  
  51. /**
  52. * @param array $data
  53. * @return \lithium\data\entity\Document
  54. */
  55. private function _createNewQuestion( Array $data )
  56. {
  57. $question = Questions::create($data);
  58. $question->save();
  59. return $question;
  60. }
  61.  
  62. public function test_Edit_With_Correct_Data()
  63. {
  64. $questionDocument = $this->_createNewQuestion($this->_correctQuestionData);
  65. $expectedData = array(
  66. '_id' => $questionDocument->_id->__toString(),
  67. 'title' => 'My edited titttle',
  68. 'content' => 'nah no content at all',
  69. 'views' => 0,
  70. 'score' => 1,
  71. 'answers' => 4,
  72. 'status' => 0,
  73. 'edited' => strtotime('2012-11-22'),
  74. );
  75. $questionDocument->save($expectedData);
  76. $this->assertEqual($expectedData, $questionDocument->to('array'));
  77. }
  78.  
  79. public function test_Edit_With_InCorrect_Data()
  80. {
  81. $questionDocument = $this->_createNewQuestion($this->_correctQuestionData);
  82. $questionDocument->save($this->_incorrectQuestionData);
  83. $this->assertEqual($this->_correctQuestionData, $questionDocument->to('array'));
  84. }
  85.  
  86.  
  87. }
  88.  
  89. ?>
  90.  
  91. namespace app\models;
  92. use app\extensions\data\Model;
  93. class Questions extends Model
  94. {
  95. protected $_schema = array(
  96. 'title' => array('type' => 'string', 'null' => false),
  97. 'content' => array('type' => 'string', 'null' => false),
  98. 'views' => array('type' => 'integer', 'null' => false),
  99. 'score' => array('type' => 'integer', 'null' => false),
  100. 'answers' => array('type' => 'integer', 'null' => false),
  101. 'status' => array('type' => 'integer', 'null' => false),
  102. 'edited' => array('type' => 'timestamp', 'null' => false),
  103. );
  104.  
  105. protected $_meta = array(
  106. 'locked' => true
  107. );
  108.  
  109. }