Skip to content
This repository was archived by the owner on Apr 16, 2024. It is now read-only.

Commit 158a095

Browse files
committed
Generate Feature tests
1 parent c0e66d5 commit 158a095

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

src/Finder.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,19 @@ public function findFeaturePath($service, $feature)
205205
return $this->findFeaturesRootPath($service)."/$feature.php";
206206
}
207207

208+
/**
209+
* Find the test file path for the given feature.
210+
*
211+
* @param string $service
212+
* @param string $feature
213+
*
214+
* @return string
215+
*/
216+
public function findFeatureTestPath($service, $test)
217+
{
218+
return $this->findServicePath($service)."/Tests/Features/$test.php";
219+
}
220+
208221
/**
209222
* Find the namespace for features in the given service.
210223
*
@@ -217,6 +230,18 @@ public function findFeatureNamespace($service)
217230
return $this->findServiceNamespace($service).'\\Features';
218231
}
219232

233+
/**
234+
* Find the namespace for features tests in the given service.
235+
*
236+
* @param string $service
237+
*
238+
* @return string
239+
*/
240+
public function findFeatureTestNamespace($service)
241+
{
242+
return $this->findServiceNamespace($service).'\\Tests\\Features';
243+
}
244+
220245
/**
221246
* Find the root path of domains.
222247
*

src/Generators/FeatureGenerator.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ public function generate($feature, $service, array $jobs = [])
5757

5858
$this->createFile($path, $content);
5959

60+
// generate test file
61+
$this->generateTestFile($feature, $service);
62+
6063
return new Feature(
6164
$feature,
6265
basename($path),
@@ -67,6 +70,24 @@ public function generate($feature, $service, array $jobs = [])
6770
);
6871
}
6972

73+
private function generateTestFile($feature, $service)
74+
{
75+
$content = file_get_contents($this->getTestStub());
76+
77+
$namespace = $this->findFeatureTestNamespace($service);
78+
$testClass = $feature.'Test';
79+
80+
$content = str_replace(
81+
['{{namespace}}', '{{testclass}}', '{{feature}}'],
82+
[$namespace, $testClass, mb_strtolower($feature)],
83+
$content
84+
);
85+
86+
$path = $this->findFeatureTestPath($service, $testClass);
87+
88+
$this->createFile($path, $content);
89+
}
90+
7091
/**
7192
* Get the stub file for the generator.
7293
*
@@ -76,4 +97,14 @@ protected function getStub()
7697
{
7798
return __DIR__.'/stubs/feature.stub';
7899
}
100+
101+
/**
102+
* Get the test stub file for the generator.
103+
*
104+
* @return string
105+
*/
106+
private function getTestStub()
107+
{
108+
return __DIR__.'/stubs/feature-test.stub';
109+
}
79110
}

src/Generators/ServiceGenerator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class ServiceGenerator extends Generator
3939
'resources/',
4040
'resources/lang/',
4141
'resources/views/',
42+
'Tests/',
43+
'Tests/Features/',
4244
];
4345

4446

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
namespace {{namespace}}
3+
4+
use TestCase;
5+
6+
class {{testclass}} extends TestCase
7+
{
8+
public function test_{{feature}}()
9+
{
10+
$this->markTestIncomplete();
11+
}
12+
}

0 commit comments

Comments
 (0)