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

Commit 2acc6f4

Browse files
committed
generate Job test files when generating jobs
1 parent 158a095 commit 2acc6f4

File tree

4 files changed

+130
-40
lines changed

4 files changed

+130
-40
lines changed

src/Finder.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,31 @@ public function findDomainJobsNamespace($domain)
374374
return $this->findDomainNamespace($domain).'\Jobs';
375375
}
376376

377+
/**
378+
* Find the namespace for the given domain's Jobs.
379+
*
380+
* @param string $domain
381+
*
382+
* @return string
383+
*/
384+
public function findDomainJobsTestsNamespace($domain)
385+
{
386+
return $this->findDomainNamespace($domain).'\Tests\Jobs';
387+
}
388+
389+
/**
390+
* Find the test path for the given job.
391+
*
392+
* @param string $domain
393+
* @param string $job
394+
*
395+
* @return string
396+
*/
397+
public function findJobTestPath($domain, $jobTest)
398+
{
399+
return $this->findDomainPath($domain).DIRECTORY_SEPARATOR.'Tests'.DIRECTORY_SEPARATOR.'Jobs'.DIRECTORY_SEPARATOR.$jobTest.'.php';
400+
}
401+
377402
/**
378403
* Find the path for the give controller class.
379404
*

src/Generators/FeatureGenerator.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ public function generate($feature, $service, array $jobs = [])
7070
);
7171
}
7272

73+
/**
74+
* Generate the test file.
75+
*
76+
* @param string $feature
77+
* @param string $service
78+
*/
7379
private function generateTestFile($feature, $service)
7480
{
7581
$content = file_get_contents($this->getTestStub());

src/Generators/JobGenerator.php

Lines changed: 87 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,39 @@
1616
use Lucid\Console\Components\Job;
1717

1818
/**
19-
* @author Abed Halawi <[email protected]>
20-
*/
21-
class JobGenerator extends Generator
22-
{
23-
public function generate($job, $domain)
24-
{
25-
$job = Str::job($job);
26-
$domain = Str::domain($domain);
27-
$path = $this->findJobPath($domain, $job);
28-
29-
if ($this->exists($path)) {
30-
throw new Exception('Job already exists');
31-
32-
return false;
33-
}
34-
35-
// Make sure the domain directory exists
36-
$this->createDirectory($this->findDomainPath($domain).'/Jobs');
37-
38-
// Create the job
39-
$namespace = $this->findDomainJobsNamespace($domain);
40-
41-
$content = file_get_contents($this->getStub());
42-
$content = str_replace(
43-
['{{job}}', '{{namespace}}', '{{foundation_namespace}}'],
44-
[$job, $namespace, $this->findFoundationNamespace()],
45-
$content
46-
);
19+
* @author Abed Halawi <[email protected]>
20+
*/
21+
class JobGenerator extends Generator
22+
{
23+
public function generate($job, $domain)
24+
{
25+
$job = Str::job($job);
26+
$domain = Str::domain($domain);
27+
$path = $this->findJobPath($domain, $job);
28+
29+
if ($this->exists($path)) {
30+
throw new Exception('Job already exists');
31+
32+
return false;
33+
}
34+
35+
// Make sure the domain directory exists
36+
$this->createDomainDirectory($domain);
37+
38+
// Create the job
39+
$namespace = $this->findDomainJobsNamespace($domain);
40+
41+
$content = file_get_contents($this->getStub());
42+
$content = str_replace(
43+
['{{job}}', '{{namespace}}', '{{foundation_namespace}}'],
44+
[$job, $namespace, $this->findFoundationNamespace()],
45+
$content
46+
);
4747

4848
$this->createFile($path, $content);
4949

50+
$this->generateTestFile($job, $domain);
51+
5052
return new Job(
5153
$job,
5254
$namespace,
@@ -56,15 +58,60 @@ public function generate($job, $domain)
5658
$this->findDomain($domain),
5759
$content
5860
);
59-
}
60-
61-
/**
62-
* Get the stub file for the generator.
63-
*
64-
* @return string
65-
*/
66-
public function getStub()
67-
{
68-
return __DIR__.'/stubs/job.stub';
69-
}
70-
}
61+
}
62+
63+
/**
64+
* Generate test file.
65+
*
66+
* @param string $job
67+
* @param string $domain
68+
*/
69+
private function generateTestFile($job, $domain)
70+
{
71+
$content = file_get_contents($this->getTestStub());
72+
73+
$namespace = $this->findDomainJobsTestsNamespace($domain);
74+
$testClass = $job.'Test';
75+
76+
$content = str_replace(
77+
['{{namespace}}', '{{testclass}}', '{{job}}'],
78+
[$namespace, $testClass, mb_strtolower($job)],
79+
$content
80+
);
81+
82+
$path = $this->findJobTestPath($domain, $testClass);
83+
84+
$this->createFile($path, $content);
85+
}
86+
87+
/**
88+
* Create domain directory.
89+
*
90+
* @param string $domain
91+
*/
92+
private function createDomainDirectory($domain)
93+
{
94+
$this->createDirectory($this->findDomainPath($domain).'/Jobs');
95+
$this->createDirectory($this->findDomainPath($domain).'/Tests/Jobs');
96+
}
97+
98+
/**
99+
* Get the stub file for the generator.
100+
*
101+
* @return string
102+
*/
103+
public function getStub()
104+
{
105+
return __DIR__.'/stubs/job.stub';
106+
}
107+
108+
/**
109+
* Get the test stub file for the generator.
110+
*
111+
* @return string
112+
*/
113+
public function getTestStub()
114+
{
115+
return __DIR__.'/stubs/job-test.stub';
116+
}
117+
}

src/Generators/stubs/job-test.stub

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_{{job}}()
9+
{
10+
$this->markTestIncomplete();
11+
}
12+
}

0 commit comments

Comments
 (0)