-
Notifications
You must be signed in to change notification settings - Fork 6
Discover providers from the project itself. #84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| <?php | ||
|
|
||
| namespace Xefi\Faker\Tests\Support\Concerns; | ||
|
|
||
| trait CreatesTemporaryProjects | ||
| { | ||
| /** | ||
| * The temporary project paths created during the test. | ||
| * | ||
| * @var array | ||
| */ | ||
| private array $temporaryProjectPaths = []; | ||
|
|
||
| /** | ||
| * Create a temporary project directory holding an installed.json and, | ||
| * when given, a root composer.json. | ||
| * | ||
| * @param ?array $composer | ||
| * @param array $installedPackages | ||
| * | ||
| * @return string | ||
| */ | ||
| private function createTemporaryProject(?array $composer = null, array $installedPackages = []): string | ||
| { | ||
| $projectPath = sys_get_temp_dir().'/faker-php-'.uniqid(); | ||
|
|
||
| mkdir($projectPath.'/vendor/composer', 0777, true); | ||
|
|
||
| file_put_contents( | ||
| $projectPath.'/vendor/composer/installed.json', | ||
| json_encode(['packages' => $installedPackages]) | ||
| ); | ||
|
|
||
| if ($composer !== null) { | ||
| file_put_contents($projectPath.'/composer.json', json_encode($composer)); | ||
| } | ||
|
|
||
| $this->temporaryProjectPaths[] = $projectPath; | ||
|
|
||
| return $projectPath; | ||
| } | ||
|
|
||
| /** | ||
| * Remove every temporary project created during the test. | ||
| * | ||
| * @return void | ||
| */ | ||
| private function deleteTemporaryProjects(): void | ||
| { | ||
| foreach ($this->temporaryProjectPaths as $projectPath) { | ||
| $files = new \RecursiveIteratorIterator( | ||
| new \RecursiveDirectoryIterator($projectPath, \FilesystemIterator::SKIP_DOTS), | ||
| \RecursiveIteratorIterator::CHILD_FIRST | ||
| ); | ||
|
|
||
| foreach ($files as $file) { | ||
| $file->isDir() ? rmdir($file->getPathname()) : unlink($file->getPathname()); | ||
| } | ||
|
|
||
| rmdir($projectPath); | ||
| } | ||
|
|
||
| $this->temporaryProjectPaths = []; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "name": "xefi/faker-php-tests-support" | ||
| } | ||
|
GautierDele marked this conversation as resolved.
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,9 +5,20 @@ | |
| namespace Xefi\Faker\Tests\Unit; | ||
|
|
||
| use Xefi\Faker\Manifests\PackageManifest; | ||
| use Xefi\Faker\Tests\Support\Concerns\CreatesTemporaryProjects; | ||
| use Xefi\Faker\Tests\Support\TestServiceProvider; | ||
|
|
||
| final class PackageManifestTest extends TestCase | ||
| { | ||
| use CreatesTemporaryProjects; | ||
|
|
||
| protected function tearDown(): void | ||
| { | ||
| $this->deleteTemporaryProjects(); | ||
|
|
||
| parent::tearDown(); | ||
| } | ||
|
|
||
| public function testAssetLoading() | ||
| { | ||
| @unlink('/tmp/packages.php'); | ||
|
|
@@ -41,4 +52,196 @@ public function testShouldRecompile() | |
|
|
||
| unlink('/tmp/packages.php'); | ||
| } | ||
|
|
||
| public function testProjectProvidersAreDiscovered() | ||
| { | ||
| $projectPath = $this->createTemporaryProject([ | ||
| 'name' => 'xefi/my-project', | ||
| 'extra' => [ | ||
| 'faker' => [ | ||
| 'providers' => [TestServiceProvider::class], | ||
| ], | ||
| ], | ||
| ]); | ||
|
|
||
| $manifest = new PackageManifest($projectPath, $projectPath.'/packages.php'); | ||
|
|
||
| $this->assertEquals( | ||
| [ | ||
| 'xefi/my-project' => [TestServiceProvider::class], | ||
| ], | ||
| $manifest->providers() | ||
| ); | ||
| } | ||
|
|
||
| public function testProjectProvidersAreMergedWithTheInstalledPackagesOnes() | ||
| { | ||
| $projectPath = $this->createTemporaryProject( | ||
| [ | ||
| 'name' => 'xefi/my-project', | ||
| 'extra' => [ | ||
| 'faker' => [ | ||
| 'providers' => ['Xefi\Faker\Tests\Support\ProjectServiceProvider'], | ||
| ], | ||
| ], | ||
| ], | ||
| [ | ||
| [ | ||
| 'name' => 'xefi/faker-number', | ||
| 'extra' => [ | ||
| 'faker' => [ | ||
| 'providers' => [TestServiceProvider::class], | ||
| ], | ||
| ], | ||
| ], | ||
| ] | ||
| ); | ||
|
|
||
| $manifest = new PackageManifest($projectPath, $projectPath.'/packages.php'); | ||
|
|
||
| $this->assertEquals( | ||
| [ | ||
| 'xefi/faker-number' => [TestServiceProvider::class], | ||
| 'xefi/my-project' => ['Xefi\Faker\Tests\Support\ProjectServiceProvider'], | ||
| ], | ||
| $manifest->providers() | ||
| ); | ||
| } | ||
|
|
||
| public function testProjectProvidersAreKeyedByRootWhenTheProjectHasNoName() | ||
| { | ||
| $projectPath = $this->createTemporaryProject([ | ||
| 'extra' => [ | ||
| 'faker' => [ | ||
| 'providers' => [TestServiceProvider::class], | ||
| ], | ||
| ], | ||
| ]); | ||
|
|
||
| $manifest = new PackageManifest($projectPath, $projectPath.'/packages.php'); | ||
|
|
||
| $this->assertEquals( | ||
| [ | ||
| 'root' => [TestServiceProvider::class], | ||
| ], | ||
| $manifest->providers() | ||
| ); | ||
| } | ||
|
|
||
| public function testProjectWithoutFakerConfigurationIsIgnored() | ||
| { | ||
| $projectPath = $this->createTemporaryProject( | ||
| [ | ||
| 'name' => 'xefi/my-project', | ||
| 'extra' => [ | ||
| 'branch-alias' => ['dev-master' => '2.0.x-dev'], | ||
| ], | ||
| ], | ||
| [ | ||
| [ | ||
| 'name' => 'xefi/faker-number', | ||
| 'extra' => [ | ||
| 'faker' => [ | ||
| 'providers' => [TestServiceProvider::class], | ||
| ], | ||
| ], | ||
| ], | ||
| ] | ||
| ); | ||
|
|
||
| $manifest = new PackageManifest($projectPath, $projectPath.'/packages.php'); | ||
|
|
||
| $this->assertEquals( | ||
| [ | ||
| 'xefi/faker-number' => [TestServiceProvider::class], | ||
| ], | ||
| $manifest->providers() | ||
| ); | ||
| } | ||
|
|
||
| public function testProjectTakesPrecedenceOverAnInstalledPackageOfTheSameName() | ||
| { | ||
| $projectPath = $this->createTemporaryProject( | ||
| [ | ||
| 'name' => 'xefi/faker-number', | ||
| 'extra' => [ | ||
| 'faker' => [ | ||
| 'providers' => ['Xefi\Faker\Tests\Support\ProjectServiceProvider'], | ||
| ], | ||
| ], | ||
| ], | ||
| [ | ||
| [ | ||
| 'name' => 'xefi/faker-number', | ||
| 'extra' => [ | ||
| 'faker' => [ | ||
| 'providers' => [TestServiceProvider::class], | ||
| ], | ||
| ], | ||
| ], | ||
| ] | ||
| ); | ||
|
|
||
| $manifest = new PackageManifest($projectPath, $projectPath.'/packages.php'); | ||
|
|
||
| $this->assertEquals( | ||
| [ | ||
| 'xefi/faker-number' => ['Xefi\Faker\Tests\Support\ProjectServiceProvider'], | ||
| ], | ||
| $manifest->providers() | ||
| ); | ||
| } | ||
|
|
||
| public function testProjectWithoutComposerFileIsIgnored() | ||
| { | ||
| $projectPath = $this->createTemporaryProject(null, [ | ||
| [ | ||
| 'name' => 'xefi/faker-number', | ||
| 'extra' => [ | ||
| 'faker' => [ | ||
| 'providers' => [TestServiceProvider::class], | ||
| ], | ||
| ], | ||
| ], | ||
| ]); | ||
|
|
||
| $manifest = new PackageManifest($projectPath, $projectPath.'/packages.php'); | ||
| $manifest->build(); | ||
|
|
||
| $this->assertEquals( | ||
| [ | ||
| 'xefi/faker-number' => [ | ||
| 'providers' => [TestServiceProvider::class], | ||
| ], | ||
| ], | ||
| require $projectPath.'/packages.php' | ||
| ); | ||
| } | ||
|
Comment on lines
+195
to
+219
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win Handle a missing root This test declares that a project without Guard that timestamp check with 🧰 Tools🪛 OpenGrep (1.26.0)[ERROR] 217-217: Dynamic file path passed to include/require. This can lead to local or remote file inclusion. Use a fixed allowlist of paths. (coderabbit.file-inclusion.php-dynamic-include) 🤖 Prompt for AI Agents |
||
|
|
||
| public function testShouldRecompileWhenTheProjectComposerFileChanged() | ||
| { | ||
| $projectPath = $this->createTemporaryProject([ | ||
| 'name' => 'xefi/my-project', | ||
| 'extra' => [ | ||
| 'faker' => [ | ||
| 'providers' => [TestServiceProvider::class], | ||
| ], | ||
| ], | ||
| ]); | ||
|
|
||
| $manifest = new PackageManifest($projectPath, $projectPath.'/packages.php'); | ||
| $manifest->build(); | ||
| touch($projectPath.'/vendor/composer/installed.json', time() - 1); | ||
| touch($projectPath.'/composer.json', time() - 1); | ||
|
|
||
| $this->assertFalse($manifest->shouldRecompile()); | ||
|
|
||
| // Test on current time | ||
| touch($projectPath.'/composer.json'); | ||
| $this->assertTrue($manifest->shouldRecompile()); | ||
|
|
||
| // Test on future | ||
| touch($projectPath.'/composer.json', time() + 1); | ||
| $this->assertTrue($manifest->shouldRecompile()); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.