From 0b06d9985f29b94260e3b7e0d1a887fb55b2d286 Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Tue, 31 Mar 2020 11:53:08 -0400 Subject: [PATCH] Strengthen test with data provider --- src/Blueprint.php | 17 +++++++++-------- tests/Feature/BlueprintTest.php | 27 +++++++++++++++------------ 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/Blueprint.php b/src/Blueprint.php index 745ea1b2..bba03703 100644 --- a/src/Blueprint.php +++ b/src/Blueprint.php @@ -3,6 +3,7 @@ namespace Blueprint; use Blueprint\Contracts\Lexer; +use Illuminate\Support\Str; use Symfony\Component\Yaml\Yaml; use Blueprint\Contracts\Generator; @@ -13,14 +14,14 @@ class Blueprint public static function relativeNamespace(string $fullyQualifiedClassName) { - $newClassName = preg_replace( - '!^'.preg_quote(config('blueprint.namespace')).'!', - '', - $fullyQualifiedClassName, - 1 - ); - - return ltrim($newClassName, '\\'); + $namespace = config('blueprint.namespace') . '\\'; + $reference = ltrim($fullyQualifiedClassName, '\\'); + + if (Str::startsWith($reference, $namespace)) { + return Str::after($reference, $namespace); + } + + return $reference; } public function parse($content) diff --git a/tests/Feature/BlueprintTest.php b/tests/Feature/BlueprintTest.php index cf4d6077..fb6f8bf1 100644 --- a/tests/Feature/BlueprintTest.php +++ b/tests/Feature/BlueprintTest.php @@ -278,21 +278,24 @@ public function generate_uses_registered_generators_and_returns_generated_files( /** * @test + * @dataProvider namespacesDataProvider */ - public function relative_namespace_only_replace_first_occurrence_of_default_namespace() + public function relative_namespace_removes_namespace_prefix_from_reference($namespace, $expected, $reference) { - $string = "App\Appointments"; + config(['blueprint.namespace' => $namespace]); - $actual = Blueprint::relativeNamespace($string); - - $this->assertEquals("Appointments", $actual); - - config(['blueprint.namespace'=>'Foo']); - - $string = "Foo\Appointments"; - - $actual = Blueprint::relativeNamespace($string); + $this->assertEquals($expected, Blueprint::relativeNamespace($reference)); + } - $this->assertEquals("Appointments", $actual); + public function namespacesDataProvider() + { + return [ + ['App', 'Models\User', 'App\Models\User'], + ['App', 'Models\User', '\App\Models\User'], + ['App', 'Some\Other\Reference', 'Some\Other\Reference'], + ['App', 'App\Appointments', 'App\App\Appointments'], + ['Foo', 'Bar', 'Foo\Bar'], + ['Foo', 'Foo\Bar', '\Foo\Foo\Bar'], + ]; } }