Skip to content

Commit

Permalink
Merge branch '8.x'
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

# Conflicts:
#	composer.json
  • Loading branch information
crynobone committed Aug 21, 2023
2 parents bede67c + baf2942 commit 7f99fe4
Show file tree
Hide file tree
Showing 17 changed files with 149 additions and 53 deletions.
1 change: 0 additions & 1 deletion .coveralls.yml

This file was deleted.

3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
# Ignore following folder/file.
/.github export-ignore
/tests export-ignore
/.coveralls.yml export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.phpunit.cache export-ignore
/canvas.yaml export-ignore
/phpstan-baseline.neon export-ignore
/phpstan.neon.dist export-ignore
/phpunit.xml export-ignore
/pint.json export-ignore
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ on:
push:
pull_request:
workflow_dispatch:
schedule:
- cron: '0 0 * * 4'

jobs:
tests:
Expand Down
16 changes: 10 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,19 @@
}
},
"scripts": {
"post-autoload-dump": "@composer run prepare",
"prepare": "@php ./vendor/bin/testbench package:discover --ansi",
"ci": [
"@composer audit",
"@composer run prepare",
"post-autoload-dump": "@prepare",
"prepare": "@php vendor/bin/testbench package:discover --ansi",
"lint": [
"@php vendor/bin/phpstan analyse",
"@php vendor/bin/pint"
],
"test": "@php vendor/bin/phpunit -c ./ --color"
"test": "@php vendor/bin/phpunit -c ./ --color",
"ci": [
"@composer audit",
"@prepare",
"@lint",
"@test"
]
},
"prefer-stable": true,
"minimum-stability": "dev"
Expand Down
5 changes: 4 additions & 1 deletion pint.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"strict": true
},
"no_superfluous_phpdoc_tags": false,
"php_unit_method_casing": false
"php_unit_method_casing": false,
"nullable_type_declaration_for_default_null_value": {
"use_nullable_type_declaration": true
}
}
}
45 changes: 42 additions & 3 deletions src/Commands/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@
namespace Orchestra\Canvas\Core\Commands;

use Illuminate\Console\Concerns\CallsCommands;
use Illuminate\Console\Concerns\ConfiguresPrompts;
use Illuminate\Console\Concerns\HasParameters;
use Illuminate\Console\Concerns\InteractsWithIO;
use Illuminate\Console\OutputStyle;
use Illuminate\Console\View\Components\Factory;
use Illuminate\Container\Container;
use Orchestra\Canvas\Core\Presets\Preset;
use Orchestra\Testbench\Foundation\Application as Testbench;
use Symfony\Component\Console\Command\Command as SymfonyConsole;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

abstract class Command extends \Symfony\Component\Console\Command\Command
{
use CallsCommands,
ConfiguresPrompts,
HasParameters,
InteractsWithIO;

Expand All @@ -25,6 +29,13 @@ abstract class Command extends \Symfony\Component\Console\Command\Command
*/
protected $preset;

/**
* The Laravel application instance.
*
* @var \Illuminate\Contracts\Foundation\Application
*/
protected $laravel;

/**
* Construct a new generator command.
*/
Expand All @@ -37,19 +48,37 @@ public function __construct(Preset $preset)
$this->specifyParameters();
}

/**
* Initializes the command after the input has been bound and before the input
* is validated.
*
* @return void
*
* @phpstan-param \Symfony\Component\Console\Output\OutputInterface&\Illuminate\Console\OutputStyle $output
*/
protected function initialize(InputInterface $input, OutputInterface $output)
{
$this->components = new Factory($output);

$this->configurePrompts($input);
}

/**
* Run the console command.
*
* @return int
*/
public function run(InputInterface $input, OutputInterface $output): int
{
$this->output = new OutputStyle($input, $output);
$container = Container::getInstance();

$this->components = new Factory($this->output);
$this->laravel = $container->bound('app')
? $container->get('app')
: Testbench::create(basePath: $this->preset->laravelPath());

return parent::run(
$this->input = $input, $this->output
$this->input = $input,
$this->output = new OutputStyle($input, $output)
);
}

Expand All @@ -67,4 +96,14 @@ protected function resolveCommand($command)
: $command
);
}

/**
* Get the Laravel application instance.
*
* @return \Illuminate\Contracts\Foundation\Application
*/
public function getLaravel()
{
return $this->laravel;
}
}
21 changes: 19 additions & 2 deletions src/Commands/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Orchestra\Canvas\Core\Commands;

use Illuminate\Console\Concerns\CreatesMatchingTest;
use Orchestra\Canvas\Core\CodeGenerator;
use Orchestra\Canvas\Core\Contracts\GeneratesCodeListener;
use Orchestra\Canvas\Core\GeneratesCode;
Expand All @@ -11,8 +12,8 @@
use Symfony\Component\Console\Output\OutputInterface;

/**
* @property string $name
* @property string $description
* @property string|null $name
* @property string|null $description
*/
abstract class Generator extends Command implements GeneratesCodeListener
{
Expand Down Expand Up @@ -78,6 +79,11 @@ protected function configure()
$this->setName($this->getName())
->setDescription($this->getDescription())
->addArgument('name', InputArgument::REQUIRED, "The name of the {$this->fileType}");

if (\in_array(CreatesMatchingTest::class, class_uses_recursive($this))) {
/** @phpstan-ignore-next-line */
$this->addTestOptions();
}
}

