Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save chengkangzai/9eb347d3a0e9dac34912eebd2705706b to your computer and use it in GitHub Desktop.
Save chengkangzai/9eb347d3a0e9dac34912eebd2705706b to your computer and use it in GitHub Desktop.
Parse Pest Test case name to academic Test Case
$tests = collect();
$files = File::allFiles(__DIR__ . '/../../../tests/Feature');
foreach ($files as $file) {
$baseName = $file->getBasename('.php'); //CreatePaymentActionTest
$name = str($baseName)->snake()->replace('_', ' ')->title(); //Create Payment Action Test
$realPath = $file->getPathInfo()->getRealPath(); //Feature
$nameSpace = str($realPath)->explode('/tests/Feature/')->last();
$levels = str($nameSpace)->explode('/');
$level = $levels->count();
$content = file_get_contents($file->getPathname());
$test = $this->getTestName($content);
$tests->push([
'name' => $name->toString(),
'level' => $level,
'levels' => $levels->toArray(),
'real_path' => $realPath,
'test' => $test,
]);
}
$tests = $tests
->groupBy('levels.0')
->map(function (Collection $item) {
return $item
->groupBy('levels.1')
->map(function (Collection $item) {
return $item->groupBy('levels.2');
});
});
private function getTestName(string $content)
{
return str($content)
->explode('it(\'')
->skip(1)
->map(function (string $item) {
$with = str($item)
->contains('})->with([');
$withCount = 0;
if ($with) {
$string = str($item)->explode('})->with([')
->skip(1);
$withCount = substr_count($string, '\n') - 2;
}
$s = str($item)
->explode('\', function')
->first();
$testName = str($s)
->replace('w/o', 'without')
->replace('w/', 'with')
->title()
->toString();
return [
'name' => $testName,
'with' => $with,
'with_count' => $withCount
];
});
}
----
@php
$caseId = 0;
@endphp
@foreach ($tests as $k => $level1)
<h2>{{ $k }}</h2>
@foreach ($level1 as $k => $level2)
<h3>{{ $k }}</h3>
@if ($k == '')
@foreach ($level2 as $l2)
@foreach ($l2 as $k => $t)
<h4> {{ $t['name'] }}</h4>
<table class="table table-bordered table-striped" border="1">
<thead>
<tr>
<td>ID</td>
<td>Test Name</td>
<td>Test Step</td>
<td>Expected Result</td>
</tr>
</thead>
<tbody>
@foreach ($t['test'] as $n)
@if(!$n['with'])
<tr>
<td>{{ ++$caseId }}</td>
<td>It {{ $n['name'] }}</td>
<td></td>
<td>It {{str($n['name'])->replace('Should','')}}</td>
</tr>
@else
@for ($i = 0; $i < $n['with_count']; $i++)
<tr>
<td>{{ ++$caseId }}</td>
<td>It {{ $n['name'] }}</td>
<td></td>
<td>It {{str($n['name'])->replace('Should','')}}</td>
</tr>
@endfor
@endif
@endforeach
</tbody>
</table>
@endforeach
@endforeach
@else
@foreach ($level2 as $k => $level3)
@if($k =='')
<h3>{{ $k }}</h3>
@foreach ($level3 as $t)
<h4>{{ $t['name'] }}</h4>
<table class="table table-bordered table-striped" border="1">
<thead>
<tr>
<td>ID</td>
<td>Test Name</td>
<td>Test Step</td>
<td>Expected Result</td>
</tr>
</thead>
<tbody>
@foreach ($t['test'] as $n)
@if(!$n['with'])
<tr>
<td>{{ ++$caseId }}</td>
<td>It {{ $n['name'] }}</td>
<td></td>
<td>It {{str($n['name'])->replace('Should','')}}</td>
</tr>
@else
@for ($i = 0; $i < $n['with_count']; $i++)
<tr>
<td>{{ ++$caseId }}</td>
<td>It {{ $n['name'] }}</td>
<td></td>
<td>It {{str($n['name'])->replace('Should','')}}</td>
</tr>
@endfor
@endif
@endforeach
</tbody>
</table>
@endforeach
@else
<h3>{{ $k }}</h3>
@foreach ($level3 as $k => $t)
<h4> {{ $t['name'] }}</h4>
<table class="table table-bordered table-striped" border="1">
<thead>
<tr>
<td>ID</td>
<td>Test Name</td>
<td>Test Step</td>
<td>Expected Result</td>
</tr>
</thead>
<tbody>
@foreach ($t['test'] as $n)
@if(!$n['with'])
<tr>
<td>{{ ++$caseId }}</td>
<td>It {{ $n['name'] }}</td>
<td></td>
<td>It {{str($n['name'])->replace('Should','')}}</td>
</tr>
@else
@for ($i = 0; $i < $n['with_count']; $i++)
<tr>
<td>{{ ++$caseId }}</td>
<td>It {{ $n['name'] }}</td>
<td></td>
<td>It {{str($n['name'])->replace('Should','')}}</td>
</tr>
@endfor
@endif
@endforeach
</tbody>
</table>
@endforeach
@endif
@endforeach
@endif
@endforeach
@endforeach
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment