diff --git a/src/Exceptions/Valravn/ValravnErrorCode.php b/src/Exceptions/Valravn/ValravnErrorCode.php index 1c9ffb8..0eeae8e 100644 --- a/src/Exceptions/Valravn/ValravnErrorCode.php +++ b/src/Exceptions/Valravn/ValravnErrorCode.php @@ -7,6 +7,6 @@ class ValravnErrorCode extends ErrorCode { protected static string $prefix = 'ValravnECx'; - protected int $FAILED_TO_EXECUTE_DELETING_HOOK = 1; + protected int $failedToDelete = 1; } \ No newline at end of file diff --git a/src/Exceptions/Valravn/ValravnException.php b/src/Exceptions/Valravn/ValravnException.php index 40e97e2..5438372 100644 --- a/src/Exceptions/Valravn/ValravnException.php +++ b/src/Exceptions/Valravn/ValravnException.php @@ -7,10 +7,10 @@ use Symfony\Component\HttpFoundation\Response; class ValravnException extends BaseException { - public static function failedToExecuteDeletingHook( Model $model ): BaseException { + public static function failedToDelete( Model $model ): BaseException { return self::make( - "Failed to execute deleting hook on [" . get_class( $model ) . "] $model->id", - ValravnErrorCode::failedToExecuteDeletingHook(), + "Failed to delete [" . get_class( $model ) . "] $model->id", + ValravnErrorCode::failedToDelete(), Response::HTTP_INTERNAL_SERVER_ERROR ); } diff --git a/src/Repositories/Contracts/Repository.php b/src/Repositories/Contracts/Repository.php index b336c60..4647adc 100644 --- a/src/Repositories/Contracts/Repository.php +++ b/src/Repositories/Contracts/Repository.php @@ -50,6 +50,9 @@ public function enableAuthorization(): static { return $this; } + /** + * @throws AuthorizationException + */ public function ifShouldAuthorize( callable $callable ): void { if ( $this->shouldAuthorize() ) { $callable(); @@ -125,8 +128,9 @@ public function delete( Model|int $model ): bool { try { $this->deleting( $model ); $model->delete(); + $this->deleted( $model ); } catch ( Throwable $e ) { - throw ValravnException::failedToExecuteDeletingHook( $model ); + throw ValravnException::failedToDelete( $model ); } DB::commit(); @@ -142,6 +146,15 @@ protected function deleting( Model $model ) { // } + /** + * Deleted Hook + * + * @param Model $model + */ + protected function deleted( Model $model ) { + // + } + public function create( array $data ): Model { $this->authorize(); diff --git a/tests/Feature/Repositories/RepositoryTest.php b/tests/Feature/Repositories/RepositoryTest.php new file mode 100644 index 0000000..1abe24e --- /dev/null +++ b/tests/Feature/Repositories/RepositoryTest.php @@ -0,0 +1,187 @@ +count( 5 )->create(); + $this->repository = app( SampleRepository::class )->disableAuthorization(); + } + + /** + * @test + * + * @return void + */ + public function shouldAuthorizeAsDefault(): void { + self::assertTrue( app( SampleRepository::class )->shouldAuthorize() ); + } + + /** + * @test + * + * @return void + */ + public function shouldAuthorizeAsDisabled(): void { + $this->repository->disableAuthorization(); + self::assertFalse( $this->repository->shouldAuthorize() ); + } + + /** + * @test + * + * @return void + */ + public function shouldAuthorizeAsEnabled(): void { + $this->repository->disableAuthorization()->enableAuthorization(); + self::assertTrue( $this->repository->shouldAuthorize() ); + } + + /** + * @test + * + * @return void + * @throws AuthorizationException + */ + public function all(): void { + $models = $this->repository->all()->get(); + self::assertEquals( + Post::all()->toArray(), + $models->toArray() + ); + } + + /** + * @test + * + * @return void + * @throws AuthorizationException + */ + public function allUsingSelect(): void { + $models = $this->repository->select( 'id' )->all()->get(); + self::assertEquals( + Post::all()->map( + fn( $value ) => [ 'id' => $value->id ] + ) + ->toArray(), + $models->toArray() + ); + } + + /** + * @test + * + * @return void + * @throws AuthorizationException + */ + public function find(): void { + $model = $this->repository->find( 1 ); + self::assertEquals( + Post::query()->first()->toArray(), + $model->toArray() + ); + } + + /** + * @test + * + * @return void + * @throws AuthorizationException + */ + public function findUsingSelect(): void { + $model = $this->repository->select( 'id' )->find( 1 ); + self::assertEquals( + [ + 'id' => 1 + ], + $model->toArray() + ); + } + + /** + * @test + * + * @return void + */ + public function deleteAction(): void { + self::assertTrue( $this->repository->delete( 1 ) ); + $this->assertDatabaseMissing( Post::table(), [ 'id' => 1 ] ); + } + + /** + * @test + * + * @return void + */ + public function createAction(): void { + $data = [ + 'title' => fake()->sentence(), + 'content' => fake()->paragraph(), + ]; + self::assertInstanceOf( + Model::class, + $this->repository->create( $data ) + ); + $this->assertDatabaseHas( Post::table(), $data ); + } + + /** + * @test + * + * @return void + */ + public function updateAction(): void { + $data = [ + 'title' => fake()->sentence(), + 'content' => fake()->paragraph(), + ]; + self::assertTrue( $this->repository->update( 1, $data ) ); + $this->assertDatabaseHas( Post::table(), $data + [ 'id' => 1 ] ); + } + + /** + * @test + * + * @return void + */ + public function batchUpdateAction(): void { + $data = [ + [ + 'id' => 1, + 'title' => fake()->sentence() + ], + [ + 'id' => 2, + 'content' => fake()->sentence() + ], + [ + 'id' => 3, + 'title' => fake()->sentence(), + 'content' => fake()->sentence() + ] + ]; + + self::assertTrue( + $this->repository->batchUpdate( BatchUpdateDto::make( [ 'batch' => $data ] ) ) + ); + $this->assertDatabaseHas( Post::table(), $data[ 0 ] ); + $this->assertDatabaseHas( Post::table(), $data[ 1 ] ); + $this->assertDatabaseHas( Post::table(), $data[ 2 ] ); + } + + } \ No newline at end of file diff --git a/tests/Instances/Repositories/SampleRepository.php b/tests/Instances/Repositories/SampleRepository.php new file mode 100644 index 0000000..fc40ba0 --- /dev/null +++ b/tests/Instances/Repositories/SampleRepository.php @@ -0,0 +1,18 @@ +