From 523dc5ce30c95873252f3c38496a3d847fec7fa9 Mon Sep 17 00:00:00 2001 From: Percy Mamedy Date: Thu, 16 Sep 2021 00:31:11 +0400 Subject: [PATCH 1/3] Laravel 8 support --- .styleci.yml | 3 -- .travis.yml | 10 ++--- README.md | 1 + composer.json | 12 +++--- tests/AbstractTestCase.php | 15 ------- .../Database/Factories/BarFactory.php | 29 +++++++++++++ .../Database/Factories/FooFactory.php | 28 +++++++++++++ .../Database/Factories/UserFactory.php | 33 +++++++++++++++ tests/Fixtures/Models/Bar.php | 15 +++++++ tests/Fixtures/Models/Foo.php | 15 +++++++ tests/Fixtures/Models/User.php | 15 +++++++ .../database/factories/ModelFactory.php | 41 ------------------- tests/TestRepository.php | 30 +++++++------- tests/TestRepositoryScopes.php | 8 ++-- 14 files changed, 167 insertions(+), 88 deletions(-) create mode 100644 tests/Fixtures/Database/Factories/BarFactory.php create mode 100644 tests/Fixtures/Database/Factories/FooFactory.php create mode 100644 tests/Fixtures/Database/Factories/UserFactory.php delete mode 100644 tests/Fixtures/database/factories/ModelFactory.php diff --git a/.styleci.yml b/.styleci.yml index 98dbe08..0285f17 100644 --- a/.styleci.yml +++ b/.styleci.yml @@ -1,4 +1 @@ preset: laravel - -disabled: - - simplified_null_return diff --git a/.travis.yml b/.travis.yml index d3c94d4..cbb31ee 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,14 +8,14 @@ env: matrix: fast_finish: true include: - - php: 7.2 - - php: 7.2 - env: SETUP=lowest - php: 7.3 - php: 7.3 env: SETUP=lowest - - php: 7.4snapshot - - php: 7.4snapshot + - php: 7.4 + - php: 7.4 + env: SETUP=lowest + - php: 8.0 + - php: 8.0 env: SETUP=lowest cache: diff --git a/README.md b/README.md index 0fb33f7..88ea9c8 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ Mango Repo is open-sourced software licensed under the [MIT license](http://open 5.8.x | 0.4.x 6.x | 1.x 7.x | 2.x + 8.x | 3.x ### Installation Install Mango Repo as you would with any other dependency managed by Composer: diff --git a/composer.json b/composer.json index 31fdf59..628c276 100644 --- a/composer.json +++ b/composer.json @@ -10,14 +10,14 @@ } ], "require": { - "php": "^7.2", - "illuminate/support": "^7.0", - "larachimp/pine-annotations": "^2.0" + "php": "^7.3|^8.0", + "illuminate/support": "^8.0", + "larachimp/pine-annotations": "^3.0" }, "require-dev": { - "mockery/mockery": "^1.3.1", - "orchestra/testbench": "^5.0", - "phpunit/phpunit": "^8.4|^9.0" + "mockery/mockery": "^1.4.3", + "orchestra/testbench": "^6.0", + "phpunit/phpunit": "^9.3.3" }, "autoload": { "psr-4": { diff --git a/tests/AbstractTestCase.php b/tests/AbstractTestCase.php index 9c718ac..355edb8 100644 --- a/tests/AbstractTestCase.php +++ b/tests/AbstractTestCase.php @@ -19,7 +19,6 @@ protected function setUp(): void { parent::setUp(); $this->createAppropriateTables(); - $this->withFactories(__DIR__.'/Fixtures/database/factories'); } /** @@ -48,20 +47,6 @@ protected function getPackageProviders($app) ]; } - /** - * Get package aliases. - * - * @param \Illuminate\Foundation\Application $app - * - * @return array - */ - protected function getPackageAliases($app) - { - return [ - 'Acme' => 'Acme\Facade', - ]; - } - /** * Define environment setup. * diff --git a/tests/Fixtures/Database/Factories/BarFactory.php b/tests/Fixtures/Database/Factories/BarFactory.php new file mode 100644 index 0000000..c1c9506 --- /dev/null +++ b/tests/Fixtures/Database/Factories/BarFactory.php @@ -0,0 +1,29 @@ + $this->faker->name, + 'is_active' => true, + ]; + } +} diff --git a/tests/Fixtures/Database/Factories/FooFactory.php b/tests/Fixtures/Database/Factories/FooFactory.php new file mode 100644 index 0000000..7b1f059 --- /dev/null +++ b/tests/Fixtures/Database/Factories/FooFactory.php @@ -0,0 +1,28 @@ + $this->faker->name, + ]; + } +} diff --git a/tests/Fixtures/Database/Factories/UserFactory.php b/tests/Fixtures/Database/Factories/UserFactory.php new file mode 100644 index 0000000..b37fcab --- /dev/null +++ b/tests/Fixtures/Database/Factories/UserFactory.php @@ -0,0 +1,33 @@ + $this->faker->name, + 'email' => $this->faker->unique()->safeEmail, + 'password' => bcrypt('secret'), + 'remember_token' => Str::random(10), + 'is_active' => true, + ]; + } +} diff --git a/tests/Fixtures/Models/Bar.php b/tests/Fixtures/Models/Bar.php index ef1b745..7f0e38a 100644 --- a/tests/Fixtures/Models/Bar.php +++ b/tests/Fixtures/Models/Bar.php @@ -2,11 +2,16 @@ namespace LaraChimp\MangoRepo\Tests\Fixtures\Models; +use Illuminate\Database\Eloquent\Factories\Factory; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use LaraChimp\MangoRepo\Tests\Fixtures\Database\Factories\BarFactory; use LaraChimp\MangoRepo\Tests\Fixtures\Models\Scopes\IsActive; class Bar extends Model { + use HasFactory; + /** * The attributes that are mass assignable. * @@ -28,4 +33,14 @@ protected static function boot() static::addGlobalScope(new IsActive()); } + + /** + * Create a new factory instance for the model. + * + * @return Factory + */ + protected static function newFactory() + { + return BarFactory::new(); + } } diff --git a/tests/Fixtures/Models/Foo.php b/tests/Fixtures/Models/Foo.php index 678b850..be2b14d 100644 --- a/tests/Fixtures/Models/Foo.php +++ b/tests/Fixtures/Models/Foo.php @@ -2,10 +2,15 @@ namespace LaraChimp\MangoRepo\Tests\Fixtures\Models; +use Illuminate\Database\Eloquent\Factories\Factory; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use LaraChimp\MangoRepo\Tests\Fixtures\Database\Factories\FooFactory; class Foo extends Model { + use HasFactory; + /** * The attributes that are mass assignable. * @@ -14,4 +19,14 @@ class Foo extends Model protected $fillable = [ 'name', ]; + + /** + * Create a new factory instance for the model. + * + * @return Factory + */ + protected static function newFactory() + { + return FooFactory::new(); + } } diff --git a/tests/Fixtures/Models/User.php b/tests/Fixtures/Models/User.php index af8092b..9783338 100644 --- a/tests/Fixtures/Models/User.php +++ b/tests/Fixtures/Models/User.php @@ -3,10 +3,15 @@ namespace LaraChimp\MangoRepo\Tests\Fixtures\Models; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Factories\Factory; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use LaraChimp\MangoRepo\Tests\Fixtures\Database\Factories\UserFactory; class User extends Model { + use HasFactory; + /** * The attributes that are mass assignable. * @@ -17,6 +22,16 @@ class User extends Model 'email', ]; + /** + * Create a new factory instance for the model. + * + * @return Factory + */ + protected static function newFactory() + { + return UserFactory::new(); + } + /** * Apply an is active scope filter to the model. * diff --git a/tests/Fixtures/database/factories/ModelFactory.php b/tests/Fixtures/database/factories/ModelFactory.php deleted file mode 100644 index 0de4243..0000000 --- a/tests/Fixtures/database/factories/ModelFactory.php +++ /dev/null @@ -1,41 +0,0 @@ -make(Illuminate\Database\Eloquent\Factory::class); - -$factory->define(LaraChimp\MangoRepo\Tests\Fixtures\Models\User::class, function (Faker\Generator $faker) { - static $password; - - return [ - 'name' => $faker->name, - 'email' => $faker->unique()->safeEmail, - 'password' => $password ?: $password = bcrypt('secret'), - 'remember_token' => Str::random(10), - 'is_active' => true, - ]; -}); - -$factory->define(LaraChimp\MangoRepo\Tests\Fixtures\Models\Foo::class, function (Faker\Generator $faker) { - return [ - 'name' => $faker->name, - ]; -}); - -$factory->define(LaraChimp\MangoRepo\Tests\Fixtures\Models\Bar::class, function (Faker\Generator $faker) { - return [ - 'name' => $faker->name, - 'is_active' => true, - ]; -}); diff --git a/tests/TestRepository.php b/tests/TestRepository.php index 751e4e3..2750593 100644 --- a/tests/TestRepository.php +++ b/tests/TestRepository.php @@ -3,6 +3,7 @@ namespace LaraChimp\MangoRepo\Tests; use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Foundation\Testing\DatabaseTransactions; use LaraChimp\MangoRepo\Tests\Fixtures\Models\Foo; use LaraChimp\MangoRepo\Tests\Fixtures\Models\User; @@ -45,11 +46,12 @@ public function testRepositoriesWithTargetConst() */ public function testAllMethod() { - factory(User::class)->create([ + User::factory()->create([ 'email' => 'hello@larachimp.com', 'name' => 'User 1', ]); - factory(User::class)->create([ + + User::factory()->create([ 'email' => 'hello2@larachimp.com', 'name' => 'User 2', ]); @@ -80,7 +82,7 @@ public function testAllMethod() */ public function testPaginateMethod() { - factory(Foo::class, 20)->create(); + Foo::factory(20)->create(); $foos = $this->app->make(FooRepository::class); $foosPaginated = $foos->paginate(10); @@ -96,7 +98,7 @@ public function testPaginateMethod() */ public function testSimplePaginateMethod() { - factory(Foo::class, 20)->create(); + Foo::factory(20)->create(); $foos = $this->app->make(FooRepository::class); $foosPaginated = $foos->simplePaginate(10); @@ -131,7 +133,7 @@ public function testCreateMethod() */ public function testUpdateMethod() { - $user = factory(User::class)->create([ + $user = User::factory()->create([ 'name' => 'SomeUser', 'email' => 'some@email.com', ]); @@ -155,14 +157,14 @@ public function testUpdateMethod() */ public function testDeleteMethod() { - $this->expectException(\Illuminate\Database\Eloquent\ModelNotFoundException::class); + $this->expectException(ModelNotFoundException::class); - $user1 = factory(User::class)->create([ + $user1 = User::factory()->create([ 'name' => 'User 1', 'email' => 'hello@larachimp.com', ]); - $user2 = factory(User::class)->create([ + $user2 = User::factory()->create([ 'name' => 'User 2', 'email' => 'helloAgain@larachimp.com', ]); @@ -182,7 +184,7 @@ public function testDeleteMethod() */ public function testFindMethod() { - $user1 = factory(User::class)->create([ + $user1 = User::factory()->create([ 'name' => 'User 1', 'email' => 'hello@larachimp.com', ]); @@ -206,9 +208,9 @@ public function testFindMethod() */ public function testFindOrFailMethod() { - $this->expectException(\Illuminate\Database\Eloquent\ModelNotFoundException::class); + $this->expectException(ModelNotFoundException::class); - $user1 = factory(User::class)->create([ + $user1 = User::factory()->create([ 'name' => 'User 1', 'email' => 'hello@larachimp.com', ]); @@ -231,15 +233,15 @@ public function testFindOrFailMethod() */ public function testFindByMethod() { - factory(Foo::class, 3)->create([ + Foo::factory(3)->create([ 'name' => 'FooBar', ]); - factory(Foo::class, 4)->create([ + Foo::factory(4)->create([ 'name' => 'Baz', ]); - factory(Foo::class, 2)->create([ + Foo::factory(2)->create([ 'name' => 'Bin', ]); diff --git a/tests/TestRepositoryScopes.php b/tests/TestRepositoryScopes.php index 1ae7546..b544b6b 100644 --- a/tests/TestRepositoryScopes.php +++ b/tests/TestRepositoryScopes.php @@ -21,8 +21,8 @@ class TestRepositoryScopes extends AbstractTestCase */ public function testLocalScopeOnModelIsAppliedWhenQueringByRepository() { - factory(User::class, 5)->create(); - factory(User::class, 3)->create([ + User::factory(5)->create(); + User::factory(3)->create([ 'is_active' => false, ]); @@ -40,8 +40,8 @@ public function testLocalScopeOnModelIsAppliedWhenQueringByRepository() */ public function testGlobalScopeOnModelIsAppliedWhenQueringByRepository() { - factory(Bar::class, 5)->create(); - factory(Bar::class, 3)->create([ + Bar::factory(5)->create(); + Bar::factory(3)->create([ 'is_active' => false, ]); From 810a1a09b5b2f4e58d01c0603185492d2672828b Mon Sep 17 00:00:00 2001 From: Percy Mamedy Date: Thu, 16 Sep 2021 00:32:51 +0400 Subject: [PATCH 2/3] Fix styles --- tests/AbstractTestCase.php | 6 ++---- tests/Fixtures/Database/Factories/UserFactory.php | 2 +- tests/Fixtures/Models/User.php | 3 +-- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/tests/AbstractTestCase.php b/tests/AbstractTestCase.php index 355edb8..83c91e6 100644 --- a/tests/AbstractTestCase.php +++ b/tests/AbstractTestCase.php @@ -35,8 +35,7 @@ protected function tearDown(): void /** * Get package providers. * - * @param \Illuminate\Foundation\Application $app - * + * @param \Illuminate\Foundation\Application $app * @return array */ protected function getPackageProviders($app) @@ -50,8 +49,7 @@ protected function getPackageProviders($app) /** * Define environment setup. * - * @param \Illuminate\Foundation\Application $app - * + * @param \Illuminate\Foundation\Application $app * @return void */ protected function getEnvironmentSetUp($app) diff --git a/tests/Fixtures/Database/Factories/UserFactory.php b/tests/Fixtures/Database/Factories/UserFactory.php index b37fcab..4ade316 100644 --- a/tests/Fixtures/Database/Factories/UserFactory.php +++ b/tests/Fixtures/Database/Factories/UserFactory.php @@ -3,8 +3,8 @@ namespace LaraChimp\MangoRepo\Tests\Fixtures\Database\Factories; use Illuminate\Database\Eloquent\Factories\Factory; -use LaraChimp\MangoRepo\Tests\Fixtures\Models\User; use Illuminate\Support\Str; +use LaraChimp\MangoRepo\Tests\Fixtures\Models\User; class UserFactory extends Factory { diff --git a/tests/Fixtures/Models/User.php b/tests/Fixtures/Models/User.php index 9783338..2b146fa 100644 --- a/tests/Fixtures/Models/User.php +++ b/tests/Fixtures/Models/User.php @@ -35,8 +35,7 @@ protected static function newFactory() /** * Apply an is active scope filter to the model. * - * @param Builder $query - * + * @param Builder $query * @return Builder */ public function scopeIsActive($query) From 7c709de425d6cae04b7544ff61d484762a1253fe Mon Sep 17 00:00:00 2001 From: Percy Mamedy Date: Thu, 16 Sep 2021 00:33:47 +0400 Subject: [PATCH 3/3] Fix styles --- src/Annotations/EloquentModel.php | 1 + src/Concerns/IsRepositorable.php | 21 ++++++++------------ src/Concerns/IsRepositoryBootable.php | 3 +-- src/Concerns/IsRepositoryScopable.php | 5 ++--- src/Console/MakeCommand.php | 5 ++--- src/Contracts/RepositoryInterface.php | 15 ++++++-------- src/Exceptions/InvalidModelException.php | 6 +++--- src/Exceptions/UnspecifiedModelException.php | 6 +++--- tests/Fixtures/Models/Scopes/IsActive.php | 5 ++--- tests/TestMakeCommand.php | 3 +-- 10 files changed, 29 insertions(+), 41 deletions(-) diff --git a/src/Annotations/EloquentModel.php b/src/Annotations/EloquentModel.php index 8d4e487..9833d1e 100644 --- a/src/Annotations/EloquentModel.php +++ b/src/Annotations/EloquentModel.php @@ -16,6 +16,7 @@ class EloquentModel * use for the repository. * * @Required + * * @var string */ public $target; diff --git a/src/Concerns/IsRepositorable.php b/src/Concerns/IsRepositorable.php index e9955c3..df55ff3 100644 --- a/src/Concerns/IsRepositorable.php +++ b/src/Concerns/IsRepositorable.php @@ -16,8 +16,7 @@ trait IsRepositorable /** * Sets the Model to the Repo. * - * @param Model $model - * + * @param Model $model * @return self */ public function setModel(Model $model) @@ -40,10 +39,9 @@ public function getModel() /** * Update a Model in the database. * - * @param array $values - * @param \Illuminate\Database\Eloquent\Model|mixed $idOrModel - * @param array $options - * + * @param array $values + * @param \Illuminate\Database\Eloquent\Model|mixed $idOrModel + * @param array $options * @return bool */ public function update(array $values, $idOrModel, array $options = []) @@ -58,8 +56,7 @@ public function update(array $values, $idOrModel, array $options = []) /** * Delete a record from the database. * - * @param \Illuminate\Database\Eloquent\Model|mixed $idOrModel - * + * @param \Illuminate\Database\Eloquent\Model|mixed $idOrModel * @return bool|null * * @throws \Exception @@ -76,9 +73,8 @@ public function delete($idOrModel) /** * Find a Model or Models Using some criteria. * - * @param array $criteria - * @param array $columns - * + * @param array $criteria + * @param array $columns * @return \Illuminate\Database\Eloquent\Collection|null */ public function findBy($criteria = [], $columns = ['*']) @@ -101,8 +97,7 @@ public function findBy($criteria = [], $columns = ['*']) /** * Return response from repository. * - * @param mixed $value - * + * @param mixed $value * @return mixed */ protected function response($value) diff --git a/src/Concerns/IsRepositoryBootable.php b/src/Concerns/IsRepositoryBootable.php index 4039443..dd1e68c 100644 --- a/src/Concerns/IsRepositoryBootable.php +++ b/src/Concerns/IsRepositoryBootable.php @@ -77,8 +77,7 @@ protected function getEloquentModelByAnnotation() /** * Find the first Eloquent Model specified by the annotations. * - * @param Collection $annotations - * + * @param Collection $annotations * @return Model */ protected function findFirstEloquentModel(Collection $annotations) diff --git a/src/Concerns/IsRepositoryScopable.php b/src/Concerns/IsRepositoryScopable.php index ecf51d3..061337c 100644 --- a/src/Concerns/IsRepositoryScopable.php +++ b/src/Concerns/IsRepositoryScopable.php @@ -8,9 +8,8 @@ trait IsRepositoryScopable * Whenever a function is called we try to * scope it to the model. * - * @param string $name - * @param mixed $arguments - * + * @param string $name + * @param mixed $arguments * @return $this */ public function __call($name, $arguments) diff --git a/src/Console/MakeCommand.php b/src/Console/MakeCommand.php index 88f7ade..e30affc 100644 --- a/src/Console/MakeCommand.php +++ b/src/Console/MakeCommand.php @@ -49,9 +49,8 @@ protected function getStub() /** * Replace the namespace for the given stub. * - * @param string $stub - * @param string $name - * + * @param string $stub + * @param string $name * @return $this */ protected function replaceNamespace(&$stub, $name) diff --git a/src/Contracts/RepositoryInterface.php b/src/Contracts/RepositoryInterface.php index faf7b4c..3db9d86 100644 --- a/src/Contracts/RepositoryInterface.php +++ b/src/Contracts/RepositoryInterface.php @@ -7,10 +7,9 @@ interface RepositoryInterface /** * Update a Model in the database. * - * @param array $values - * @param \Illuminate\Database\Eloquent\Model|mixed $idOrModel - * @param array $options - * + * @param array $values + * @param \Illuminate\Database\Eloquent\Model|mixed $idOrModel + * @param array $options * @return bool */ public function update(array $values, $idOrModel, array $options = []); @@ -18,8 +17,7 @@ public function update(array $values, $idOrModel, array $options = []); /** * Delete a record from the database. * - * @param \Illuminate\Database\Eloquent\Model|mixed $idOrModel - * + * @param \Illuminate\Database\Eloquent\Model|mixed $idOrModel * @return mixed */ public function delete($idOrModel); @@ -27,9 +25,8 @@ public function delete($idOrModel); /** * Find a Model or Models Using some criteria. * - * @param array $criteria - * @param array $columns - * + * @param array $criteria + * @param array $columns * @return \Illuminate\Database\Eloquent\Collection|null */ public function findBy($criteria = [], $columns = ['*']); diff --git a/src/Exceptions/InvalidModelException.php b/src/Exceptions/InvalidModelException.php index 1a44a1d..5b812cf 100644 --- a/src/Exceptions/InvalidModelException.php +++ b/src/Exceptions/InvalidModelException.php @@ -16,9 +16,9 @@ class InvalidModelException extends \RuntimeException /** * InvalidModelException constructor. * - * @param string $message - * @param int $code - * @param Exception|null $previous + * @param string $message + * @param int $code + * @param Exception|null $previous */ public function __construct($message = '', $code = 0, Exception $previous = null) { diff --git a/src/Exceptions/UnspecifiedModelException.php b/src/Exceptions/UnspecifiedModelException.php index 1be5df3..f83b69f 100644 --- a/src/Exceptions/UnspecifiedModelException.php +++ b/src/Exceptions/UnspecifiedModelException.php @@ -16,9 +16,9 @@ class UnspecifiedModelException extends \RuntimeException /** * UnspecifiedModelException constructor. * - * @param string $message - * @param int $code - * @param Exception|null $previous + * @param string $message + * @param int $code + * @param Exception|null $previous */ public function __construct($message = '', $code = 0, Exception $previous = null) { diff --git a/tests/Fixtures/Models/Scopes/IsActive.php b/tests/Fixtures/Models/Scopes/IsActive.php index 57bfd71..1345adb 100644 --- a/tests/Fixtures/Models/Scopes/IsActive.php +++ b/tests/Fixtures/Models/Scopes/IsActive.php @@ -11,9 +11,8 @@ class IsActive implements Scope /** * Apply the scope to a given Eloquent query builder. * - * @param \Illuminate\Database\Eloquent\Builder $builder - * @param \Illuminate\Database\Eloquent\Model $model - * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @param \Illuminate\Database\Eloquent\Model $model * @return void */ public function apply(Builder $builder, Model $model) diff --git a/tests/TestMakeCommand.php b/tests/TestMakeCommand.php index 1101ea0..8ca684a 100644 --- a/tests/TestMakeCommand.php +++ b/tests/TestMakeCommand.php @@ -19,8 +19,7 @@ protected function tearDown(): void /** * Define environment setup. * - * @param \Illuminate\Foundation\Application $app - * + * @param \Illuminate\Foundation\Application $app * @return void */ protected function getEnvironmentSetUp($app)