diff --git a/.gitignore b/.gitignore index d554f36..df695e4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /vendor +/coverage composer.phar composer.lock .DS_Store @@ -8,3 +9,4 @@ composer.lock laravel-doctrine-orm.iml .phpcs-cache .phpunit.cache + diff --git a/composer.json b/composer.json index 057c75f..28fcabd 100644 --- a/composer.json +++ b/composer.json @@ -37,7 +37,8 @@ "mockery/mockery": "^1.6", "php-parallel-lint/php-parallel-lint": "^1.4", "phpstan/phpstan": "^1.12", - "phpunit/phpunit": "^11.4" + "phpunit/phpunit": "^11.4", + "orchestra/testbench": "^9.5" }, "suggest": { "gedmo/doctrine-extensions": "Behavioral Doctrine2 extensions", @@ -50,7 +51,10 @@ }, "autoload-dev": { "psr-4": { - "LaravelDoctrineTest\\Extensions\\": "tests/" + "LaravelDoctrineTest\\Extensions\\": "tests/", + "Workbench\\App\\": "workbench/app/", + "Workbench\\Database\\Factories\\": "workbench/database/factories/", + "Workbench\\Database\\Seeders\\": "workbench/database/seeders/" } }, "scripts": { @@ -60,11 +64,26 @@ "vendor/bin/phpunit", "vendor/bin/phpstan analyze src --level 1" ], - "coverage": "XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html=coverage" + "coverage": "XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html=coverage", + "post-autoload-dump": [ + "@clear", + "@prepare" + ], + "clear": "@php vendor/bin/testbench package:purge-skeleton --ansi", + "prepare": "@php vendor/bin/testbench package:discover --ansi", + "build": "@php vendor/bin/testbench workbench:build --ansi", + "serve": [ + "Composer\\Config::disableProcessTimeout", + "@build", + "@php vendor/bin/testbench serve --ansi" + ], + "lint": [ + "@php vendor/bin/phpstan analyse --verbose --ansi" + ] }, "config": { "allow-plugins": { "dealerdirect/phpcodesniffer-composer-installer": true } } -} +} \ No newline at end of file diff --git a/src/BeberleiExtensionsServiceProvider.php b/src/BeberleiExtensionsServiceProvider.php index 953fb8b..03909c2 100644 --- a/src/BeberleiExtensionsServiceProvider.php +++ b/src/BeberleiExtensionsServiceProvider.php @@ -94,11 +94,4 @@ public function boot(DoctrineManager $manager): void $configuration->setCustomStringFunctions($this->string); }); } - - /** - * Register the service provider. - */ - public function register(): void - { - } } diff --git a/src/GedmoExtensionsServiceProvider.php b/src/GedmoExtensionsServiceProvider.php index ff6a476..bc259aa 100644 --- a/src/GedmoExtensionsServiceProvider.php +++ b/src/GedmoExtensionsServiceProvider.php @@ -4,6 +4,7 @@ namespace LaravelDoctrine\Extensions; +use Gedmo\DoctrineExtensions; use Illuminate\Support\ServiceProvider; class GedmoExtensionsServiceProvider extends ServiceProvider @@ -18,6 +19,12 @@ public function register(): void foreach ($registry->getManagers() as $manager) { $chain = $manager->getConfiguration()->getMetadataDriverImpl(); + + if ($this->needsAllMappings()) { + DoctrineExtensions::registerMappingIntoDriverChainORM($chain); + } else { + DoctrineExtensions::registerAbstractMappingIntoDriverChainORM($chain); + } } }); } diff --git a/testbench.yaml b/testbench.yaml new file mode 100644 index 0000000..18db9dd --- /dev/null +++ b/testbench.yaml @@ -0,0 +1,18 @@ +providers: + - LaravelDoctrine\ORM\DoctrineServiceProvider + - LaravelDoctrine\Extensions\BeberleiExtensionsServiceProvider + - LaravelDoctrine\Extensions\GedmoExtensionsServiceProvider + +workbench: + start: '/' + install: true + health: false + discovers: + web: false + api: false + commands: false + components: false + views: false + assets: + - laravel-assets + sync: [] diff --git a/tests/Feature/BeberleiExtensionsServiceProviderTest.php b/tests/Feature/BeberleiExtensionsServiceProviderTest.php index e0ec011..3c57747 100644 --- a/tests/Feature/BeberleiExtensionsServiceProviderTest.php +++ b/tests/Feature/BeberleiExtensionsServiceProviderTest.php @@ -4,35 +4,18 @@ namespace LaravelDoctrineTest\Extensions\Feature; -use Illuminate\Contracts\Foundation\Application; -use LaravelDoctrine\Extensions\BeberleiExtensionsServiceProvider; use LaravelDoctrine\ORM\DoctrineManager; -use Mockery as m; -use PHPUnit\Framework\TestCase; + +use function app; class BeberleiExtensionsServiceProviderTest extends TestCase { - protected Application $app; - protected BeberleiExtensionsServiceProvider $provider; - protected DoctrineManager $manager; - - public function setUp(): void + public function testCustomFunctionsCanBeRegistered(): void { - $this->app = m::mock(Application::class); - $this->manager = m::mock(DoctrineManager::class); + $doctrineManager = $this->app->get(DoctrineManager::class); - $this->provider = new BeberleiExtensionsServiceProvider($this->app); + $entityManager = app('em'); - // silence the 'This test did not perform any assertions' warning $this->assertTrue(true); - - parent::setUp(); - } - - public function testCustomFunctionsCanBeRegistered(): void - { - $this->manager->shouldReceive('extendAll')->once(); - - $this->provider->boot($this->manager); } } diff --git a/tests/Feature/Blameable/BlameableExtensionTest.php b/tests/Feature/Blameable/BlameableExtensionTest.php index 2db6202..b56d8b8 100644 --- a/tests/Feature/Blameable/BlameableExtensionTest.php +++ b/tests/Feature/Blameable/BlameableExtensionTest.php @@ -13,10 +13,8 @@ class BlameableExtensionTest extends TestCase { public function testCanRegisterExtension(): void { - $guard = m::mock(Guard::class); - $extension = new BlameableExtension( - $guard, + m::mock(Guard::class), ); $extension->addSubscribers( diff --git a/tests/Feature/GedmoExtensionsServiceProviderAllMappingsTest.php b/tests/Feature/GedmoExtensionsServiceProviderAllMappingsTest.php new file mode 100644 index 0000000..c303028 --- /dev/null +++ b/tests/Feature/GedmoExtensionsServiceProviderAllMappingsTest.php @@ -0,0 +1,27 @@ +set('doctrine.gedmo.all_mappings', true); + }); + } + + public function testCustomExtensionsCanBeRegistered(): void + { + $this->app['events']->dispatch('doctrine.extensions.booting'); + + $this->assertTrue(true); + } +} diff --git a/tests/Feature/GedmoExtensionsServiceProviderTest.php b/tests/Feature/GedmoExtensionsServiceProviderTest.php new file mode 100644 index 0000000..65c4423 --- /dev/null +++ b/tests/Feature/GedmoExtensionsServiceProviderTest.php @@ -0,0 +1,15 @@ +app['events']->dispatch('doctrine.extensions.booting'); + + $this->assertTrue(true); + } +} diff --git a/tests/Feature/TestCase.php b/tests/Feature/TestCase.php index f9151c4..7d47386 100644 --- a/tests/Feature/TestCase.php +++ b/tests/Feature/TestCase.php @@ -7,10 +7,16 @@ use Doctrine\Common\EventManager; use Doctrine\ORM\EntityManagerInterface; use Mockery as m; -use PHPUnit\Framework\TestCase as PHPUnitTestCase; +use Orchestra\Testbench\Concerns\WithWorkbench; +use Orchestra\Testbench\TestCase as OrchestraTestCase; -abstract class TestCase extends PHPUnitTestCase +use function restore_error_handler; +use function restore_exception_handler; + +abstract class TestCase extends OrchestraTestCase { + use WithWorkbench; + protected EventManager $evm; protected EntityManagerInterface $em; @@ -19,11 +25,18 @@ public function setUp(): void $this->evm = m::mock(EventManager::class); $this->em = m::mock(EntityManagerInterface::class); - $this->evm->shouldReceive('addEventSubscriber')->once(); + $this->evm->shouldReceive('addEventSubscriber'); + + parent::setUp(); } public function tearDown(): void { + restore_error_handler(); + restore_exception_handler(); + m::close(); + + parent::tearDown(); } } diff --git a/tests/Feature/Uploadable/UploadableExtensionTest.php b/tests/Feature/Uploadable/UploadableExtensionTest.php index 879b698..cb06d5a 100644 --- a/tests/Feature/Uploadable/UploadableExtensionTest.php +++ b/tests/Feature/Uploadable/UploadableExtensionTest.php @@ -6,6 +6,7 @@ use Gedmo\Uploadable\UploadableListener; use LaravelDoctrine\Extensions\Uploadable\UploadableExtension; +use LaravelDoctrine\Extensions\Uploadable\UploadableFacade; use LaravelDoctrineTest\Extensions\Feature\TestCase; use Mockery as m; @@ -13,6 +14,8 @@ class UploadableExtensionTest extends TestCase { public function testCanRegisterExtension(): void { + $this->assertFalse(UploadableFacade::isFake()); + $listener = m::mock(UploadableListener::class); $extension = new UploadableExtension( diff --git a/workbench/app/Models/.gitkeep b/workbench/app/Models/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/workbench/app/Models/User.php b/workbench/app/Models/User.php new file mode 100644 index 0000000..1405251 --- /dev/null +++ b/workbench/app/Models/User.php @@ -0,0 +1,45 @@ + + */ + protected $fillable = [ + 'name', + 'email', + 'password', + ]; + + /** + * The attributes that should be hidden for serialization. + * + * @var array + */ + protected $hidden = [ + 'password', + 'remember_token', + ]; + + /** + * The attributes that should be cast. + * + * @var array + */ + protected $casts = [ + 'email_verified_at' => 'datetime', + 'password' => 'hashed', + ]; +} diff --git a/workbench/app/Providers/WorkbenchServiceProvider.php b/workbench/app/Providers/WorkbenchServiceProvider.php new file mode 100644 index 0000000..e8cec9c --- /dev/null +++ b/workbench/app/Providers/WorkbenchServiceProvider.php @@ -0,0 +1,24 @@ +withRouting( + web: __DIR__.'/../routes/web.php', + commands: __DIR__.'/../routes/console.php', + ) + ->withMiddleware(function (Middleware $middleware) { + // + }) + ->withExceptions(function (Exceptions $exceptions) { + // + })->create(); diff --git a/workbench/bootstrap/providers.php b/workbench/bootstrap/providers.php new file mode 100644 index 0000000..3ac44ad --- /dev/null +++ b/workbench/bootstrap/providers.php @@ -0,0 +1,5 @@ + + */ +class UserFactory extends Factory +{ + /** + * The current password being used by the factory. + */ + protected static ?string $password; + + /** + * The name of the factory's corresponding model. + * + * @var class-string + */ + protected $model = User::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'name' => fake()->name(), + 'email' => fake()->unique()->safeEmail(), + 'email_verified_at' => now(), + 'password' => static::$password ??= Hash::make('password'), + 'remember_token' => Str::random(10), + ]; + } + + /** + * Indicate that the model's email address should be unverified. + */ + public function unverified(): static + { + return $this->state(fn (array $attributes) => [ + 'email_verified_at' => null, + ]); + } +} diff --git a/workbench/database/migrations/.gitkeep b/workbench/database/migrations/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/workbench/database/seeders/DatabaseSeeder.php b/workbench/database/seeders/DatabaseSeeder.php new file mode 100644 index 0000000..ce9bd15 --- /dev/null +++ b/workbench/database/seeders/DatabaseSeeder.php @@ -0,0 +1,23 @@ +create(); + + UserFactory::new()->create([ + 'name' => 'Test User', + 'email' => 'test@example.com', + ]); + } +} diff --git a/workbench/resources/views/.gitkeep b/workbench/resources/views/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/workbench/routes/console.php b/workbench/routes/console.php new file mode 100644 index 0000000..eff2ed2 --- /dev/null +++ b/workbench/routes/console.php @@ -0,0 +1,8 @@ +comment(Inspiring::quote()); +})->purpose('Display an inspiring quote')->hourly(); diff --git a/workbench/routes/web.php b/workbench/routes/web.php new file mode 100644 index 0000000..86a06c5 --- /dev/null +++ b/workbench/routes/web.php @@ -0,0 +1,7 @@ +