Skip to content

Commit

Permalink
RepositoryTest completed;
Browse files Browse the repository at this point in the history
  • Loading branch information
hans-thomas committed May 6, 2023
1 parent cdc7819 commit 7e51c11
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Exceptions/Valravn/ValravnErrorCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}
6 changes: 3 additions & 3 deletions src/Exceptions/Valravn/ValravnException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Expand Down
15 changes: 14 additions & 1 deletion src/Repositories/Contracts/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public function enableAuthorization(): static {
return $this;
}

/**
* @throws AuthorizationException
*/
public function ifShouldAuthorize( callable $callable ): void {
if ( $this->shouldAuthorize() ) {
$callable();
Expand Down Expand Up @@ -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();

Expand All @@ -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();

Expand Down
187 changes: 187 additions & 0 deletions tests/Feature/Repositories/RepositoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
<?php

namespace Hans\Tests\Valravn\Feature\Repositories;

use Hans\Tests\Valravn\Core\Factories\PostFactory;
use Hans\Tests\Valravn\Core\Models\Post;
use Hans\Tests\Valravn\Instances\Repositories\SampleRepository;
use Hans\Tests\Valravn\TestCase;
use Hans\Valravn\DTOs\BatchUpdateDto;
use Hans\Valravn\Repositories\Contracts\Repository;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\Model;

class RepositoryTest extends TestCase {
private Repository $repository;

/**
* @return void
*/
protected function setUp(): void {
parent::setUp();
PostFactory::new()->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 ] );
}

}
18 changes: 18 additions & 0 deletions tests/Instances/Repositories/SampleRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Hans\Tests\Valravn\Instances\Repositories;

use Hans\Tests\Valravn\Core\Models\Post;
use Hans\Valravn\Repositories\Contracts\Repository;
use Illuminate\Contracts\Database\Eloquent\Builder;

class SampleRepository extends Repository {

/**
* @return Builder
*/
protected function getQueryBuilder(): Builder {
return Post::query();
}

}

0 comments on commit 7e51c11

Please sign in to comment.