Skip to content

Commit

Permalink
New Feature: Generate Job Classes
Browse files Browse the repository at this point in the history
- Added Job Make Command
- Attempt to generate tests: - Generation is working
- TODO: Generated tests cannot be seen by pest. Come up with a concrete setup for tests in the modules
  • Loading branch information
coolsam726 committed Apr 9, 2024
1 parent d0baef6 commit 49f9ba5
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 2 deletions.
3 changes: 2 additions & 1 deletion config/modular.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
'resources/css',
'routes',
'storage',
'tests',
'tests/Feature',
'tests/Unit',
],
];
19 changes: 19 additions & 0 deletions src/Commands/JobMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Savannabits\Modular\Commands;

use Savannabits\Modular\Support\Concerns\GeneratesModularFiles;

class JobMakeCommand extends \Illuminate\Foundation\Console\JobMakeCommand
{
use GeneratesModularFiles;

protected $name = 'modular:make-job';

protected $description = 'Create a new job class in a modular package';

protected function getRelativeNamespace(): string
{
return 'Jobs';
}
}
25 changes: 24 additions & 1 deletion src/Commands/ModuleMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private function generateModuleFiles(): void
} catch (FileNotFoundException|NotFoundExceptionInterface|ContainerExceptionInterface $e) {
$this->error($e->getMessage());
}
// $this->generateModuleFacade();
$this->generatePestFiles();
}

private function generateModuleComposerFile(): void
Expand Down Expand Up @@ -134,6 +134,29 @@ private function generateModuleServiceProvider(): void
]);
}

private function generatePestFiles(): void
{
// phpunit.xml
$path = Modular::module($this->moduleName)->path('phpunit.xml');
$this->copyStubToApp('phpunit', $path, [
'moduleName' => $this->moduleStudlyName,
]);

// Pest.php
$path = Modular::module($this->moduleName)->testsPath('Pest.php');
$this->copyStubToApp('pest.class', $path, [
'namespace' => $this->moduleNamespace,
]);

// TestCase.php
$path = Modular::module($this->moduleName)->testsPath('TestCase.php');
$this->copyStubToApp('test-case', $path, [
'namespace' => $this->moduleNamespace.'\\Tests',
]);
}



private function installModule(): void
{
$this->comment('Activating the new Module');
Expand Down
37 changes: 37 additions & 0 deletions src/Commands/TestMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Savannabits\Modular\Commands;

use Illuminate\Support\Str;
use Savannabits\Modular\Support\Concerns\GeneratesModularFiles;

class TestMakeCommand extends \Illuminate\Foundation\Console\TestMakeCommand
{
use GeneratesModularFiles;

protected $name = 'modular:make-test';

protected $description = 'Create a new test class in a modular package';

protected function getPath($name): string
{
$name = Str::replaceFirst($this->rootNamespace(), '', $name);

return $this->getModule()->testsPath(str_replace('\\', '/', $name).'.php');
}

/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace): string
{
if ($this->option('unit')) {
return $rootNamespace.'\Unit';
} else {
return $rootNamespace.'\Feature';
}
}
}
10 changes: 10 additions & 0 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,14 @@ public function resourcePath(string $string, bool $relative = false): string
{
return $this->path('resources'.DIRECTORY_SEPARATOR.trim($string, DIRECTORY_SEPARATOR), $relative);
}

public function testsPath(string $string, bool $relative = false): string
{
return $this->path('tests'.DIRECTORY_SEPARATOR.trim($string, DIRECTORY_SEPARATOR), $relative);
}

public function getBaseProviderClass(): string
{
return $this->makeNamespace('\\'.$this->studlyName.'ServiceProvider');
}
}
48 changes: 48 additions & 0 deletions stubs/pest.class.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
|
| The closure you provide to your test functions is always bound to a specific PHPUnit test
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
| need to change it using the "uses()" function to bind a different classes or traits.
|
*/

uses(
{{ namespace }}\Tests\TestCase::class,
// Illuminate\Foundation\Testing\RefreshDatabase::class,
)->in(__DIR__);

/*
|--------------------------------------------------------------------------
| Expectations
|--------------------------------------------------------------------------
|
| When you're writing tests, you often need to check that values meet certain conditions. The
| "expect()" function gives you access to a set of "expectations" methods that you can use
| to assert different things. Of course, you may extend the Expectation API at any time.
|
*/

expect()->extend('toBeOne', function () {
return $this->toBe(1);
});

/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
|
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
| project that you don't want to repeat in every file. Here you can also expose helpers as
| global functions to help you to reduce the number of lines of code in your test files.
|
*/

function something()
{
// ..
}
33 changes: 33 additions & 0 deletions stubs/phpunit.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Unit">
<directory>tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory>tests/Feature</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>app</directory>
</include>
</source>
<php>
<env name="APP_ENV" value="testing"/>
<env name="APP_MAINTENANCE_DRIVER" value="file"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_STORE" value="array"/>
<!-- <env name="DB_CONNECTION" value="sqlite"/> -->
<!-- <env name="DB_DATABASE" value=":memory:"/> -->
<env name="MAIL_MAILER" value="array"/>
<env name="PULSE_ENABLED" value="false"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="TELESCOPE_ENABLED" value="false"/>
</php>
</phpunit>
10 changes: 10 additions & 0 deletions stubs/test-case.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace {{ namespace }};

use Orchestra\Testbench\TestCase as Orchestra;

abstract class TestCase extends Orchestra
{
//
}

0 comments on commit 49f9ba5

Please sign in to comment.