From db37d217519b71a15354cf8adb3ceef1734ff0b2 Mon Sep 17 00:00:00 2001 From: Tim Joosten Date: Sun, 26 Nov 2017 07:19:18 +0100 Subject: [PATCH] Develop (#66) * Fix model path in the repository generator * Fix #61: Convert $id to $primaryKey * Fix #61: Convert $id to $primaryKey * Implement pho_cs and perform nitpick" --- .gitignore | 3 +- .php_cs | 19 ++++ .php_cs.cache | 1 - composer.json | 4 +- config/repositories.php | 8 +- .../Commands/Creators/CriteriaCreator.php | 10 +- .../Commands/Creators/RepositoryCreator.php | 8 +- .../Console/Commands/MakeCriteriaCommand.php | 8 +- .../Commands/MakeRepositoryCommand.php | 54 ++++----- .../Contracts/CacheableInterface.php | 2 +- .../Contracts/CriteriaInterface.php | 8 +- .../Contracts/RepositoryInterface.php | 30 ++--- .../Repositories/Criteria/Criteria.php | 6 +- .../Repositories/Eloquent/Repository.php | 105 +++++++++--------- .../Events/RepositoryEntityCreated.php | 2 +- .../Events/RepositoryEntityDeleted.php | 2 +- .../Events/RepositoryEntityUpdated.php | 2 +- .../Events/RepositoryEventBase.php | 2 +- .../Exceptions/RepositoryException.php | 4 +- .../Repositories/Helpers/CacheKeys.php | 2 +- .../Listeners/CleanCacheRepository.php | 3 +- .../Providers/RepositoryProvider.php | 6 +- .../Traits/CachableRepository.php | 6 +- tests/BaseTest.php | 66 +++++++++++ tests/RepositoryTest.php | 14 +-- tests/resources/FindOrFailTest.php | 20 ++++ tests/resources/models/User.php | 2 +- .../resources/repositories/UserRepository.php | 4 +- 28 files changed, 252 insertions(+), 149 deletions(-) create mode 100644 .php_cs create mode 100644 tests/BaseTest.php create mode 100644 tests/resources/FindOrFailTest.php diff --git a/.gitignore b/.gitignore index 41aab32..f875bbe 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /vendor composer.phar composer.lock -/.idea \ No newline at end of file +/.idea +/.php_cs.cache \ No newline at end of file diff --git a/.php_cs b/.php_cs new file mode 100644 index 0000000..ad43c44 --- /dev/null +++ b/.php_cs @@ -0,0 +1,19 @@ +notPath('bootstrap') + ->notPath('storage') + ->notPath('vendor') + ->in(__DIR__) + ->name('*.php') + ->name('_ide_helper') + ->notName('*.blade.php') + ->ignoreDotFiles(true) + ->ignoreVCS(true); +return PhpCsFixer\Config::create() + ->setRules([ + '@PSR2' => true, + 'array_syntax' => ['syntax' => 'short'], + 'ordered_imports' => ['sortAlgorithm' => 'alpha'], + 'no_unused_imports' => true, + ]) + ->setFinder($finder); \ No newline at end of file diff --git a/.php_cs.cache b/.php_cs.cache index edba1e5..e69de29 100644 --- a/.php_cs.cache +++ b/.php_cs.cache @@ -1 +0,0 @@ -{"php":"7.1.11","version":"2.8.2:v2.8.2#b331701944cbe492e466d2b46b2880068803eb08","rules":{"blank_line_after_namespace":true,"braces":true,"class_definition":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_constants":true,"lowercase_keywords":true,"method_argument_space":{"ensure_fully_multiline":true},"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true,"encoding":true,"full_opening_tag":true},"hashes":[]} \ No newline at end of file diff --git a/composer.json b/composer.json index 9481d00..92bbecf 100644 --- a/composer.json +++ b/composer.json @@ -9,11 +9,11 @@ "illuminate/support": "5.4.*|5.5.*", "illuminate/database": "5.4.*|5.5.*", "illuminate/filesystem": "5.4.*|5.5.*", - "illuminate/console": "5.4.*|5.5.*" + "illuminate/console": "5.4.*|5.5.*", + "friendsofphp/php-cs-fixer": "^2.8" }, "require-dev": { "mockery/mockery": "dev-master@dev", - "squizlabs/php_codesniffer": "3.*", "phpunit/phpunit": "~5.7|~6.0", "orchestra/testbench": "~3.0" }, diff --git a/config/repositories.php b/config/repositories.php index 95b0ea5..3af8853 100644 --- a/config/repositories.php +++ b/config/repositories.php @@ -4,11 +4,11 @@ /** * -------------------------------------------------------------------------- - * Repository namespace + * Repository namespace * -------------------------------------------------------------------------- - * + * * The namespace for the repository classes. - * + * */ 'repository_namespace' => 'App\Repositories', @@ -150,4 +150,4 @@ 'except' => null, ], ], -]; \ No newline at end of file +]; diff --git a/src/ActivismeBE/Repositories/Console/Commands/Creators/CriteriaCreator.php b/src/ActivismeBE/Repositories/Console/Commands/Creators/CriteriaCreator.php index 718341f..a9e5d38 100644 --- a/src/ActivismeBE/Repositories/Console/Commands/Creators/CriteriaCreator.php +++ b/src/ActivismeBE/Repositories/Console/Commands/Creators/CriteriaCreator.php @@ -2,16 +2,16 @@ namespace ActivismeBE\DatabaseLayering\Repositories\Console\Commands\Creators; +use Doctrine\Common\Inflector\Inflector; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Facades\Config; -use Doctrine\Common\Inflector\Inflector; /** * Class CriteriaCreator * * @package ActivismeBE\DatabaseLayering\Console\Commands\Creators */ -class CriteriaCreator +class CriteriaCreator { protected $files; /** @var Filesystem */ protected $criteria; /** @var Criteria */ @@ -147,7 +147,7 @@ protected function getPath() */ protected function getStub() { - return $this->files->get($this->getStubPath() . "criteria.stub"); // Return the stub. + return $this->files->get($this->getStubPath() . "criteria.stub"); // Return the stub. } /** @@ -157,7 +157,7 @@ protected function getStub() */ protected function getStubPath() { - // TODO: Check if we can refactor this one. + // TODO: Check if we can refactor this one. $path = __DIR__ . '/../../../../../../resources/stubs/'; // Path return $path; // Return the path. @@ -204,4 +204,4 @@ protected function pluralizeModel() return $model_name; // Return the pluralized model. } -} \ No newline at end of file +} diff --git a/src/ActivismeBE/Repositories/Console/Commands/Creators/RepositoryCreator.php b/src/ActivismeBE/Repositories/Console/Commands/Creators/RepositoryCreator.php index 361901f..e8101ab 100644 --- a/src/ActivismeBE/Repositories/Console/Commands/Creators/RepositoryCreator.php +++ b/src/ActivismeBE/Repositories/Console/Commands/Creators/RepositoryCreator.php @@ -2,16 +2,16 @@ namespace ActivismeBE\DatabaseLayering\Repositories\Console\Commands\Creators; +use Doctrine\Common\Inflector\Inflector; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Facades\Config; -use Doctrine\Common\Inflector\Inflector; /** * Class RepositoryCreator * * @package ActivismeBE\DatabaseLayering\Console\Commands\Creators */ -class RepositoryCreator +class RepositoryCreator { protected $files; /** @var FileSystem */ protected $repository; /** @var Repository */ @@ -74,8 +74,8 @@ public function create($repository, $model) } /** - * Create a new directory if the directory doesn't exists. - * + * Create a new directory if the directory doesn't exists. + * * @return void */ protected function createDirectory() diff --git a/src/ActivismeBE/Repositories/Console/Commands/MakeCriteriaCommand.php b/src/ActivismeBE/Repositories/Console/Commands/MakeCriteriaCommand.php index b6e82bd..6a30a5b 100644 --- a/src/ActivismeBE/Repositories/Console/Commands/MakeCriteriaCommand.php +++ b/src/ActivismeBE/Repositories/Console/Commands/MakeCriteriaCommand.php @@ -2,10 +2,10 @@ namespace ActivismeBE\DatabaseLayering\Repositories\Console\Commands; +use ActivismeBE\DatabaseLayering\Repositories\Console\Commands\Creators\CriteriaCreator; use Illuminate\Console\Command; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputArgument; -use ActivismeBE\DatabaseLayering\Repositories\Console\Commands\Creators\CriteriaCreator; +use Symfony\Component\Console\Input\InputOption; /** * Class MakeCriteriaCommand @@ -74,7 +74,7 @@ public function writeCriteria($arguments, $options) $criteria = $arguments['criteria']; // Set criteria $model = $options['model']; // Set model. - if ($this->creator->create($criteria, $model)) { // Create the criteria. + if ($this->creator->create($criteria, $model)) { // Create the criteria. $this->info("Succesfully created the criteria class."); // Information message. } } @@ -98,4 +98,4 @@ protected function getOptions() { return [['model', null, InputOption::VALUE_OPTIONAL, 'The model name.', null]]; } -} \ No newline at end of file +} diff --git a/src/ActivismeBE/Repositories/Console/Commands/MakeRepositoryCommand.php b/src/ActivismeBE/Repositories/Console/Commands/MakeRepositoryCommand.php index c4e72c5..304fcb8 100644 --- a/src/ActivismeBE/Repositories/Console/Commands/MakeRepositoryCommand.php +++ b/src/ActivismeBE/Repositories/Console/Commands/MakeRepositoryCommand.php @@ -2,51 +2,51 @@ namespace ActivismeBE\DatabaseLayering\Repositories\Console\Commands; -use Illuminate\Console\Command; -use Symfony\Component\Console\Input\InputOption; -use Symfony\Component\Console\Input\InputArgument; use ActivismeBE\DatabaseLayering\Repositories\Console\Commands\Creators\RepositoryCreator; +use Illuminate\Console\Command; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputOption; /** * Class MakeRepositoryCommand - * + * * @package ActivismeBE\DatabaseLayering\Repositories\Console\Commands */ -class MakeRepositoryCommand extends Command +class MakeRepositoryCommand extends Command { /** - * The name and signature of the console command. - * - * @var string - */ - protected $name = 'make:repository'; + * The name and signature of the console command. + * + * @var string + */ + protected $name = 'make:repository'; /** - * The console command description. - * - * @var string + * The console command description. + * + * @var string */ protected $description = 'Create a new repository class'; /** * @var RepositoryCreator */ - protected $creator; + protected $creator; /** - * @var Composer - */ - protected $composer; + * @var Composer + */ + protected $composer; /** - * @param RepositoryCreator $creator + * @param RepositoryCreator $creator */ - public function __construct(RepositoryCreator $creator) + public function __construct(RepositoryCreator $creator) { - parent::__construct(); + parent::__construct(); $this->creator = $creator; // Set the creator - $this->composer = app()['composer']; // Set Composer + $this->composer = app()['composer']; // Set Composer } /** @@ -77,11 +77,11 @@ protected function writeRepository($arguments, $options) } } - /** - * Get the console command arguments. - * - * @return array - */ + /** + * Get the console command arguments. + * + * @return array + */ protected function getArguments() { return [['repository', InputArgument::REQUIRED, 'The repository name.']]; @@ -96,4 +96,4 @@ protected function getOptions() { return [['model', null, InputOption::VALUE_OPTIONAL, 'The model name.', null]]; } -} \ No newline at end of file +} diff --git a/src/ActivismeBE/Repositories/Contracts/CacheableInterface.php b/src/ActivismeBE/Repositories/Contracts/CacheableInterface.php index 27d906f..d8cbfbc 100644 --- a/src/ActivismeBE/Repositories/Contracts/CacheableInterface.php +++ b/src/ActivismeBE/Repositories/Contracts/CacheableInterface.php @@ -49,4 +49,4 @@ public function getCacheMinutes(); * @return $this */ public function skipCache($status = true); -} \ No newline at end of file +} diff --git a/src/ActivismeBE/Repositories/Contracts/CriteriaInterface.php b/src/ActivismeBE/Repositories/Contracts/CriteriaInterface.php index 4beaa9f..b15b592 100644 --- a/src/ActivismeBE/Repositories/Contracts/CriteriaInterface.php +++ b/src/ActivismeBE/Repositories/Contracts/CriteriaInterface.php @@ -6,10 +6,10 @@ /** * Interface CriteriaInterface - * + * * @package ActivismeBE\DatabaseLayering\Contracts */ -interface CriteriaInterface +interface CriteriaInterface { /** * @param bool $status @@ -24,7 +24,7 @@ public function getCriteria(); /** * @param Criteria $criteria - * + * * @return $this */ public function getByCriteria(Criteria $criteria); @@ -39,4 +39,4 @@ public function pushCriteria(Criteria $criteria); * @return $this */ public function applyCriteria(); -} \ No newline at end of file +} diff --git a/src/ActivismeBE/Repositories/Contracts/RepositoryInterface.php b/src/ActivismeBE/Repositories/Contracts/RepositoryInterface.php index 5e20674..5b8fd23 100644 --- a/src/ActivismeBE/Repositories/Contracts/RepositoryInterface.php +++ b/src/ActivismeBE/Repositories/Contracts/RepositoryInterface.php @@ -4,7 +4,7 @@ /** * Interface RepositoryInterface - * + * * @category RepositoryInterface * @package ActivismeBE\DatabaseLayering\Contracts * @author Tim Joosten @@ -15,7 +15,7 @@ interface RepositoryInterface * Get all the rows for the database table. * * @param array $columns The columns u want to use in your view. - * + * * @return mixed */ public function all($columns = ['*']); @@ -25,7 +25,7 @@ public function all($columns = ['*']); * * @param integer $perPage The ata rows per page in the view. * @param array $columns The coolumns u want to use in your view. - * + * * @return mixed */ public function paginate($perPage = 1, $columns = ['*']); @@ -53,7 +53,7 @@ public function saveModel(array $data); * * @param array $data The input data fields u want to update in the db. * @param integer $primaryKey The PK in the database. - * + * * @return mixed */ public function update(array $data, $primaryKey); @@ -62,7 +62,7 @@ public function update(array $data, $primaryKey); * Delete a record in the database. * * @param integer $primaryKey The resource id in the database. (PK) - * + * * @return mixed */ public function delete($primaryKey); @@ -83,7 +83,7 @@ public function deleteAllBy($field, $selector, $value); * * @param integer $primaryKey The resource id in the database. (PK) * @param array $columns The db table columns that u want to use in your view. - * + * * @return mixed */ public function find($primaryKey, $columns = ['*']); @@ -91,8 +91,8 @@ public function find($primaryKey, $columns = ['*']); /** * Try to find a record in the database based on the primary key. * - * @param integer $primaryKey The primary key in the database table. - * + * @param integer $primaryKey The primary key in the database table. + * * @return void */ public function findOrFail($primaryKey); @@ -103,18 +103,18 @@ public function findOrFail($primaryKey); * @param string $field The field where u want to search on. * @param string $value The value in the above given field. * @param array $columns The database columns u want to use on the view. - * + * * @return mixed */ public function findBy($field, $value, $columns = ['*']); /** - * Find a database record by value and column. - * + * Find a database record by value and column. + * * @param string $field The database column name. * @param string $value The value in the database column. * @param array $columns The database table columns u want to use in your view. - * + * * @return mixed */ public function findAllBy($field, $value, $columns = ['*']); @@ -122,10 +122,10 @@ public function findAllBy($field, $value, $columns = ['*']); /** * Find a collection of models by the given query conditions. * - * @param array $where The keys and values where u want to search on. - * @param array $columns The database columns u want to use in the view. + * @param array $where The keys and values where u want to search on. + * @param array $columns The database columns u want to use in the view. * * @return mixed */ public function findWhere($where, $columns = ['*']); -} \ No newline at end of file +} diff --git a/src/ActivismeBE/Repositories/Criteria/Criteria.php b/src/ActivismeBE/Repositories/Criteria/Criteria.php index bc3e81a..dbe97bc 100644 --- a/src/ActivismeBE/Repositories/Criteria/Criteria.php +++ b/src/ActivismeBE/Repositories/Criteria/Criteria.php @@ -4,12 +4,12 @@ use ActivismeBE\DatabaseLayering\Repositories\Contracts\RepositoryInterface; -abstract class Criteria +abstract class Criteria { /** * @param $model * @param RepositoryInterface $repository * @return mixed */ - public abstract function apply($model, RepositoryInterface $repository); -} \ No newline at end of file + abstract public function apply($model, RepositoryInterface $repository); +} diff --git a/src/ActivismeBE/Repositories/Eloquent/Repository.php b/src/ActivismeBE/Repositories/Eloquent/Repository.php index 280bd80..75ed9f9 100644 --- a/src/ActivismeBE/Repositories/Eloquent/Repository.php +++ b/src/ActivismeBE/Repositories/Eloquent/Repository.php @@ -2,22 +2,22 @@ namespace ActivismeBE\DatabaseLayering\Repositories\Eloquent; +use ActivismeBE\DatabaseLayering\Repositories\Contracts\CriteriaInterface; +use ActivismeBE\DatabaseLayering\Repositories\Contracts\RepositoryInterface; +use ActivismeBE\DatabaseLayering\Repositories\Criteria\Criteria; +use ActivismeBE\DatabaseLayering\Repositories\Exceptions\RepositoryException; use Closure; -use Illuminate\Support\Collection; -use Illuminate\Database\Eloquent\Model; + use Illuminate\Container\Container as App; +use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\ModelNotFoundException; - -use ActivismeBE\DatabaseLayering\Repositories\Contracts\RepositoryInterface; -use ActivismeBE\DatabaseLayering\Repositories\Contracts\CriteriaInterface; -use ActivismeBE\DatabaseLayering\Repositories\Exceptions\RepositoryException; -use ActivismeBE\DatabaseLayering\Repositories\Criteria\Criteria; +use Illuminate\Support\Collection; /** * Class Repository - * - * @category - * @author + * + * @category + * @author * @author * @license MIT License * @link https://cpsb.github.io/ActivismeBE-database-layering/license @@ -60,7 +60,7 @@ abstract class Repository implements RepositoryInterface, CriteriaInterface /** * @param App $app * @param Collection $collection - * + * * @throws \ActivismeBE\DatabaseLayering\Repositories\Exceptions\RepositoryException */ public function __construct(App $app, Collection $collection) @@ -77,14 +77,14 @@ public function __construct(App $app, Collection $collection) * * @return mixed */ - public abstract function model(); + abstract public function model(); /** * Get the base enttiy form the model. - * - * @return \Illuminate\Database\Eloquent\Model + * + * @return \Illuminate\Database\Eloquent\Model */ - public function entity() + public function entity() { return $this->newModel; } @@ -93,21 +93,20 @@ public function entity() * Get all the records form the database table. * * @param array $columns The database column names u want to use in your view. - * + * * @return mixed */ public function all($columns = ['*']) { $this->applyCriteria(); return $this->model->get($columns); - } /** * Apply database relations on the query. * * @param array $relations The relations u want to apply on your query. - * + * * @return $this */ public function with(array $relations) @@ -120,10 +119,10 @@ public function with(array $relations) * Pluck all the values based on key and column. * * @param string $value The value for the lists function. - * @param string $key The key for the lists function. - * + * @param string $key The key for the lists function. + * * @deprecated deprecated in Laravel 5.3 - * + * * @return array */ public function pluck($value, $key = null) @@ -141,9 +140,9 @@ public function pluck($value, $key = null) /** * Paginate the database results from the query. * - * @param integer $perPage The data records u want to display per page. + * @param integer $perPage The data records u want to display per page. * @param array $columns Te database columns u want to use in the view. - * + * * @return mixed */ public function paginate($perPage = 25, $columns = ['*']) @@ -156,7 +155,7 @@ public function paginate($perPage = 25, $columns = ['*']) * Create a new data record in the database. * * @param array $data The data fields u want to store in the database table. - * + * * @return mixed */ public function create(array $data) @@ -165,25 +164,25 @@ public function create(array $data) } /** - * Fill attributes data before saving. - * - * @param array $data The given data from the input. - * @param integer $primaryKey The primary key in the database table. - * + * Fill attributes data before saving. + * + * @param array $data The given data from the input. + * @param integer $primaryKey The primary key in the database table. + * * @throws ModelNotFoundException - * + * * @return mixed */ - public function fill(array $data, $primaryKey) + public function fill(array $data, $primaryKey) { - $model = $this->makeModel()->find($primaryKey); + $model = $this->makeModel()->find($primaryKey); if (! $model) { throw new ModelNotFoundException("Model '" . $this->model() . "' with id ${id} not found."); } return $model->fill($data)->save(); - } + } /** * Save a model without mass assignment @@ -191,7 +190,7 @@ public function fill(array $data, $primaryKey) * @see \ActivismeBE\DatabaseLayering\Tests\Repositories\RepositoryTest::testSaveModel() * * @param array $data - * + * * @return bool */ public function saveModel(array $data) @@ -209,7 +208,7 @@ public function saveModel(array $data) * @param array $data * @param int $primaryKey * @param string $attribute - * + * * @return mixed */ public function update(array $data, $primaryKey, $attribute = "id") @@ -220,9 +219,9 @@ public function update(array $data, $primaryKey, $attribute = "id") /** * Update database records through the eloquent fill method. * - * @param array $data The data fields u want to + * @param array $data The data fields u want to * @param integer $primaryKey The primary key in the database column. - * + * * @return mixed */ public function updateRich(array $data, $primaryKey) @@ -273,7 +272,7 @@ public function deleteAllBy($field, $selector, $value) * @param array $columns The database columns u want to use. * @return mixed */ - public function find($primaryKey, $columns = array('*')) + public function find($primaryKey, $columns = ['*']) { $this->applyCriteria(); return $this->model->find($primaryKey, $columns); @@ -284,12 +283,12 @@ public function find($primaryKey, $columns = array('*')) * * @see \ActivismeBE\DatabaseLayering\Tests\Repositories\RepositoryTest::testFindOrfailSuc() * @see \ActivismeBE\DatabaseLayering\Tests\Repositories\RepositoryTest::testFindOrFailErr() - * + * * @param integer $primaryKey The primary key in the database table. - * + * * @return mixed */ - public function findOrFail($primaryKey) + public function findOrFail($primaryKey) { return $this->model->findOrFail($primaryKey); } @@ -300,7 +299,7 @@ public function findOrFail($primaryKey) * @param string $attribute The database column name. * @param string $value The value that u want to find in the database table. * @param array $columns The database columns u want to use. - * + * * @return mixed */ public function findBy($attribute, $value, $columns = ['*']) @@ -315,7 +314,7 @@ public function findBy($attribute, $value, $columns = ['*']) * @param string $attribute The database column name. * @param string $value The value where u want to search on. * @param array $columns The database columns want to use. - * + * * @return mixed */ public function findAllBy($attribute, $value, $columns = ['*']) @@ -329,7 +328,7 @@ public function findAllBy($attribute, $value, $columns = ['*']) * * @param array $where The where criteria for the find query. * @param array $columns The database table columns u want to use in your view. - * @param bool $or Enable of disable setter for OR WHERE queries. + * @param bool $or Enable of disable setter for OR WHERE queries. * * @return \Illuminate\Database\Eloquent\Collection|null */ @@ -393,9 +392,9 @@ public function makeModel() * Set Eloquent Model to instantiate * * @param $eloquentModel - * + * * @throws RepositoryException - * + * * @return Model */ public function setModel($eloquentModel) @@ -426,7 +425,7 @@ public function resetScope() * Skip the given criteria. * * @param bool $status - * + * * @return $this */ public function skipCriteria($status = true) @@ -449,7 +448,7 @@ public function getCriteria() * Get records based on the repository call criteria. * * @param Criteria $criteria The criteria u want to take. - * + * * @return $this */ public function getByCriteria(Criteria $criteria) @@ -462,7 +461,7 @@ public function getByCriteria(Criteria $criteria) * Push new query criteria in the interface call. * * @param Criteria $criteria The Criteria instance u want to apply. - * + * * @return $this */ public function pushCriteria(Criteria $criteria) @@ -488,12 +487,14 @@ public function pushCriteria(Criteria $criteria) */ public function applyCriteria() { - if ($this->skipCriteria === true) + if ($this->skipCriteria === true) { return $this; + } foreach ($this->getCriteria() as $criteria) { - if ($criteria instanceof Criteria) + if ($criteria instanceof Criteria) { $this->model = $criteria->apply($this->model, $this); + } } return $this; } -} \ No newline at end of file +} diff --git a/src/ActivismeBE/Repositories/Events/RepositoryEntityCreated.php b/src/ActivismeBE/Repositories/Events/RepositoryEntityCreated.php index b960cda..f82ccd2 100644 --- a/src/ActivismeBE/Repositories/Events/RepositoryEntityCreated.php +++ b/src/ActivismeBE/Repositories/Events/RepositoryEntityCreated.php @@ -15,4 +15,4 @@ class RepositoryEntityCreated extends RepositoryEventBase * @var string */ protected $action = 'created'; -} \ No newline at end of file +} diff --git a/src/ActivismeBE/Repositories/Events/RepositoryEntityDeleted.php b/src/ActivismeBE/Repositories/Events/RepositoryEntityDeleted.php index 978442e..cd4389c 100644 --- a/src/ActivismeBE/Repositories/Events/RepositoryEntityDeleted.php +++ b/src/ActivismeBE/Repositories/Events/RepositoryEntityDeleted.php @@ -15,4 +15,4 @@ class RepositoryEntityDeleted extends RepositoryEventBase * @return string */ protected $action = 'deleted'; -} \ No newline at end of file +} diff --git a/src/ActivismeBE/Repositories/Events/RepositoryEntityUpdated.php b/src/ActivismeBE/Repositories/Events/RepositoryEntityUpdated.php index 47a35a0..0354baf 100644 --- a/src/ActivismeBE/Repositories/Events/RepositoryEntityUpdated.php +++ b/src/ActivismeBE/Repositories/Events/RepositoryEntityUpdated.php @@ -15,4 +15,4 @@ class RepositoryEntityUpdated extends RepositoryEventBase * @var string */ protected $action = 'updated'; -} \ No newline at end of file +} diff --git a/src/ActivismeBE/Repositories/Events/RepositoryEventBase.php b/src/ActivismeBE/Repositories/Events/RepositoryEventBase.php index 8d9389e..63f6cae 100644 --- a/src/ActivismeBE/Repositories/Events/RepositoryEventBase.php +++ b/src/ActivismeBE/Repositories/Events/RepositoryEventBase.php @@ -65,4 +65,4 @@ public function getAction() { return $this->action; } -} \ No newline at end of file +} diff --git a/src/ActivismeBE/Repositories/Exceptions/RepositoryException.php b/src/ActivismeBE/Repositories/Exceptions/RepositoryException.php index d573bae..6fac867 100644 --- a/src/ActivismeBE/Repositories/Exceptions/RepositoryException.php +++ b/src/ActivismeBE/Repositories/Exceptions/RepositoryException.php @@ -4,10 +4,10 @@ /** * Class RepositoryException - * + * * @package Bosnadev\Repositories\Exceptions */ class RepositoryException extends \Exception { // -} \ No newline at end of file +} diff --git a/src/ActivismeBE/Repositories/Helpers/CacheKeys.php b/src/ActivismeBE/Repositories/Helpers/CacheKeys.php index 0675bc1..51db79f 100644 --- a/src/ActivismeBE/Repositories/Helpers/CacheKeys.php +++ b/src/ActivismeBE/Repositories/Helpers/CacheKeys.php @@ -111,4 +111,4 @@ public function __call($method, $parameters) $instance = new static; return call_user_func_array([$instance, $method], $parameters); } -} \ No newline at end of file +} diff --git a/src/ActivismeBE/Repositories/Listeners/CleanCacheRepository.php b/src/ActivismeBE/Repositories/Listeners/CleanCacheRepository.php index 456219d..b31390a 100644 --- a/src/ActivismeBE/Repositories/Listeners/CleanCacheRepository.php +++ b/src/ActivismeBE/Repositories/Listeners/CleanCacheRepository.php @@ -6,7 +6,6 @@ use ActivismeBE\DatabaseLayering\Repositories\Events\RepositoryEventBase; use ActivismeBE\DatabaseLayering\Repositories\Helpers\CacheKeys; use Illuminate\Contracts\Cache\Repository as CacheRepository; -use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Log; @@ -74,4 +73,4 @@ public function handle(RepositoryEventBase $event) Log::error($exception->getMessage()); } } -} \ No newline at end of file +} diff --git a/src/ActivismeBE/Repositories/Providers/RepositoryProvider.php b/src/ActivismeBE/Repositories/Providers/RepositoryProvider.php index e4f6365..099445a 100644 --- a/src/ActivismeBE/Repositories/Providers/RepositoryProvider.php +++ b/src/ActivismeBE/Repositories/Providers/RepositoryProvider.php @@ -7,9 +7,9 @@ use ActivismeBE\DatabaseLayering\Repositories\Console\Commands\MakeCriteriaCommand; use ActivismeBE\DatabaseLayering\Repositories\Console\Commands\MakeRepositoryCommand; +use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Composer; use Illuminate\Support\ServiceProvider; -use Illuminate\Filesystem\Filesystem; /** * Class RepositoryProvider @@ -81,7 +81,7 @@ protected function registerBindings() $this->app->instance('FileSystem', new Filesystem()); // Composer. - $this->app->bind('Composer', function ($app) { + $this->app->bind('Composer', function ($app) { return new Composer($app['FileSystem']); }); @@ -131,4 +131,4 @@ public function provides() { return ['command.repository.make', 'command.criteria.make']; } -} \ No newline at end of file +} diff --git a/src/ActivismeBE/Repositories/Traits/CachableRepository.php b/src/ActivismeBE/Repositories/Traits/CachableRepository.php index 9f50d5b..10213c0 100644 --- a/src/ActivismeBE/Repositories/Traits/CachableRepository.php +++ b/src/ActivismeBE/Repositories/Traits/CachableRepository.php @@ -4,9 +4,9 @@ use ActivismeBE\DatabaseLayering\Repositories\Contracts\CriteriaInterface; use ActivismeBE\DatabaseLayering\Repositories\Helpers\CacheKeys; +use Exception; use Illuminate\Contracts\Cache\Repository as CacheRepository; use ReflectionObject; -use Exception; /** * Trait CachableRepository @@ -188,7 +188,7 @@ public function all($columns = ['*']) return parent::all($columns); } - $key = $this->getCacheKey('all',func_get_args()); + $key = $this->getCacheKey('all', func_get_args()); $minutes = $this->getCacheMinutes(); $value = $this->getCacheRepository()->remember($key, $minutes, function () use ($columns) { return parent::all($columns); @@ -305,4 +305,4 @@ public function getByCriteria(CriteriaInterface $criteria) return $value; } -} \ No newline at end of file +} diff --git a/tests/BaseTest.php b/tests/BaseTest.php new file mode 100644 index 0000000..268f612 --- /dev/null +++ b/tests/BaseTest.php @@ -0,0 +1,66 @@ +mock = m::mock('Eloquent'); + $this->repository = new UserRepository($this->app, new Collection()); + + $this->setUpDatabase($this->app); + } + + protected function getPackageProviders($app) + { + return [RepositoryProvider::class]; + } + + /** + * Define environment setup. + * + * @param \Illuminate\Foundation\Application $app + * @return void + */ + protected function getEnvironmentSetUp($app) + { + // Setup default database to use sqlite :memory: + $app['config']->set('database.default', 'testbench'); + $app['config']->set('database.connections.testbench', [ + 'driver' => 'sqlite', + 'database' => ':memory:', + 'prefix' => '', + ]); + } + + protected function setUpDatabase($app) + { + $app['db']->connection()->getSchemaBuilder()->create('users', function (Blueprint $table) { + $table->increments('id'); + $table->string('first_name')->nullable(); + $table->string('last_name')->nullable(); + $table->string('email')->nullable(); + $table->string('password')->nullable(); + $table->timestamps(); + }); + + $app[User::class]->insert([ + 'first_name' => 'firstname', + 'last_name' => 'lastname', + 'email' => 'email@example.tld', + 'password' => 'secret', + ]); + } +} diff --git a/tests/RepositoryTest.php b/tests/RepositoryTest.php index 2d31c2b..436f42d 100644 --- a/tests/RepositoryTest.php +++ b/tests/RepositoryTest.php @@ -5,19 +5,19 @@ //error_reporting(E_ALL); //ini_set("display_errors", 1); +use \Illuminate\Database\Schema\Blueprint; +use \Mockery as m; use ActivismeBE\DatabaseLayering\Repositories\Providers\RepositoryProvider; use ActivismeBE\DatabaseLayering\Tests\Resources\Models\User; use ActivismeBE\DatabaseLayering\Tests\Resources\Repositories\UserRepository; -use \Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Collection; -use \Mockery as m; class RepositoryTest extends \Orchestra\Testbench\TestCase { protected $mock; protected $repository; - public function setUp() + public function setUp() { parent::setUp(); $this->mock = m::mock('Eloquent'); @@ -67,7 +67,7 @@ protected function setUpDatabase($app) ]); } - public function testFindOrFailSuc() + public function testFindOrFailSuc() { $call = $this->repository->findOrFail(1); @@ -77,9 +77,9 @@ public function testFindOrFailSuc() $this->assertEquals('secret', $call->password); } - public function testFindOrFailErr() + public function testFindOrFailErr() { - // + // } public function testFindAllColumns() @@ -119,4 +119,4 @@ public function testSaveModel() { // TODO: Write test } -} \ No newline at end of file +} diff --git a/tests/resources/FindOrFailTest.php b/tests/resources/FindOrFailTest.php new file mode 100644 index 0000000..07af721 --- /dev/null +++ b/tests/resources/FindOrFailTest.php @@ -0,0 +1,20 @@ +repository->findOrFail(1); + + $this->assertEquals('firstname', $call->first_name); + $this->assertEquals('lastname', $call->last_name); + $this->assertEquals('email@example.tld', $call->email); + $this->assertEquals('secret', $call->password); + } + + public function testFindOrFailErr() + { + } +} diff --git a/tests/resources/models/User.php b/tests/resources/models/User.php index 105bc8d..fc88f46 100644 --- a/tests/resources/models/User.php +++ b/tests/resources/models/User.php @@ -7,4 +7,4 @@ class User extends Model { protected $fillable = ['first_name']; -} \ No newline at end of file +} diff --git a/tests/resources/repositories/UserRepository.php b/tests/resources/repositories/UserRepository.php index 444f495..fb073fc 100644 --- a/tests/resources/repositories/UserRepository.php +++ b/tests/resources/repositories/UserRepository.php @@ -2,10 +2,8 @@ namespace ActivismeBE\DatabaseLayering\Tests\Resources\Repositories; -use ActivismeBE\DatabaseLayering\Tests\Resources\Models\User; -use ActivismeBE\DatabaseLayering\Repositories\Contracts\RepositoryInterface; use ActivismeBE\DatabaseLayering\Repositories\Eloquent\Repository; - +use ActivismeBE\DatabaseLayering\Tests\Resources\Models\User; class UserRepository extends Repository {