Skip to content

Commit 0b06d99

Browse files
committed
Strengthen test with data provider
1 parent b76bf39 commit 0b06d99

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

src/Blueprint.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Blueprint;
44

55
use Blueprint\Contracts\Lexer;
6+
use Illuminate\Support\Str;
67
use Symfony\Component\Yaml\Yaml;
78
use Blueprint\Contracts\Generator;
89

@@ -13,14 +14,14 @@ class Blueprint
1314

1415
public static function relativeNamespace(string $fullyQualifiedClassName)
1516
{
16-
$newClassName = preg_replace(
17-
'!^'.preg_quote(config('blueprint.namespace')).'!',
18-
'',
19-
$fullyQualifiedClassName,
20-
1
21-
);
22-
23-
return ltrim($newClassName, '\\');
17+
$namespace = config('blueprint.namespace') . '\\';
18+
$reference = ltrim($fullyQualifiedClassName, '\\');
19+
20+
if (Str::startsWith($reference, $namespace)) {
21+
return Str::after($reference, $namespace);
22+
}
23+
24+
return $reference;
2425
}
2526

2627
public function parse($content)

tests/Feature/BlueprintTest.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -278,21 +278,24 @@ public function generate_uses_registered_generators_and_returns_generated_files(
278278

279279
/**
280280
* @test
281+
* @dataProvider namespacesDataProvider
281282
*/
282-
public function relative_namespace_only_replace_first_occurrence_of_default_namespace()
283+
public function relative_namespace_removes_namespace_prefix_from_reference($namespace, $expected, $reference)
283284
{
284-
$string = "App\Appointments";
285+
config(['blueprint.namespace' => $namespace]);
285286

286-
$actual = Blueprint::relativeNamespace($string);
287-
288-
$this->assertEquals("Appointments", $actual);
289-
290-
config(['blueprint.namespace'=>'Foo']);
291-
292-
$string = "Foo\Appointments";
293-
294-
$actual = Blueprint::relativeNamespace($string);
287+
$this->assertEquals($expected, Blueprint::relativeNamespace($reference));
288+
}
295289

296-
$this->assertEquals("Appointments", $actual);
290+
public function namespacesDataProvider()
291+
{
292+
return [
293+
['App', 'Models\User', 'App\Models\User'],
294+
['App', 'Models\User', '\App\Models\User'],
295+
['App', 'Some\Other\Reference', 'Some\Other\Reference'],
296+
['App', 'App\Appointments', 'App\App\Appointments'],
297+
['Foo', 'Bar', 'Foo\Bar'],
298+
['Foo', 'Foo\Bar', '\Foo\Foo\Bar'],
299+
];
297300
}
298301
}

0 commit comments

Comments
 (0)