/**
Expand Down Expand Up @@ -114,6 +120,17 @@ public function codeHasBeenGenerated(string $className): int
return static::SUCCESS;
}

/**
* Run after code successfully generated.
*/
public function afterCodeHasBeenGenerated(string $className, string $path): void
{
if (\in_array(CreatesMatchingTest::class, class_uses_recursive($this))) {
/** @phpstan-ignore-next-line */
$this->handleTestCreation($path);
}
}

/**
* Get the published stub file for the generator.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Contracts/GeneratesCodeListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public function codeAlreadyExists(string $className): mixed;
*/
public function codeHasBeenGenerated(string $className): mixed;

/**
* Run after code successfully generated.
*/
public function afterCodeHasBeenGenerated(string $className, string $path): void;

/**
* Get the stub file for the generator.
*/
Expand Down
4 changes: 3 additions & 1 deletion src/GeneratesCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ public function __invoke(bool $force = false)

$this->files->put($path, $this->sortImports($this->buildClass($className)));

return $this->listener->codeHasBeenGenerated($className);
return tap($this->listener->codeHasBeenGenerated($className), function ($exitCode) use ($className, $path) {
$this->listener->afterCodeHasBeenGenerated($className, Str::of($path)->after($this->preset->sourcePath()));
});
}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/GeneratesCommandCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Orchestra\Canvas\Core;

use Illuminate\Support\Str;

/**
* @see https://github.com/laravel/framework/blob/8.x/src/Illuminate/Foundation/Console/ConsoleMakeCommand.php
* @see https://github.com/laravel/framework/blob/10.x/src/Illuminate/Foundation/Console/ConsoleMakeCommand.php
*/
class GeneratesCommandCode extends GeneratesCode
{
Expand All @@ -16,6 +18,8 @@ protected function replaceClass(string $stub, string $name): string
{
$stub = parent::replaceClass($stub, $name);

return str_replace(['dummy:command', '{{ command }}', '{{command}}'], $this->options['command'], $stub);
$command = $this->options['command'] ?: 'app:'.Str::of($name)->classBasename()->kebab()->value();

return str_replace(['dummy:command', '{{ command }}', '{{command}}'], $command, $stub);
}
}
8 changes: 8 additions & 0 deletions src/Presets/Laravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ public function name(): string
return 'laravel';
}

/**
* Get the path to the base working directory.
*/
public function laravelPath(): string
{
return $this->basePath();
}

/**
* Get the path to the source directory.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Presets/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ public function name(): string
return 'package';
}

/**
* Get the path to the base working directory.
*/
public function laravelPath(): string
{
return sprintf('%s/orchestra/testbench-core/laravel', $this->vendorPath());
}

/**
* Get the path to the source directory.
*/
Expand Down
31 changes: 30 additions & 1 deletion src/Presets/Preset.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ public function basePath(): string
return $this->basePath;
}

/**
* Get the path to the base working directory.
*/
public function vendorPath(): string
{
return "{$this->basePath}/vendor";
}

/**
* Get the path to the resource directory.
*/
Expand Down Expand Up @@ -103,10 +111,26 @@ public function seederPath(): string
return sprintf(
'%s/%s',
$this->basePath(),
$this->config('seeder.path', 'database/seeds')
$this->config('seeder.path', 'database/seeders')
);
}

/**
* Database factory namespace.
*/
public function factoryNamespace(): string
{
return $this->config('factory.namespace', 'Database\Factories');
}

/**
* Database seeder namespace.
*/
public function seederNamespace(): string
{
return $this->config('seeder.path', 'Database\Seeders');
}

/**
* Sync commands to preset.
*/
Expand All @@ -131,6 +155,11 @@ public function hasCustomStubPath(): bool
*/
abstract public function name(): string;

/**
* Get the path to the base working directory.
*/
abstract public function laravelPath(): string;

/**
* Get the path to the source directory.
*/
Expand Down
27 changes: 0 additions & 27 deletions src/Testing/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace Orchestra\Canvas\Core\Testing;

use Composer\InstalledVersions;
use Composer\Semver\VersionParser;

class TestCase extends \Orchestra\Testbench\TestCase
{
use Concerns\InteractsWithPublishedFiles;
Expand All @@ -15,28 +12,4 @@ class TestCase extends \Orchestra\Testbench\TestCase
* @var array<int, string>|null
*/
protected $files = [];

/**
* Setup the test environment.
*/
protected function setUp(): void
{
parent::setUp();

if (InstalledVersions::satisfies(new VersionParser, 'orchestra/testbench-core', '<8.2.0')) {
$this->setUpInteractsWithPublishedFiles();
}
}

/**
* Teardown the test environment.
*/
protected function tearDown(): void
{
if (InstalledVersions::satisfies(new VersionParser, 'orchestra/testbench-core', '<8.2.0')) {
$this->tearDownInteractsWithPublishedFiles();
}

parent::tearDown();
}
}
2 changes: 1 addition & 1 deletion tests/Feature/CommandsProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public function it_can_setup_laravel_preset()
$this->assertSame("{$directory}/resources", $preset->resourcePath());
$this->assertSame("{$directory}/database/factories", $preset->factoryPath());
$this->assertSame("{$directory}/database/migrations", $preset->migrationPath());
$this->assertSame("{$directory}/database/seeds", $preset->seederPath());
$this->assertSame("{$directory}/database/seeders", $preset->seederPath());
}
}
Loading

0 comments on commit 7f99fe4

Please sign in to comment.