/src/package/Data/Repositories/Support/Suites.php

https://github.com/antonioribeiro/ci · PHP · 186 lines · 102 code · 23 blank · 61 comment · 10 complexity · 59ec80c39e31dc635ec7fa42e037f2f8 MD5 · raw file

  1. <?php
  2. namespace PragmaRX\Tddd\Package\Data\Repositories\Support;
  3. use PragmaRX\Tddd\Package\Data\Models\Project;
  4. use PragmaRX\Tddd\Package\Data\Models\Suite;
  5. use PragmaRX\Tddd\Package\Data\Models\Tester;
  6. use Symfony\Component\Finder\Finder;
  7. trait Suites
  8. {
  9. /**
  10. * Create or update a suite.
  11. *
  12. * @param $name
  13. * @param $project_id
  14. * @param $suite_data
  15. *
  16. * @return Suite|null|bool
  17. */
  18. public function createOrUpdateSuite($name, $project_id, $suite_data)
  19. {
  20. $project_id = $project_id instanceof Project ? $project_id->id : $project_id;
  21. if (is_null($tester = Tester::where('name', $suite_data['tester'])->first())) {
  22. $this->addMessage("Tester {$suite_data['tester']} not found.", 'error');
  23. return false;
  24. }
  25. return Suite::updateOrCreate(
  26. [
  27. 'name' => $name,
  28. 'project_id' => $project_id,
  29. ],
  30. [
  31. 'tester_id' => $tester->id,
  32. 'tests_path' => array_get($suite_data, 'tests_path'),
  33. 'command_options' => array_get($suite_data, 'command_options'),
  34. 'file_mask' => array_get($suite_data, 'file_mask'),
  35. 'retries' => array_get($suite_data, 'retries'),
  36. 'editor' => array_get($suite_data, 'editor'),
  37. 'coverage_enabled' => array_get($suite_data, 'coverage.enabled', false),
  38. 'coverage_index' => array_get($suite_data, 'coverage.index'),
  39. ]
  40. );
  41. }
  42. /**
  43. * Find suite by project and name.
  44. *
  45. * @param $name
  46. * @param $project_id
  47. *
  48. * @return \PragmaRX\Tddd\Package\Data\Models\Suite|null
  49. */
  50. public function findSuiteByNameAndProject($name, $project_id)
  51. {
  52. return Suite::where('name', $name)
  53. ->where('project_id', $project_id)
  54. ->first();
  55. }
  56. /**
  57. * Get all suites.
  58. *
  59. * @return \Illuminate\Database\Eloquent\Collection|static[]
  60. */
  61. public function getSuites()
  62. {
  63. return Suite::all();
  64. }
  65. /**
  66. * Find suite by id.
  67. *
  68. * @return \PragmaRX\Tddd\Package\Data\Models\Suite|null
  69. */
  70. public function findSuiteById($id)
  71. {
  72. return Suite::find($id);
  73. }
  74. /**
  75. * Remove suites that are not in present in config.
  76. *
  77. * @param $suites
  78. * @param $project
  79. */
  80. public function removeMissingSuites($suites, $project)
  81. {
  82. Suite::where('project_id', $project->id)->whereNotIn('name', collect($suites)->keys())->each(function ($suite) {
  83. $suite->delete();
  84. });
  85. }
  86. /**
  87. * Sync all tests for a particular suite.
  88. *
  89. * @param $suite
  90. * @param $exclusions
  91. */
  92. protected function syncSuiteTests($suite, $exclusions, $showTests)
  93. {
  94. $files = $this->getAllFilesFromSuite($suite);
  95. foreach ($files as $file) {
  96. if (!$this->isExcluded($exclusions, null, $file) && $this->isTestable($file->getRealPath())) {
  97. $this->createOrUpdateTest($file, $suite);
  98. if ($showTests) {
  99. $this->addMessage('NEW TEST: '.$file->getRealPath());
  100. }
  101. } else {
  102. // If the test already exists, delete it.
  103. //
  104. if ($test = $this->findTestByNameAndSuite($file, $suite)) {
  105. $test->delete();
  106. }
  107. }
  108. }
  109. foreach ($suite->tests as $test) {
  110. if (!file_exists($path = $test->fullPath)) {
  111. $test->delete();
  112. }
  113. }
  114. }
  115. /**
  116. * Get all files from a suite.
  117. *
  118. * @param $suite
  119. *
  120. * @return array
  121. */
  122. protected function getAllFilesFromSuite($suite)
  123. {
  124. if (!file_exists($suite->testsFullPath)) {
  125. die('FATAL ERROR: directory not found: '.$suite->testsFullPath.'.');
  126. }
  127. $files = Finder::create()->files()->in($suite->testsFullPath);
  128. if ($suite->file_mask) {
  129. $files->name($suite->file_mask);
  130. }
  131. return iterator_to_array($files, false);
  132. }
  133. /**
  134. * Get all suites for a path.
  135. *
  136. * @param $path
  137. *
  138. * @return mixed
  139. */
  140. public function getSuitesForPath($path)
  141. {
  142. $projects = $this->getProjects();
  143. // Reduce the collection of projects by those whose path properties
  144. // (should be only 1) are contained in the fullpath of our
  145. // changed file
  146. $filtered_projects = $projects->filter(function ($project) use ($path) {
  147. return substr_count($path, $project->path) > 0;
  148. });
  149. // Get filtered projects dependencies
  150. $depends = $projects->filter(function ($project) use ($filtered_projects) {
  151. if (!is_null($depends = config("tddd.projects.{$project->name}.depends"))) {
  152. return collect($depends)->filter(function ($item) use ($filtered_projects) {
  153. return !is_null($filtered_projects->where('name', $item)->first());
  154. });
  155. }
  156. return false;
  157. });
  158. // At this point we have (hopefully only 1) project. Now we need
  159. // the suite(s) associated with the project.
  160. return Suite::whereIn('project_id', $filtered_projects->merge($depends)->pluck('id'))
  161. ->get();
  162. }
  163. }