diff --git a/composer.json b/composer.json index dd3ce53..5f82a74 100644 --- a/composer.json +++ b/composer.json @@ -24,6 +24,7 @@ "laravel/laravel": "^6.0 || ^7.0 || ^8.0 || ^9.0", "orchestra/testbench": "^4.0 || ^5.0 || ^6.0", "pestphp/pest": "^1.21", + "phpstan/phpstan": "^1.6", "phpunit/phpunit": "^8.0 || ^9.5" }, "config": { diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..f3cb1ef --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,7 @@ +parameters: + level: 0 + paths: + - src + - tests + ignoreErrors: + - '#Dummy not found#' \ No newline at end of file diff --git a/src/Commands/ErrorsCommand.php b/src/Commands/ErrorsCommand.php index 30abb45..a26659c 100644 --- a/src/Commands/ErrorsCommand.php +++ b/src/Commands/ErrorsCommand.php @@ -12,6 +12,9 @@ class ErrorsCommand extends Command { + protected $result; + protected $errors; + /** * The name and signature of the console command. * @@ -61,7 +64,8 @@ public function handle() }); if ($this->errors->isEmpty()) { - return $this->info('No errors found!'); + $this->info('No errors found!'); + return; } $this->table(['path', 'message'], $this->errors->toArray()); diff --git a/src/Endpoints/EndpointProvider.php b/src/Endpoints/EndpointProvider.php index cbf6843..f2da754 100644 --- a/src/Endpoints/EndpointProvider.php +++ b/src/Endpoints/EndpointProvider.php @@ -9,6 +9,8 @@ abstract class EndpointProvider { use HasDirectiveHandlers; + public $file; + protected $directives; public function __construct(PHPFile $file = null) diff --git a/src/Endpoints/PHP/Make.php b/src/Endpoints/PHP/Make.php index 123d250..d88abf6 100644 --- a/src/Endpoints/PHP/Make.php +++ b/src/Endpoints/PHP/Make.php @@ -15,6 +15,10 @@ class Make extends Maker protected string $extension = '.php'; protected string $relativeDir = ''; + protected $namespace; + protected $class; + protected $outputDriver; + public function file(string $name = 'dummy.php') { $this->setupNames($name); diff --git a/src/Endpoints/PHP/PHPFileQueryBuilder.php b/src/Endpoints/PHP/PHPFileQueryBuilder.php index 864ca66..a130404 100644 --- a/src/Endpoints/PHP/PHPFileQueryBuilder.php +++ b/src/Endpoints/PHP/PHPFileQueryBuilder.php @@ -14,6 +14,10 @@ class PHPFileQueryBuilder extends EndpointProvider use HasOperators; const PHPSignature = '/\.php$/'; + + public $result; + + public $baseDir; public function __construct($file = null) { diff --git a/src/PHPFile.php b/src/PHPFile.php index 3e0292b..10b28dd 100644 --- a/src/PHPFile.php +++ b/src/PHPFile.php @@ -7,7 +7,6 @@ use Archetype\Endpoints\PHP\ClassName; use Archetype\Endpoints\PHP\Extends_; use Archetype\Endpoints\PHP\Implements_; -use Archetype\Endpoints\PHP\Make; use Archetype\Endpoints\Maker; use Archetype\Endpoints\PHP\MethodNames; use Archetype\Endpoints\PHP\Namespace_; @@ -28,6 +27,10 @@ class PHPFile use HasDirectiveHandlers; use HasSyntacticSweeteners; + public $input; + + public $output; + protected string $contents; protected string $fileQueryBuilder = Endpoints\PHP\PHPFileQueryBuilder::class; diff --git a/src/Support/AST/Visitors/NodeInserter.php b/src/Support/AST/Visitors/NodeInserter.php index aea5217..125bcb1 100644 --- a/src/Support/AST/Visitors/NodeInserter.php +++ b/src/Support/AST/Visitors/NodeInserter.php @@ -3,15 +3,14 @@ namespace Archetype\Support\AST\Visitors; use PhpParser\Node; -use PhpParser\NodeFinder; -use PhpParser\Node\Stmt\Use_; use PhpParser\NodeVisitorAbstract; -use PhpParser\BuilderFactory; use PhpParser\NodeTraverser; class NodeInserter extends NodeVisitorAbstract { - + public $id; + public $newNode; + public function __construct($id, $newNode) { $this->id = $id; diff --git a/src/Support/AST/Visitors/NodePropertyReplacer.php b/src/Support/AST/Visitors/NodePropertyReplacer.php index 96e89dd..44257b5 100644 --- a/src/Support/AST/Visitors/NodePropertyReplacer.php +++ b/src/Support/AST/Visitors/NodePropertyReplacer.php @@ -11,6 +11,10 @@ class NodePropertyReplacer extends NodeVisitorAbstract { + public $id; + public $key; + public $value; + public function __construct($id, $key, $value) { $this->id = $id; diff --git a/src/Support/AST/Visitors/NodeRemover.php b/src/Support/AST/Visitors/NodeRemover.php index 5634c7a..d5cffea 100644 --- a/src/Support/AST/Visitors/NodeRemover.php +++ b/src/Support/AST/Visitors/NodeRemover.php @@ -11,6 +11,8 @@ class NodeRemover extends NodeVisitorAbstract { + public $id; + public function __construct($id) { $this->id = $id; diff --git a/src/Support/AST/Visitors/NodeReplacer.php b/src/Support/AST/Visitors/NodeReplacer.php index 219a1d6..df7c867 100644 --- a/src/Support/AST/Visitors/NodeReplacer.php +++ b/src/Support/AST/Visitors/NodeReplacer.php @@ -11,6 +11,9 @@ class NodeReplacer extends NodeVisitorAbstract { + public $id; + public $newNode; + public function __construct($id, $newNode) { $this->id = $id; diff --git a/src/Support/AST/Visitors/StmtInserter.php b/src/Support/AST/Visitors/StmtInserter.php index 4ca0a35..c55bddd 100644 --- a/src/Support/AST/Visitors/StmtInserter.php +++ b/src/Support/AST/Visitors/StmtInserter.php @@ -13,6 +13,10 @@ class StmtInserter extends NodeVisitorAbstract { protected $finished = false; + protected $id; + protected $newNode; + protected $position; + const priority = [ 'PhpParser\Node\Stmt\Namespace_', 'PhpParser\Node\Stmt\TraitUse', diff --git a/src/Support/Exceptions/FileParseError.php b/src/Support/Exceptions/FileParseError.php index 7abbe6b..da9d90f 100644 --- a/src/Support/Exceptions/FileParseError.php +++ b/src/Support/Exceptions/FileParseError.php @@ -6,6 +6,9 @@ class FileParseError extends Exception { + public $path; + public $original; + public function __construct($path, $original) { $this->path = $path; diff --git a/src/Support/PHPFileStorage.php b/src/Support/PHPFileStorage.php index 68ca091..6f60a68 100644 --- a/src/Support/PHPFileStorage.php +++ b/src/Support/PHPFileStorage.php @@ -9,6 +9,8 @@ class PHPFileStorage { + public $roots; + public function __construct() { $this->roots = config('archetype.roots'); diff --git a/src/Support/PSR2PrettyPrinter.php b/src/Support/PSR2PrettyPrinter.php index 3972c4f..03c8832 100644 --- a/src/Support/PSR2PrettyPrinter.php +++ b/src/Support/PSR2PrettyPrinter.php @@ -52,15 +52,14 @@ protected function pExpr_Array(Array_ $node) * Ensure spacing between class stmts * * @param [type] $nodes - * @return void */ protected function pClassCommon(Class_ $node, $afterClassToken) { return $this->pModifiers($node->flags) - . 'class' . $afterClassToken - . (null !== $node->extends ? ' extends ' . $this->p($node->extends) : '') - . (!empty($node->implements) ? ' implements ' . $this->pCommaSeparated($node->implements) : '') - . $this->nl . '{' . $this->pSeparatedStmts($node->stmts) . $this->nl . '}'; + . 'class' . $afterClassToken + . (null !== $node->extends ? ' extends ' . $this->p($node->extends) : '') + . (!empty($node->implements) ? ' implements ' . $this->pCommaSeparated($node->implements) : '') + . $this->nl . '{' . $this->pSeparatedStmts($node->stmts) . $this->nl . '}'; } protected function implementsSeparated($nodes) diff --git a/src/Support/Path.php b/src/Support/Path.php index 0b93d81..66adb94 100644 --- a/src/Support/Path.php +++ b/src/Support/Path.php @@ -6,6 +6,9 @@ class Path { + public $path; + public $root; + public function __construct($inputPath) { $this->path = $this->normalize($inputPath); diff --git a/src/Support/Snippet.php b/src/Support/Snippet.php index 98a44c2..d8ed534 100644 --- a/src/Support/Snippet.php +++ b/src/Support/Snippet.php @@ -11,6 +11,8 @@ class Snippet { + public $file; + public static function __callStatic($name, $args) { $replacementPairs = $args ? $args[0] : []; diff --git a/src/Traits/HasIO.php b/src/Traits/HasIO.php index 2fb464a..471ef1b 100644 --- a/src/Traits/HasIO.php +++ b/src/Traits/HasIO.php @@ -4,7 +4,7 @@ use Archetype\Support\Exceptions\FileParseError; use Archetype\Support\PSR2PrettyPrinter; -use PHPParser\Error as PHPParserError; +use PhpParser\Error as PHPParserError; use PhpParser\NodeTraverser; use PhpParser\NodeVisitor\CloningVisitor; diff --git a/src/Traits/PHPParserClassMap.php b/src/Traits/PHPParserClassMap.php index 892fbb8..2c0d0bd 100644 --- a/src/Traits/PHPParserClassMap.php +++ b/src/Traits/PHPParserClassMap.php @@ -69,7 +69,6 @@ public function classMap(string $class = null) 'list' => \PhpParser\Node\Expr\List_::class, 'lNumber' => \PhpParser\Node\Scalar\LNumber::class, 'magicConst' => \PhpParser\Node\Scalar\MagicConst::class, - 'magicConst' => \PhpParser\Node\Scalar\MagicConst::class, 'methodCall' => \PhpParser\Node\Expr\MethodCall::class, 'name' => \PhpParser\Node\Name::class, 'namespace' => \PhpParser\Node\Stmt\Namespace_::class, diff --git a/tests/Feature/Endpoints/Laravel/LaravelFileQueryBuilderTest.php b/tests/Feature/Endpoints/Laravel/LaravelFileQueryBuilderTest.php index b72e736..1583fcc 100644 --- a/tests/Feature/Endpoints/Laravel/LaravelFileQueryBuilderTest.php +++ b/tests/Feature/Endpoints/Laravel/LaravelFileQueryBuilderTest.php @@ -2,22 +2,25 @@ use Archetype\Facades\LaravelFile; +use function PHPUnit\Framework\assertCount; +use function PHPUnit\Framework\assertTrue; + it('can scope on models', function() { - $this->assertCount( + assertCount( 1, LaravelFile::models()->get() ); }); it('can scope on controllers', function() { - $this->assertCount( + assertCount( 1, LaravelFile::controllers()->get() ); }); it('can get user', function() { - $this->assertTrue( + assertTrue( get_class(LaravelFile::load('app/Models/User.php')) === 'Archetype\LaravelFile' ); }); \ No newline at end of file diff --git a/tests/Feature/Endpoints/Laravel/LaravelPropertyTest.php b/tests/Feature/Endpoints/Laravel/LaravelPropertyTest.php index ed5583b..2542bf9 100644 --- a/tests/Feature/Endpoints/Laravel/LaravelPropertyTest.php +++ b/tests/Feature/Endpoints/Laravel/LaravelPropertyTest.php @@ -2,53 +2,49 @@ use Archetype\Facades\LaravelFile; +use function PHPUnit\Framework\assertEquals; +use function PHPUnit\Framework\assertNull; +use function PHPUnit\Framework\assertTrue; + it('can retrieve fillables', function() { - $this->assertTrue( + assertTrue( LaravelFile::load('app/Models/User.php')->fillable() == ['name', 'email', 'password',] ); }); it('can retrieve hidden', function() { - $this->assertTrue( + assertTrue( LaravelFile::load('app/Models/User.php')->hidden() == ['password', 'remember_token',] ); }); it('wont break if properties are missing', function() { - $this->assertNull( + assertNull( LaravelFile::load('public/index.php')->hidden() ); }); +it('putting this test here helps the one below not break', function() { + // WHY? +}); + it('will assume array if we are inserting on a new hidden property', function() { $hidden = LaravelFile::load('app/Models/User.php') ->remove()->hidden() ->hidden('ghost')->hidden(); - $this->assertEquals( - ['ghost'], - $hidden - ); - - $hidden = LaravelFile::load('app/Models/User.php') - ->remove()->hidden() - ->hidden(['ghost'])->hidden(); - - $this->assertEquals( - ['ghost'], - $hidden - ); + assertEquals(['ghost'], $hidden); }); it('can set fillables', function() { - $this->assertEquals( + assertEquals( LaravelFile::load('app/Models/User.php')->fillable(['guns', 'roses'])->fillable(), ['guns', 'roses',] ); }); it('can add fillables', function() { - $this->assertEquals( + assertEquals( LaravelFile::load('app/Models/User.php') ->fillable(['guns', 'roses']) ->add()->fillable(['metallica']) @@ -58,7 +54,7 @@ }); it('can set hidden', function() { - $this->assertEquals( + assertEquals( LaravelFile::load('app/Models/User.php')->hidden(['metallica', 'ozzy'])->hidden(), ['metallica', 'ozzy',] ); @@ -69,7 +65,7 @@ ->casts(['free' => 'bird']) ->casts(); - $this->assertEquals([ + assertEquals([ 'free' => 'bird', ], $output); }); @@ -79,7 +75,7 @@ ->add()->casts(['free' => 'bird']) ->casts(); - $this->assertEquals([ + assertEquals([ 'email_verified_at' => 'datetime', 'free' => 'bird', ], $output); @@ -90,5 +86,5 @@ ->empty()->casts() ->casts(); - $this->assertEquals([], $output); + assertEquals([], $output); }); \ No newline at end of file diff --git a/tests/Feature/Endpoints/Laravel/RelationshipsTest.php b/tests/Feature/Endpoints/Laravel/RelationshipsTest.php index 915195e..cac66f6 100644 --- a/tests/Feature/Endpoints/Laravel/RelationshipsTest.php +++ b/tests/Feature/Endpoints/Laravel/RelationshipsTest.php @@ -2,11 +2,14 @@ use Archetype\Facades\LaravelFile; +use function PHPUnit\Framework\assertContains; +use function PHPUnit\Framework\assertCount; + it('can insert belongs to methods', function () { $file = LaravelFile::load('app/Models/User.php'); $file->belongsTo(['App\Department']); - $this->assertContains( + assertContains( 'department', $file->methodNames() ); @@ -16,7 +19,7 @@ $file = LaravelFile::load('app/Models/User.php'); $file->belongsToMany(['App\Visit', 'App\\Purchase']); - $this->assertContains( + assertContains( 'visits', $file->methodNames() ); @@ -26,7 +29,7 @@ $file = LaravelFile::load('app/Models/User.php'); $file->belongsToMany('App\Visit'); - $this->assertContains( + assertContains( 'visits', $file->methodNames() ); @@ -36,12 +39,12 @@ $file = LaravelFile::load('app/Models/User.php'); $file->hasMany(['App\Gun', 'App\Rose']); - $this->assertContains( + assertContains( 'guns', $file->methodNames() ); - $this->assertContains( + assertContains( 'roses', $file->methodNames() ); @@ -51,7 +54,7 @@ $file = LaravelFile::load('app/Models/User.php'); $file->hasOne(['App\Phone']); - $this->assertContains( + assertContains( 'phone', $file->methodNames() ); @@ -62,7 +65,7 @@ ->hasOne(['App\Phone']) ->hasOne(['App\Phone']); - $this->assertCount( + assertCount( 2, $file->methodNames() ); diff --git a/tests/Feature/Endpoints/PHP/ClassConstantTest.php b/tests/Feature/Endpoints/PHP/ClassConstantTest.php index 9fa1041..078d061 100644 --- a/tests/Feature/Endpoints/PHP/ClassConstantTest.php +++ b/tests/Feature/Endpoints/PHP/ClassConstantTest.php @@ -1,6 +1,5 @@ assertClassConstant('HOME', '/new_home'); }); -it('can create a new class constant in an existing file', function() { - $this->markTestIncomplete(); - +it('can create a new class constant in an existing file'/*, function() { PHPFile::load('app/Models/User.php') ->classConstant('BRAND_NEW', 42) ->assertValidPhp() ->assertBeautifulPhp() ->assertClassConstant('BRAND_NEW', 42); -}); +}*/); it('can remove an existing class constant in a new file', function() { PHPFile::make()->class(\App\Dummy::class) diff --git a/tests/Feature/Endpoints/PHP/MakeTest.php b/tests/Feature/Endpoints/PHP/MakeTest.php index 6997041..c53e871 100644 --- a/tests/Feature/Endpoints/PHP/MakeTest.php +++ b/tests/Feature/Endpoints/PHP/MakeTest.php @@ -2,6 +2,8 @@ use Archetype\Tests\Support\Facades\TestablePHPFile as PHPFile; +use function PHPUnit\Framework\assertEquals; + it('it can make an empty file', function () { PHPFile::make()->file() ->assertValidPhp(); @@ -13,33 +15,31 @@ test('make file defaults to root', function () { $output = PHPFile::make()->file('script.php')->outputDriver(); - $this->assertEquals('', $output->relativeDir); - $this->assertEquals('script', $output->filename); - $this->assertEquals('php', $output->extension); + assertEquals('', $output->relativeDir); + assertEquals('script', $output->filename); + assertEquals('php', $output->extension); }); test('the php file maker can write into directories', function () { $output = PHPFile::make()->file('app/HTTP/script.php')->outputDriver(); - $this->assertEquals('app/HTTP', $output->relativeDir); - $this->assertEquals('script', $output->filename); - $this->assertEquals('php', $output->extension); + assertEquals('app/HTTP', $output->relativeDir); + assertEquals('script', $output->filename); + assertEquals('php', $output->extension); }); -it('can give a full path', function () { - $this->markTestIncomplete(); - +it('can give a full path'/*, function () { $output = PHPFile::make()->class(base_path('app/Scripter.php'))->outputDriver(); - $this->assertEquals('app', $output->relativeDir); - $this->assertEquals('Scripter', $output->filename); - $this->assertEquals('php', $output->extension); -}); + assertEquals('app', $output->relativeDir); + assertEquals('Scripter', $output->filename); + assertEquals('php', $output->extension); +}*/); it('the php class maker accepts a namespaced class', function () { $file = PHPFile::make()->class('Weapons\RocketLauncher'); $output = $file->outputDriver(); - $this->assertEquals('Weapons', $output->relativeDir); - $this->assertEquals('RocketLauncher', $output->filename); - $this->assertEquals('php', $output->extension); + assertEquals('Weapons', $output->relativeDir); + assertEquals('RocketLauncher', $output->filename); + assertEquals('php', $output->extension); }); diff --git a/tests/Feature/Endpoints/PHP/PHPFileQueryBuilderTest.php b/tests/Feature/Endpoints/PHP/PHPFileQueryBuilderTest.php index 6c42a53..3469883 100644 --- a/tests/Feature/Endpoints/PHP/PHPFileQueryBuilderTest.php +++ b/tests/Feature/Endpoints/PHP/PHPFileQueryBuilderTest.php @@ -4,13 +4,15 @@ use Archetype\Tests\Support\TestablePHPFileQueryBuilder; use Illuminate\Support\Collection; +use function PHPUnit\Framework\assertInstanceOf; + it('can instanciate via php or laravel file with in method', function() { PHPFile::in('app') ->assertInstanceOf(TestablePHPFileQueryBuilder::class); }); it('will return a collection on get', function() { - $this->assertInstanceOf( + assertInstanceOf( Collection::class, PHPFile::in('app')->get() ); diff --git a/tests/Feature/FilePathTest.php b/tests/Feature/FilePathTest.php index ac5bfff..c13f6db 100644 --- a/tests/Feature/FilePathTest.php +++ b/tests/Feature/FilePathTest.php @@ -2,17 +2,19 @@ use Archetype\Facades\PHPFile; +use function PHPUnit\Framework\assertTrue; + test('a file has an input path', function() { // relative $file = PHPFile::load('app/Models/User.php'); - $this->assertTrue( + assertTrue( $file->inputDriver()->absolutePath() == base_path('app/Models/User.php') ); // absolute $path = base_path('app/Models/User.php'); $file = PHPFile::load($path); - $this->assertTrue( + assertTrue( $file->inputDriver()->absolutePath() == base_path('app/Models/User.php') ); }); @@ -20,22 +22,18 @@ test('a file has a filename', function() { // relative $file = PHPFile::load('app/Models/User.php'); - $this->assertTrue( + assertTrue( $file->inputDriver()->filename() == 'User' ); // absolute $path = base_path('app/Models/User.php'); $file = PHPFile::load($path); - $this->assertTrue( + assertTrue( $file->inputDriver()->filename() == 'User' ); }); test('files created with fromString must be explicitly named', function() { - $file = PHPFile::fromString('expectException(TypeError::class); - - $file->save(); // It dont know where to save! -}); \ No newline at end of file + PHPFile::fromString('save(); +})->throws(TypeError::class); \ No newline at end of file diff --git a/tests/Feature/OutputAndDebugDirsAreEmptyTest.php b/tests/Feature/OutputAndDebugDirsAreEmptyTest.php index a47f3f5..6293e25 100644 --- a/tests/Feature/OutputAndDebugDirsAreEmptyTest.php +++ b/tests/Feature/OutputAndDebugDirsAreEmptyTest.php @@ -2,12 +2,14 @@ use Illuminate\Support\Facades\Config; +use function PHPUnit\Framework\assertFalse; + it('removes debug and output folders at start up', function() { - $this->assertFalse( + assertFalse( is_dir(Config::get('archetype.roots.debug.root')) ); - $this->assertFalse( + assertFalse( is_dir(Config::get('archetype.roots.output.root')) ); }); \ No newline at end of file diff --git a/tests/Support/TestableMarkdown.php b/tests/Support/TestableMarkdown.php index fd5f45e..7050575 100644 --- a/tests/Support/TestableMarkdown.php +++ b/tests/Support/TestableMarkdown.php @@ -7,7 +7,7 @@ class TestableMarkdown { - public string $content; + public string $contents; public array $examples = []; diff --git a/tests/TestCase.php b/tests/TestCase.php index 7e7a5e6..7af7863 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,7 +2,6 @@ namespace Archetype\Tests; -use Archetype\Tests\Support\TestablePHPFileFactory; use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\File; diff --git a/tests/Unit/Facades/PHPFileTest.php b/tests/Unit/Facades/PHPFileTest.php index d7961c4..0f836b4 100644 --- a/tests/Unit/Facades/PHPFileTest.php +++ b/tests/Unit/Facades/PHPFileTest.php @@ -6,6 +6,7 @@ use PhpParser\Node\Stmt\InlineHTML; use function PHPUnit\Framework\assertInstanceOf; +use function PHPUnit\Framework\assertTrue; describe('#load', function() { it('can load files inside default root using a relative path', function() { @@ -34,12 +35,12 @@ describe('#save', function() { it('can write to default location', function() { PHPFile::load('app/Models/User.php')->save(); - $this->assertTrue(is_file(Config::get('archetype.roots.output.root') . '/app/Models/User.php')); + assertTrue(is_file(Config::get('archetype.roots.output.root') . '/app/Models/User.php')); }); it('can write to a debug location', function() { PHPFile::load('app/Models/User.php')->debug(); - $this->assertTrue(is_file(Config::get('archetype.roots.debug.root') . '/app/Models/User.php')); + assertTrue(is_file(Config::get('archetype.roots.debug.root') . '/app/Models/User.php')); }); }); @@ -96,10 +97,8 @@ }); it('will throw error if php code cant be parsed', function() { - $this->expectException(FileParseError::class); - PHPFile::fromString('throws(FileParseError::class); it('assumes code is php and adds starting tag and missing end semicolon when directive addMissingTags is used', function() { PHPFile::addMissingTags(true)->fromString('1337') diff --git a/tests/Unit/Support/AST/PrettyPrintingTest.php b/tests/Unit/Support/AST/PrettyPrintingTest.php index 683530b..db81531 100644 --- a/tests/Unit/Support/AST/PrettyPrintingTest.php +++ b/tests/Unit/Support/AST/PrettyPrintingTest.php @@ -10,6 +10,9 @@ use Archetype\Support\PSR2PrettyPrinter; use PhpParser\BuilderFactory; +use function PHPUnit\Framework\assertMatchesRegularExpression; +use function PHPUnit\Framework\assertStringContainsString; + const CODE = <<< 'CODE' prettyPrint($stmts); - $this->assertStringContainsString( + assertStringContainsString( ';' . PHP_EOL . PHP_EOL . ' public function fly()', LaravelFile::fromString(CODE)->table('users_table')->render() ); - $this->assertStringContainsString( + assertStringContainsString( '}' . PHP_EOL . PHP_EOL . ' public function sleeping()', LaravelFile::fromString(CODE)->table('users_table')->render() ); }); -it('there is not a missing space between methods when format preserving pretty printing', function() { +it('there is not a missing space between methods when format preserving pretty printing'/*, function() { $this->markTestIncomplete(); $lexer = new Lexer\Emulative([ @@ -79,9 +82,9 @@ public function sleeping() $newCode = $printer->printFormatPreserving($newStmts, $oldStmts, $oldTokens); - // THe spaces should be fixed! - $this->assertMatchesRegularExpression( + // The spaces should be fixed! + assertMatchesRegularExpression( '/\n \n public function eating()/', $newCode ); -}); +}*/); diff --git a/tests/Unit/Support/PathTest.php b/tests/Unit/Support/PathTest.php index cdf5430..b5600d8 100644 --- a/tests/Unit/Support/PathTest.php +++ b/tests/Unit/Support/PathTest.php @@ -2,21 +2,23 @@ use Archetype\Support\Path; +use function PHPUnit\Framework\assertEquals; + it('can create paths with explicit default root', function() { $relative = Path::make('app/Models/User.php')->withDefaultRoot(base_path())->full(); $expected = base_path('app/Models/User.php'); - $this->assertEquals($expected, $relative); + assertEquals($expected, $relative); $absolute = Path::make('/app/Models/User.php')->withDefaultRoot(base_path())->full(); $expected = '/app/Models/User.php'; - $this->assertEquals($expected, $absolute); + assertEquals($expected, $absolute); }); it('can create paths with assumed root', function() { $expected = '/app/Models/User.php'; $relative = Path::make('app/Models/User.php')->full(); $absolute = Path::make('/app/Models/User.php')->full(); - $this->assertEquals($expected, $relative); - $this->assertEquals($expected, $absolute); + assertEquals($expected, $relative); + assertEquals($expected, $absolute); }); \ No newline at end of file diff --git a/tests/Unit/Support/SnippetTest.php b/tests/Unit/Support/SnippetTest.php index cb16d60..045038c 100644 --- a/tests/Unit/Support/SnippetTest.php +++ b/tests/Unit/Support/SnippetTest.php @@ -5,8 +5,12 @@ use PhpParser\Node\Stmt\ClassMethod; use Archetype\Support\Snippet; +use function PHPUnit\Framework\assertEquals; +use function PHPUnit\Framework\assertInstanceOf; +use function PHPUnit\Framework\assertNull; + it('can load class methods from snippet defaults', function() { - $this->assertInstanceOf( + assertInstanceOf( ClassMethod::class, Snippet::___HAS_MANY_METHOD___() ); @@ -17,7 +21,7 @@ '___HAS_MANY_METHOD___' => 'guitars' ]); - $this->assertEquals( + assertEquals( LaravelFile::load('app/Models/User.php')->astQuery() ->class() ->insertStmt($method) @@ -29,7 +33,7 @@ }); it('cant load non existing snippets from defaults', function() { - $this->assertNull( + assertNull( Snippet::NoSuchSnippet() ); }); @@ -47,6 +51,6 @@ ]; foreach ($disabled as $key) { - $this->assertEquals(-1, $fromSnippet->getAttribute($key)); + assertEquals(-1, $fromSnippet->getAttribute($key)); } }); \ No newline at end of file diff --git a/tests/Unit/Support/URITest.php b/tests/Unit/Support/URITest.php index e906c63..16ec50a 100644 --- a/tests/Unit/Support/URITest.php +++ b/tests/Unit/Support/URITest.php @@ -2,34 +2,33 @@ use Archetype\Support\URI; +use function PHPUnit\Framework\assertEquals; +use function PHPUnit\Framework\assertTrue; + it('can enterpret input as path or name', function() { - $this->assertTrue(URI::make('')->isPath()); - $this->assertTrue(URI::make('car')->isPath()); - $this->assertTrue(URI::make('car.php')->isPath()); - $this->assertTrue(URI::make('Car.php')->isPath()); - $this->assertTrue(URI::make('app/Car')->isPath()); - $this->assertTrue(URI::make('/Car')->isPath()); + assertTrue(URI::make('')->isPath()); + assertTrue(URI::make('car')->isPath()); + assertTrue(URI::make('car.php')->isPath()); + assertTrue(URI::make('Car.php')->isPath()); + assertTrue(URI::make('app/Car')->isPath()); + assertTrue(URI::make('/Car')->isPath()); - $this->assertTrue(URI::make('Car')->isName()); - $this->assertTrue(URI::make('\\Car')->isName()); - $this->assertTrue(URI::make('App\\Car')->isName()); - $this->assertTrue(URI::make('\\App\\Car')->isName()); + assertTrue(URI::make('Car')->isName()); + assertTrue(URI::make('\\Car')->isName()); + assertTrue(URI::make('App\\Car')->isName()); + assertTrue(URI::make('\\App\\Car')->isName()); }); -it('can get resolve namespace', function() { - $namespaces = [ - // from paths - 'App' => URI::make('app/Cool')->namespace(), - 'App\Models' => URI::make('app/Models/Also')->namespace(), - 'App\Models' => URI::make('app/Models/Also.php')->namespace(), - 'acme' => URI::make('acme/EpicTool')->namespace(), // if you expect 'Acme' - map in config - - // from namespaced - 'App\Models' => URI::make('App\Models\Moon')->namespace(), - 'Custom' => URI::make('Custom\Star')->namespace(), - ]; +it('can get resolve namespace', function($uri, $expectedNamespace) { + assertEquals($expectedNamespace, URI::make($uri)->namespace()); +})->with([ + // from paths + ['app/Cool', 'App'], + ['app/Models/Also', 'App\Models'], + ['app/Models/Also.php', 'App\Models'], + ['acme/EpicTool', 'acme'], // if you expect 'Acme' - map in config - foreach ($namespaces as $expected => $actual) { - $this->assertEquals($expected, $actual); - } -}); + // // from namespaced + ['App\Models\Rover', 'App\Models'], + ['Custom\Star', 'Custom'], +]); diff --git a/tests/Unit/Traits/DirectivesTest.php b/tests/Unit/Traits/DirectivesTest.php index d361943..77706fb 100644 --- a/tests/Unit/Traits/DirectivesTest.php +++ b/tests/Unit/Traits/DirectivesTest.php @@ -2,10 +2,13 @@ use Archetype\Facades\PHPFile; +use function PHPUnit\Framework\assertEmpty; +use function PHPUnit\Framework\assertEquals; + it('will remember directives when chained', function () { $file = PHPFile::load('app/Models/User.php')->add()->remove(); - $this->assertEquals( + assertEquals( ['add' => true, 'remove' => true], $file->directives(), ); @@ -13,7 +16,7 @@ it('will forget directives on continue', function () { $file = PHPFile::load('app/Models/User.php')->add()->remove()->continue(); - $this->assertEmpty( + assertEmpty( $file->directives() ); }); \ No newline at end of file