/demos/time-tracker/tests/unit/TimeEntryDaoTestCase.php

https://bitbucket.org/volatileeight/prado · PHP · 213 lines · 167 code · 46 blank · 0 comment · 0 complexity · ccf0933c5558a1bdf1a6cd941e75d8ca MD5 · raw file

  1. <?php
  2. require_once(dirname(__FILE__).'/BaseTestCase.php');
  3. class TimeEntryDaoTestCase extends BaseTestCase
  4. {
  5. protected $entryDao;
  6. protected $projectDao;
  7. protected $userDao;
  8. protected $categoryDao;
  9. protected $reportDao;
  10. function setup()
  11. {
  12. parent::setup();
  13. $app = Prado::getApplication();
  14. $this->entryDao = $app->getModule('daos')->getDao('TimeEntryDao');
  15. $this->projectDao = $app->getModule('daos')->getDao('ProjectDao');
  16. $this->userDao = $app->getModule('daos')->getDao('UserDao');
  17. $this->categoryDao = $app->getModule('daos')->getDao('CategoryDao');
  18. $this->reportDao = $app->getModule('daos')->getDao('ReportDao');
  19. $this->flushDatabase();
  20. }
  21. function createNewProject()
  22. {
  23. $project = new ProjectRecord;
  24. $project->CreatorUserName = "admin";
  25. $project->DateCreated = time();
  26. $project->CompletionDate = strtotime('+1 month');
  27. $project->Description = 'Test project 1';
  28. $project->EstimateDuration = 100.5;
  29. $project->ManagerUserName = 'manager';
  30. $project->Name = 'Project 1';
  31. return $project;
  32. }
  33. function createNewProject2()
  34. {
  35. $project = new ProjectRecord;
  36. $project->CreatorUserName = "manager";
  37. $project->DateCreated = time();
  38. $project->CompletionDate = strtotime('+1 week');
  39. $project->Description = 'Test project 2';
  40. $project->EstimateDuration = 30.5;
  41. $project->ManagerUserName = 'manager';
  42. $project->Name = 'Project 2';
  43. return $project;
  44. }
  45. function createNewCategory()
  46. {
  47. $category = new CategoryRecord;
  48. $category->Name = 'Category 1';
  49. $category->EstimateDuration = 5.5;
  50. $category->Abbreviation = 'CAT 1';
  51. return $category;
  52. }
  53. function createNewCategory2()
  54. {
  55. $category = new CategoryRecord;
  56. $category->Name = 'Category 2';
  57. $category->EstimateDuration = 1.5;
  58. $category->Abbreviation = 'CAT2';
  59. return $category;
  60. }
  61. function createNewCategory3()
  62. {
  63. $category = new CategoryRecord;
  64. $category->Name = 'Category 3';
  65. $category->EstimateDuration = 2.5;
  66. $category->Abbreviation = 'CAT3';
  67. return $category;
  68. }
  69. function createProjectsAndCategories()
  70. {
  71. $project1 = $this->createNewProject();
  72. $this->projectDao->addNewProject($project1);
  73. $project2 = $this->createNewProject2();
  74. $this->projectDao->addNewProject($project2);
  75. $category1 = $this->createNewCategory();
  76. $category1->ProjectID = $project1->ID;
  77. $category2 = $this->createNewCategory2();
  78. $category2->ProjectID = $project2->ID;
  79. $category3 = $this->createNewCategory3();
  80. $category3->ProjectID = $project1->ID;
  81. $this->categoryDao->addNewCategory($category1);
  82. $this->categoryDao->addNewCategory($category2);
  83. $this->categoryDao->addNewCategory($category3);
  84. return array($project1, $project2, $category1, $category2, $category3);
  85. }
  86. function assertSameEntry($entry1, $entry2)
  87. {
  88. $this->assertEqual($entry1->CreatorUserName, $entry2->CreatorUserName);
  89. $this->assertEqual($entry1->Description, $entry2->Description);
  90. $this->assertEqual($entry1->Duration, $entry2->Duration);
  91. $this->assertEqual($entry1->ID, $entry2->ID);
  92. $this->assertEqual($entry1->ReportDate, $entry2->ReportDate);
  93. $this->assertEqual($entry1->Username, $entry2->Username);
  94. }
  95. function createTimeEntry1()
  96. {
  97. $added = $this->createProjectsAndCategories();
  98. $entry = new TimeEntryRecord;
  99. $entry->CreatorUserName = "admin";
  100. $entry->Category = $added[2];
  101. $entry->Description = "New work";
  102. $entry->Duration = 1.5;
  103. $entry->Project = $added[0];
  104. $entry->ReportDate = strtotime('-1 day');
  105. $entry->Username = 'consultant';
  106. return array($entry, $added);
  107. }
  108. function createTimeEntries2()
  109. {
  110. $added = $this->createProjectsAndCategories();
  111. $entry = new TimeEntryRecord;
  112. $entry->CreatorUserName = "admin";
  113. $entry->Category = $added[2];
  114. $entry->Description = "New work";
  115. $entry->Duration = 1.2;
  116. $entry->Project = $added[0];
  117. $entry->ReportDate = strtotime('-10 day');
  118. $entry->Username = 'consultant';
  119. $entry2 = new TimeEntryRecord;
  120. $entry2->CreatorUserName = "admin";
  121. $entry2->Category = $added[4];
  122. $entry2->Description = "New work 2";
  123. $entry2->Duration = 5.5;
  124. $entry2->Project = $added[0];
  125. $entry2->ReportDate = strtotime('-4 day');
  126. $entry2->Username = 'consultant';
  127. return array($entry, $entry2, $added);
  128. }
  129. function testCreateNewTimeEntry()
  130. {
  131. $added = $this->createTimeEntry1();
  132. $entry = $added[0];
  133. $this->entryDao->addNewTimeEntry($entry);
  134. $check = $this->entryDao->getTimeEntryByID(1);
  135. $this->assertSameEntry($entry, $check);
  136. }
  137. function testDeleteTimeEntry()
  138. {
  139. $this->testCreateNewTimeEntry();
  140. $this->entryDao->deleteTimeEntry(1);
  141. $check = $this->entryDao->getTimeEntryByID(1);
  142. $this->assertNull($check);
  143. }
  144. function testGetEntriesInProject()
  145. {
  146. $added = $this->createTimeEntries2();
  147. $this->entryDao->addNewTimeEntry($added[0]);
  148. $this->entryDao->addNewTimeEntry($added[1]);
  149. $list = $this->entryDao->getTimeEntriesInProject('consultant', 1);
  150. $this->assertEqual(count($list), 2);
  151. $this->assertSameEntry($list[0], $added[0]);
  152. $this->assertSameEntry($list[1], $added[1]);
  153. }
  154. function testUpdateEntry()
  155. {
  156. $added = $this->createTimeEntry1();
  157. $entry = $added[0];
  158. $this->entryDao->addNewTimeEntry($entry);
  159. $check = $this->entryDao->getTimeEntryByID(1);
  160. $this->assertSameEntry($entry, $check);
  161. $entry->Description = "asdasd";
  162. $entry->Duration = 200;
  163. $this->entryDao->updateTimeEntry($entry);
  164. $verify = $this->entryDao->getTimeEntryByID(1);
  165. $this->assertSameEntry($entry, $verify);
  166. }
  167. }