Skip to content

Commit

Permalink
Strengthen test with data provider
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmccreary committed Mar 31, 2020
1 parent b76bf39 commit 0b06d99
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
17 changes: 9 additions & 8 deletions src/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Blueprint;

use Blueprint\Contracts\Lexer;
use Illuminate\Support\Str;
use Symfony\Component\Yaml\Yaml;
use Blueprint\Contracts\Generator;

Expand All @@ -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)
Expand Down
27 changes: 15 additions & 12 deletions tests/Feature/BlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
];
}
}

0 comments on commit 0b06d99

Please sign in to comment.