Skip to content

Commit

Permalink
[1.x] Update make:test to use PSR-4 (#482)
Browse files Browse the repository at this point in the history
* Update the make:test command to use PSR-4 standards

* Singularize the name
  • Loading branch information
srtfisher authored Dec 1, 2023
1 parent 2e60851 commit 5acf20e
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 6 deletions.
17 changes: 15 additions & 2 deletions src/mantle/application/class-application.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

use function Mantle\Support\Helpers\str;

/**
* Mantle Application
*/
Expand Down Expand Up @@ -153,7 +155,7 @@ public function __construct( string $base_path = '', string $root_url = null ) {
* @return static
*/
public function set_base_path( string $path ) {
$this->base_path = $path;
$this->base_path = str( $path )->untrailingSlash()->value();

$this->instance( 'path', $this->get_base_path() );
$this->instance( 'path.bootstrap', $this->get_bootstrap_path() );
Expand All @@ -165,11 +167,22 @@ public function set_base_path( string $path ) {
/**
* Getter for the base path.
*
* By default, this will not have a trailing slash.
*
* @param string $path Path to append.
* @return string
*/
public function get_base_path( string $path = '' ): string {
return $this->base_path . ( $path ? DIRECTORY_SEPARATOR . $path : '' );
if ( $path ) {
// Ensure the path being appended has a leading slash.
if ( 0 !== strpos( $path, '/' ) ) {
$path = '/' . $path;
}

return str( $this->base_path )->append( $path )->value();
}

return $this->base_path;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Symfony\Component\String\Inflector\InflectorInterface;
use Throwable;

use function Mantle\Support\Helpers\str;

/**
* Generator Command
*/
Expand Down Expand Up @@ -108,7 +110,7 @@ public function handle() {
return Command::FAILURE;
}

$this->log( ( $this->type ?: 'File' ) . ' created successfully: <info>' . $file_path . '</info>' );
$this->log( str( $this->type ?: 'File' )->singular() . ' created successfully: <info>' . $file_path . '</info>' );

$this->complete_synopsis( $name );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* Test Case Generator
*/
class Test_Make_Command extends Stub_Generator_Command {
use With_PSR_4_File;

/**
* The console command name.
*
Expand All @@ -37,7 +39,7 @@ class Test_Make_Command extends Stub_Generator_Command {
*
* @var string
*/
protected $prefix = 'test-';
protected $prefix = '';

/**
* Command signature.
Expand Down
4 changes: 2 additions & 2 deletions src/mantle/framework/console/generators/stubs/test.stub
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* {{ class }} Class
* {{ class }} test class file
*
* @package App
*/
Expand All @@ -12,7 +12,7 @@ use App\Tests\TestCase;
/**
* {{ class }} Test Case.
*/
class Test_{{ class }} extends TestCase {
class {{ class }} extends TestCase {
public function test_example() {
$this->assertTrue( true );
}
Expand Down
78 changes: 78 additions & 0 deletions src/mantle/framework/console/generators/trait-with-psr-4-file.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* Has_PSR_4_Folder_Path trait file
*
* @package Mantle
*/

namespace Mantle\Framework\Console\Generators;

use function Mantle\Support\Helpers\str;

/**
* Generator support for PSR-4 files.
*
* @mixin \Mantle\Framework\Console\Generators\Generator_Command
*/
trait With_PSR_4_File {
/**
* Get the class name to use.
*
* @param string $name Inputted name.
* @return string
*/
protected function get_class_name( string $name ): string {
$name = str( str( $name )->explode( '\\' )->pop() )->studly();

if ( 'Tests' === $this->type && ! str( $name )->endsWith( 'Test' ) ) {
$name = $name->append( 'Test' );
}

return $name->value();
}

/**
* Get the folder location of the file.
*
* @param string $name Name to use.
* @return string
*/
protected function get_folder_path( string $name ): string {
$parts = str( $name )->explode( '\\' );

$parts->pop();

$folder = $parts
->filter()
->map( fn ( string $part ) => str( $part )->studly()->value() )
->implode( DIRECTORY_SEPARATOR );

return str( $this->get_base_path() )
->append( strtolower( str_replace( '\\', '/', $this->type ) ) . DIRECTORY_SEPARATOR )
->append( $folder )
->value();
}

/**
* Get the location for the generated file.
*
* @param string $name Name to use.
* @return string
*/
protected function get_file_path( string $name ): string {
$filename = str(
str( $name )->explode( '\\' )->pop(),
)
->studly();

// If the type is Tests and the filename doesn't end with Test, append it.
if ( 'Tests' === $this->type && ! $filename->endsWith( 'Test' ) ) {
$filename = $filename->append( 'Test' );
}

return str( $this->get_folder_path( $name ) )
->trailingSlash()
->append( $filename->append( '.php' ) )
->value();
}
}
38 changes: 38 additions & 0 deletions src/mantle/support/class-stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,16 @@ public function dirname( $levels = 1 ) {
return new static( dirname( $this->value, $levels ) );
}

/**
* Alias to ends_with().
*
* @param string|iterable<string> $needles
* @return bool
*/
public function endsWith( $needles ) {
return $this->ends_with( $needles );
}

/**
* Determine if a given string ends with a given substring.
*
Expand Down Expand Up @@ -280,6 +290,34 @@ public function finish( $cap ) {
return new static( Str::finish( $this->value, $cap ) );
}

/**
* Ensure the string has a single trailing slash.
*
* @return static
*/
public function trailingSlash() {
return new static( Str::trailing_slash( $this->value ) );
}

/**
* Remove a trailing slash from the string.
*
* @return static
*/
public function untrailingSlash() {
return new static( Str::untrailing_slash( $this->value ) );
}

/**
* Remove a trailing string from the string.
*
* @param string $cap
* @return static
*/
public function untrailing( $cap ) {
return new static( rtrim( $this->value, $cap ) );
}

/**
* Determine if a given string matches a given pattern.
*
Expand Down

0 comments on commit 5acf20e

Please sign in to comment.