Skip to content
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

testing repositories #6

Merged
merged 11 commits into from
May 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 19 additions & 35 deletions .github/workflows/laravel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,36 @@ name: Laravel

on:
push:
branches: [ master ]
branches: [ main ]
pull_request:
branches: [ master ]
branches: [ main ]

jobs:
phpunit:
runs-on: ubuntu-18.04
services:
# Label used to access the service container
postgres:
# Docker Hub image
image: postgis/postgis:latest
# Provide the password for postgres
env:
POSTGRES_PASSWORD: postgres
ports:
- 5432:5432
# Set health checks to wait until postgres has startedkj
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
laravel-tests:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Setup PHP with pecl extension
uses: shivammathur/setup-php@v2
- uses: shivammathur/setup-php@15c43e89cdef867065b0213be354c2841860869e
with:
php-version: '7.3'
php-version: '8.0'
- uses: actions/checkout@v2
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Install Dependencies
run: composer install --no-interaction --prefer-dist --optimize-autoloader
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- name: Generate key
run: php artisan key:generate
- name: JWT key
run: php artisan jwt:secret
- name: Directory Permissions
run: chmod -R 777 storage bootstrap/cache

- name: PhpStan
run: ./vendor/phpstan/phpstan/phpstan analyse
- name: Create Database
run: |
mkdir -p database
touch database/database.sqlite
- name: Execute tests (Unit and Feature tests) via PHPUnit
env:
DB_CONNECTION: pgsql
DB_HOST: 127.0.0.1
DB_PORT: 5432
DB_DATABASE: postgres
DB_USERNAME: postgres
DB_PASSWORD: postgres
run: php artisan migrate:refresh --seed && vendor/bin/phpunit
DB_CONNECTION: sqlite
DB_DATABASE: database/database.sqlite
run: vendor/bin/phpunit
50 changes: 0 additions & 50 deletions app/Data/Dto/AuthorData.php

This file was deleted.

3 changes: 2 additions & 1 deletion app/Data/Models/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
* @method static orderBy(mixed $param, mixed $param1)
* @method static create(array $array)
* @method static count()
* @method where(string $string, int $book_id)
* @method static where(string $string, int $book_id)
* @method static find($id)
*/
class Book extends Model
{
Expand Down
87 changes: 87 additions & 0 deletions app/Data/Repository/BookRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php


namespace App\Data\Repository;

use App\Data\Models\Book;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

/**
* Class BookRepository
* @package App\Data\Repository
*/
class BookRepository implements ReadBook, WriteBook
{
/**
* @param int $id
* @return mixed
*/
public function find(int $id): mixed
{
return Book::with('authors')->find($id);
}

/**
* @return Collection|array
*/
public function all(): Collection|array
{
return Book::all();
}

/**
* @param int $id
* @return bool
*/
public function destroy(int $id): bool
{
return (bool)Book::destroy($id);
}

/**
* @param int $id
* @param array $attributes
* @return bool
*/
public function update(int $id, array $attributes): bool
{
return (bool)Book::where('id',$id)->update($attributes);
}

/**
* @param string $column
* @param string $desc
* @param int $limit
* @return LengthAwarePaginator
*/
public function paginate(string $column, string $desc, int $limit): LengthAwarePaginator
{
return Book::orderBy($column ?? 'id', $desc ?? 'desc')->paginate($limit);
}

/**
* @param array $attributes
* @return Book
*/
public function create(array $attributes): Book
{
return Book::create($attributes);
}

/**
* @param Model $model
* @param string $relation
* @param array $ids
* @return Model
*/
public function attach(Model $model, string $relation, array $ids): Model
{
$model->{$relation}()->attach($ids);

$model->load($relation);

return $model;
}
}
32 changes: 32 additions & 0 deletions app/Data/Repository/ReadBook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php


namespace App\Data\Repository;

use Illuminate\Contracts\Pagination\LengthAwarePaginator;

/**
* Interface BookRepositoryInterface
* @package App\Data\Repository
*/
interface ReadBook
{
/**
* @param int $id
* @return mixed
*/
public function find(int $id): mixed;

/**
* @return mixed
*/
public function all(): mixed;

/**
* @param string $column
* @param string $desc
* @param int $limit
* @return LengthAwarePaginator
*/
public function paginate(string $column, string $desc, int $limit): LengthAwarePaginator;
}
40 changes: 40 additions & 0 deletions app/Data/Repository/WriteBook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php


namespace App\Data\Repository;

use Illuminate\Database\Eloquent\Model;

/**
* Interface WriteModel
* @package App\Data\Repository
*/
interface WriteBook
{
/**
* @param int $id
* @param array $attributes
* @return bool
*/
public function update(int $id, array $attributes): bool;

/**
* @param array $attributes
* @return Model
*/
public function create(array $attributes): Model;

/**
* @param Model $model
* @param string $relation
* @param array $ids
* @return Model
*/
public function attach(Model $model, string $relation, array $ids): Model;

/**
* @param int $id
* @return bool
*/
public function destroy(int $id): bool;
}
8 changes: 5 additions & 3 deletions app/Domains/Book/Jobs/DestroyBookJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace App\Domains\Book\Jobs;

use App\Data\Models\Book;
use App\Data\Repository\ReadBook;
use App\Data\Repository\WriteBook;
use Lucid\Units\Job;

class DestroyBookJob extends Job
Expand All @@ -24,11 +26,11 @@ public function __construct(int $book_id)
/**
* Execute the job.
*
* @param Book $book
* @param WriteBook $book
* @return bool
*/
public function handle(Book $book): bool
public function handle(WriteBook $book): bool
{
return $book->where('id',$this->book_id)->delete();
return (bool)$book->destroy($this->book_id);
}
}
7 changes: 4 additions & 3 deletions app/Domains/Book/Jobs/GetBookByIdJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Domains\Book\Jobs;

use App\Data\Models\Book;
use App\Data\Repository\ReadBook;
use Lucid\Units\Job;

class GetBookByIdJob extends Job
Expand All @@ -25,11 +26,11 @@ public function __construct(int $book_id)
/**
* Execute the job.
*
* @param Book $book
* @param ReadBook $book
* @return mixed
*/
public function handle(Book $book): mixed
public function handle(ReadBook $book): mixed
{
return $book->where('id', $this->book_id)->with('authors')->first();
return $book->find($this->book_id);
}
}
9 changes: 4 additions & 5 deletions app/Domains/Book/Jobs/GetListBooksJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Domains\Book\Jobs;

use App\Data\Models\Book;
use App\Data\Repository\ReadBook;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Lucid\Units\Job;

Expand Down Expand Up @@ -40,12 +40,11 @@ public function __construct(string $column, string $desc, int $limit)
/**
* Execute the job.
*
* @param Book $book
* @param ReadBook $book
* @return LengthAwarePaginator
*/
public function handle(Book $book): LengthAwarePaginator
public function handle(ReadBook $book): LengthAwarePaginator
{
return $book->orderBy($this->column ?? 'id', $this->desc ?? 'desc')
->paginate($this->limit ?? 10);
return $book->paginate($this->column ?? 'id', $this->desc ?? 'desc',$this->limit ?? 10);
}
}
Loading