diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index c245b97d..d24db0d7 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -14,7 +14,7 @@ jobs: strategy: matrix: operating-system: [ ubuntu-latest ] - php-versions: [ '7.3', '7.4' ] + php-versions: ['7.4', '8.0', '8.1' ] runs-on: ${{ matrix.operating-system }} @@ -35,12 +35,12 @@ jobs: uses: actions/cache@v3 with: path: vendor - key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} + key: ${{ runner.os }}-php-${{ matrix.php-versions }}-${{ hashFiles('**/composer.json') }} restore-keys: | - ${{ runner.os }}-php- + ${{ runner.os }}-php-${{ matrix.php-versions }}- - name: Install dependencies - run: composer install --prefer-dist --no-progress + run: composer update --prefer-dist --no-progress - name: Create env run: php qt core:env diff --git a/composer.json b/composer.json index 9c885228..df054187 100644 --- a/composer.json +++ b/composer.json @@ -5,14 +5,10 @@ "license": "MIT", "type": "project", "require": { - "quantum/framework": "^2.9.9", + "quantum/framework": "dev-master", "fakerphp/faker": "^1.15", "bluemmb/faker-picsum-photos-provider": "^2.0", "ottaviano/faker-gravatar": "^0.3.0", - "doctrine/annotations": "^2.0", - "voku/html-min": "^4.5", - "league/commonmark": "^1.6", - "ezyang/htmlpurifier": "^4.18", "symfony/process": "^5.4" }, "require-dev": { diff --git a/helpers/functions.php b/helpers/functions.php index b820f1f9..96b06de5 100644 --- a/helpers/functions.php +++ b/helpers/functions.php @@ -12,16 +12,15 @@ * @since 2.9.9 */ -use Quantum\Libraries\HttpClient\Exceptions\HttpClientException; -use Quantum\Libraries\Storage\Factories\FileSystemFactory; +use Quantum\HttpClient\Exceptions\HttpClientException; +use Quantum\Storage\Factories\FileSystemFactory; use Quantum\Config\Exceptions\ConfigException; -use Quantum\Libraries\HttpClient\HttpClient; use Quantum\App\Exceptions\BaseException; use Quantum\Di\Exceptions\DiException; -use Quantum\Http\Request; +use Quantum\HttpClient\HttpClient; /** - * Gets the url with selected language + * Gets the url with the selected language * @param string $lang * @return string */ @@ -132,7 +131,7 @@ function nav_ref_encode(?string $query): string /** * Decodes a URL-safe base64 reference back to query string. * - * @param string $ref + * @param string|null $ref * @return string */ function nav_ref_decode(?string $ref): string diff --git a/tests/_root/logs/app.log b/logs/app.log similarity index 100% rename from tests/_root/logs/app.log rename to logs/app.log diff --git a/migrations/create_table_comments_1698145440.php b/migrations/create_table_comments_1698145440.php index a7b99510..94525cf5 100644 --- a/migrations/create_table_comments_1698145440.php +++ b/migrations/create_table_comments_1698145440.php @@ -1,6 +1,6 @@ * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) * @link http://quantum.softberg.org/ - * @since 2.9.9 + * @since 3.0.0 */ namespace Shared\Commands; -use Quantum\Libraries\Validation\Validator; +use Quantum\Validation\Validator; /** * Trait CommandValidationTrait @@ -26,7 +26,7 @@ trait CommandValidationTrait /** * @var Validator */ - protected $validator; + protected Validator $validator; /** * Initiates the validator diff --git a/shared/Commands/CommentCreateCommand.php b/shared/Commands/CommentCreateCommand.php index 5ef470c1..eb4dc1a9 100644 --- a/shared/Commands/CommentCreateCommand.php +++ b/shared/Commands/CommentCreateCommand.php @@ -9,17 +9,17 @@ * @author Arman Ag. * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) * @link http://quantum.softberg.org/ - * @since 2.9.9 + * @since 3.0.0 */ namespace Shared\Commands; use Quantum\Service\Exceptions\ServiceException; use Quantum\App\Exceptions\BaseException; -use Quantum\Libraries\Validation\Rule; use Quantum\Di\Exceptions\DiException; use Shared\Services\CommentService; use Quantum\Console\QtCommand; +use Quantum\Validation\Rule; use ReflectionException; /** @@ -33,27 +33,27 @@ class CommentCreateCommand extends QtCommand /** * Command name - * @var string + * @var string|null */ - protected $name = 'comment:create'; + protected ?string $name = 'comment:create'; /** * Command description - * @var string + * @var string|null */ - protected $description = 'Allows to create a comment record'; + protected ?string $description = 'Allows to create a comment record'; /** * Command help text - * @var string + * @var string|null */ - protected $help = 'Use the following format to create a comment record:' . PHP_EOL . 'php qt comment:create `Post UUID` `User UUID` `Content`'; + protected ?string $help = 'Use the following format to create a comment record:' . PHP_EOL . 'php qt comment:create `Post UUID` `User UUID` `Content`'; /** * Command arguments * @var array[] */ - protected $args = [ + protected array $args = [ ['post_uuid', 'required', 'The post uuid the comment belongs to'], ['user_uuid', 'required', 'The user uuid who writes the comment'], ['content', 'required', 'Comment text'], diff --git a/shared/Commands/CommentDeleteCommand.php b/shared/Commands/CommentDeleteCommand.php index 59e82585..c4cf86f6 100644 --- a/shared/Commands/CommentDeleteCommand.php +++ b/shared/Commands/CommentDeleteCommand.php @@ -9,7 +9,7 @@ * @author Arman Ag. * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) * @link http://quantum.softberg.org/ - * @since 2.9.9 + * @since 3.0.0 */ namespace Shared\Commands; @@ -29,21 +29,21 @@ class CommentDeleteCommand extends QtCommand { /** * Command name - * @var string + * @var string|null */ - protected $name = 'comment:delete'; + protected ?string $name = 'comment:delete'; /** * Command description - * @var string + * @var string|null */ - protected $description = 'Deletes a comment by UUID or clears the entire comments table with confirmation'; + protected ?string $description = 'Deletes a comment by UUID or clears the entire comments table with confirmation'; /** * Command help text - * @var string + * @var string|null */ - protected $help = << * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) * @link http://quantum.softberg.org/ - * @since 2.9.9 + * @since 3.0.0 */ namespace Shared\Commands; -use Quantum\Libraries\HttpClient\Exceptions\HttpClientException; use Symfony\Component\Console\Exception\ExceptionInterface; -use Quantum\Libraries\Storage\Factories\FileSystemFactory; +use Quantum\HttpClient\Exceptions\HttpClientException; use Symfony\Component\Console\Helper\ProgressBar; +use Quantum\Storage\Factories\FileSystemFactory; use Quantum\Service\Exceptions\ServiceException; use Symfony\Component\Console\Output\NullOutput; use Symfony\Component\Console\Input\ArrayInput; use Quantum\Config\Exceptions\ConfigException; use Quantum\App\Exceptions\BaseException; -use Quantum\Libraries\Database\Database; use Bluemmb\Faker\PicsumPhotosProvider; use Quantum\Di\Exceptions\DiException; use Symfony\Component\Process\Process; +use Quantum\Database\Database; use Quantum\Console\QtCommand; use Ottaviano\Faker\Gravatar; use ReflectionException; @@ -42,51 +42,51 @@ class DemoCommand extends QtCommand { + /** + * Default password for generated users + */ + const DEFAULT_PASSWORD = 'password'; + /** * Command name - * @var string + * @var string|null */ - protected $name = 'install:demo'; + protected ?string $name = 'install:demo'; /** * Command description - * @var string + * @var string|null */ - protected $description = 'Generates demo users and posts'; + protected ?string $description = 'Generates demo users and posts'; /** * Command help text - * @var string + * @var string|null */ - protected $help = 'The command will create demo users and posts for your project'; + protected ?string $help = 'The command will create demo users and posts for your project'; /** * The default action for all confirmations * @var array */ - protected $options = [ + protected array $options = [ ['yes', 'y', 'none', 'Acceptance of the confirmations'] ]; /** * @var Generator */ - protected $faker; + protected Generator $faker; /** * @var array */ - private $generatedUsers = []; + private array $generatedUsers = []; /** * @var array */ - private $generatedPosts = []; - - /** - * Default password for generated users - */ - const DEFAULT_PASSWORD = 'password'; + private array $generatedPosts = []; /** * Static values for user and post counts. diff --git a/shared/Commands/PostCreateCommand.php b/shared/Commands/PostCreateCommand.php index 0bcaebb9..46046ff0 100644 --- a/shared/Commands/PostCreateCommand.php +++ b/shared/Commands/PostCreateCommand.php @@ -9,17 +9,17 @@ * @author Arman Ag. * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) * @link http://quantum.softberg.org/ - * @since 2.9.9 + * @since 3.0.0 */ namespace Shared\Commands; use Quantum\Service\Exceptions\ServiceException; use Quantum\App\Exceptions\BaseException; -use Quantum\Libraries\Validation\Rule; use Quantum\Di\Exceptions\DiException; use Shared\Services\PostService; use Quantum\Console\QtCommand; +use Quantum\Validation\Rule; use ReflectionException; /** @@ -33,27 +33,27 @@ class PostCreateCommand extends QtCommand /** * Command name - * @var string + * @var string|null */ - protected $name = 'post:create'; + protected ?string $name = 'post:create'; /** * Command description - * @var string + * @var string|null */ - protected $description = 'Allows to create a post record'; + protected ?string $description = 'Allows to create a post record'; /** * Command help text - * @var string + * @var string|null */ - protected $help = 'Use the following format to create a post record:' . PHP_EOL . 'php qt post:create `Title` `Description` `[Image]` `[Author]`'; + protected ?string $help = 'Use the following format to create a post record:' . PHP_EOL . 'php qt post:create `Title` `Description` `[Image]` `[Author]`'; /** * Command arguments * @var array[] */ - protected $args = [ + protected array $args = [ ['title', 'required', 'Post title'], ['description', 'required', 'Post description'], ['user_uuid', 'required', 'Author uuid'], @@ -63,10 +63,7 @@ class PostCreateCommand extends QtCommand /** * Executes the command - * @throws DiException - * @throws ReflectionException - * @throws ServiceException - * @throws BaseException + * @throws BaseException|DiException|ServiceException|ReflectionException */ public function exec() { diff --git a/shared/Commands/PostDeleteCommand.php b/shared/Commands/PostDeleteCommand.php index 3961b191..21a85878 100644 --- a/shared/Commands/PostDeleteCommand.php +++ b/shared/Commands/PostDeleteCommand.php @@ -9,7 +9,7 @@ * @author Arman Ag. * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) * @link http://quantum.softberg.org/ - * @since 2.9.9 + * @since 3.0.0 */ namespace Shared\Commands; @@ -30,21 +30,21 @@ class PostDeleteCommand extends QtCommand /** * Command name - * @var string + * @var string|null */ - protected $name = 'post:delete'; + protected ?string $name = 'post:delete'; /** * Command description - * @var string + * @var string|null */ - protected $description = 'Deletes a post by UUID or clears the entire posts table with confirmation'; + protected ?string $description = 'Deletes a post by UUID or clears the entire posts table with confirmation'; /** * Command help text - * @var string + * @var string|null */ - protected $help = << * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) * @link http://quantum.softberg.org/ - * @since 2.9.9 + * @since 3.0.0 */ namespace Shared\Commands; use Quantum\Service\Exceptions\ServiceException; use Quantum\App\Exceptions\BaseException; -use Quantum\Libraries\Validation\Rule; use Quantum\Di\Exceptions\DiException; use Shared\Services\PostService; use Quantum\Console\QtCommand; +use Quantum\Validation\Rule; use ReflectionException; /** @@ -33,27 +33,27 @@ class PostUpdateCommand extends QtCommand /** * Command name - * @var string + * @var string|null */ - protected $name = 'post:update'; + protected ?string $name = 'post:update'; /** * Command description - * @var string + * @var string|null */ - protected $description = 'Allows to update a post record'; + protected ?string $description = 'Allows to update a post record'; /** * Command help text - * @var string + * @var string|null */ - protected $help = 'Use the following format to update the post:' . PHP_EOL . 'php qt post:update `[Post uuid]` -t `Title` -d `Description` [-i `Image`] [-a `Author`]'; + protected ?string $help = 'Use the following format to update the post:' . PHP_EOL . 'php qt post:update `[Post uuid]` -t `Title` -d `Description` [-i `Image`] [-a `Author`]'; /** * Command arguments * @var array */ - protected $args = [ + protected array $args = [ ['uuid', 'required', 'Post uuid'] ]; @@ -61,7 +61,7 @@ class PostUpdateCommand extends QtCommand * Command options * @var array */ - protected $options = [ + protected array $options = [ ['title', 't', 'optional', 'Post title'], ['description', 'd', 'optional', 'Post description'], ['image', 'i', 'optional', 'Post image'], @@ -69,10 +69,7 @@ class PostUpdateCommand extends QtCommand /** * Executes the command - * @throws DiException - * @throws ReflectionException - * @throws ServiceException - * @throws BaseException + * @throws BaseException|DiException|ServiceException|ReflectionException */ public function exec() { diff --git a/shared/Commands/UserCreateCommand.php b/shared/Commands/UserCreateCommand.php index e45e09b7..f96fe3ae 100644 --- a/shared/Commands/UserCreateCommand.php +++ b/shared/Commands/UserCreateCommand.php @@ -9,7 +9,7 @@ * @author Arman Ag. * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) * @link http://quantum.softberg.org/ - * @since 2.9.9 + * @since 3.0.0 */ namespace Shared\Commands; @@ -17,10 +17,11 @@ use Quantum\Service\Exceptions\ServiceException; use Quantum\App\Exceptions\BaseException; use Quantum\Di\Exceptions\DiException; -use Quantum\Libraries\Validation\Rule; -use Quantum\Libraries\Hasher\Hasher; +use Quantum\Validation\Validator; use Shared\Services\AuthService; use Quantum\Console\QtCommand; +use Quantum\Validation\Rule; +use Quantum\Hasher\Hasher; use ReflectionException; use Shared\Models\User; @@ -35,29 +36,29 @@ class UserCreateCommand extends QtCommand /** * Command name - * @var string + * @var string|null */ - protected $name = 'user:create'; + protected ?string $name = 'user:create'; /** * Command description - * @var string + * @var string|null */ - protected $description = 'Allows to create a user'; + protected ?string $description = 'Allows to create a user'; /** * Command help text - * @var string + * @var string|null */ - protected $help = 'Use the following format to create a user record:' . PHP_EOL . 'php qt user:create `Email` `Password` `[Role]` `[Firstname]` `[Lastname]`'; + protected ?string $help = 'Use the following format to create a user record:' . PHP_EOL . 'php qt user:create `Email` `Password` `[Role]` `[Firstname]` `[Lastname]`'; - protected $validator; + protected Validator $validator; /** * Command arguments * @var array[] */ - protected $args = [ + protected array $args = [ ['email', 'required', 'User email'], ['password', 'required', 'User password'], ['firstname', 'required', 'User firstname'], diff --git a/shared/Commands/UserDeleteCommand.php b/shared/Commands/UserDeleteCommand.php index 5aefe502..c74527f5 100644 --- a/shared/Commands/UserDeleteCommand.php +++ b/shared/Commands/UserDeleteCommand.php @@ -9,7 +9,7 @@ * @author Arman Ag. * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) * @link http://quantum.softberg.org/ - * @since 2.9.9 + * @since 3.0.0 */ namespace Shared\Commands; @@ -30,21 +30,21 @@ class UserDeleteCommand extends QtCommand /** * Command name - * @var string + * @var string|null */ - protected $name = 'user:delete'; + protected ?string $name = 'user:delete'; /** * Command description - * @var string + * @var string|null */ - protected $description = 'Deletes a user by UUID or clears the entire posts table with confirmation'; + protected ?string $description = 'Deletes a user by UUID or clears the entire posts table with confirmation'; /** * Command help text - * @var string + * @var string|null */ - protected $help = << * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) * @link http://quantum.softberg.org/ - * @since 2.9.9 + * @since 3.0.0 */ namespace Shared\Models; -use Quantum\Libraries\Database\Enums\Relation; +use Quantum\Model\Traits\HasTimestamps; use Quantum\Model\Traits\SoftDeletes; -use Quantum\Model\QtModel; +use Quantum\Database\Enums\Relation; +use Quantum\Model\DbModel; /** * Class Comment * @package Shared\Models */ -class Comment extends QtModel +class Comment extends DbModel { + use HasTimestamps; use SoftDeletes; /** * ID column of table * @var string */ - public $idColumn = 'id'; + public string $idColumn = 'id'; /** * The table name * @var string */ - public $table = 'comments'; + public string $table = 'comments'; /** * Fillable properties * @var array */ - public $fillable = [ + public array $fillable = [ 'uuid', 'post_uuid', 'user_uuid', 'content', - 'created_at', - 'updated_at' ]; /** diff --git a/shared/Models/Post.php b/shared/Models/Post.php index 0ac14cfc..4227b7cb 100644 --- a/shared/Models/Post.php +++ b/shared/Models/Post.php @@ -9,48 +9,48 @@ * @author Arman Ag. * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) * @link http://quantum.softberg.org/ - * @since 2.9.9 + * @since 3.0.0 */ namespace Shared\Models; -use Quantum\Libraries\Database\Enums\Relation; +use Quantum\Model\Traits\HasTimestamps; use Quantum\Model\Traits\SoftDeletes; -use Quantum\Model\QtModel; +use Quantum\Database\Enums\Relation; +use Quantum\Model\DbModel; /** * Class Post * @package Shared\Models */ -class Post extends QtModel +class Post extends DbModel { + use HasTimestamps; use SoftDeletes; /** * ID column of table * @var string */ - public $idColumn = 'id'; + public string $idColumn = 'id'; /** * The table name * @var string */ - public $table = 'posts'; + public string $table = 'posts'; /** * Fillable properties * @var array */ - public $fillable = [ + public array $fillable = [ 'uuid', 'user_uuid', 'title', 'content', 'image', - 'created_at', - 'updated_at', ]; /** diff --git a/shared/Models/User.php b/shared/Models/User.php index 9830aab7..e6fba2ab 100644 --- a/shared/Models/User.php +++ b/shared/Models/User.php @@ -14,32 +14,35 @@ namespace Shared\Models; -use Quantum\Model\QtModel; +use Quantum\Model\Traits\HasTimestamps; +use Quantum\Model\DbModel; /** * Class User * @package Shared\Models */ -class User extends QtModel +class User extends DbModel { + use HasTimestamps; + /** * ID column of table * @var string */ - public $idColumn = 'id'; + public string $idColumn = 'id'; /** * The table name * @var string */ - public $table = 'users'; + public string $table = 'users'; /** * Fillable properties * @var array */ - public $fillable = [ + public array $fillable = [ 'uuid', 'firstname', 'lastname', @@ -55,7 +58,5 @@ class User extends QtModel 'otp', 'otp_expires', 'otp_token', - 'created_at', - 'updated_at', ]; } \ No newline at end of file diff --git a/shared/Services/AuthService.php b/shared/Services/AuthService.php index e72f7ae4..c669b7af 100644 --- a/shared/Services/AuthService.php +++ b/shared/Services/AuthService.php @@ -9,20 +9,19 @@ * @author Arman Ag. * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) * @link http://quantum.softberg.org/ - * @since 2.9.9 + * @since 3.0.0 */ namespace Shared\Services; -use Quantum\Libraries\Auth\Contracts\AuthServiceInterface; +use Quantum\Auth\Contracts\AuthServiceInterface; use Quantum\Config\Exceptions\ConfigException; use Quantum\Model\Exceptions\ModelException; -use Quantum\Libraries\Auth\User as AuthUser; use Quantum\App\Exceptions\BaseException; use Quantum\Di\Exceptions\DiException; +use Quantum\Auth\User as AuthUser; use Quantum\Model\ModelCollection; use Quantum\Service\QtService; -use Quantum\Model\QtModel; use ReflectionException; use Shared\Models\User; @@ -34,9 +33,9 @@ class AuthService extends QtService implements AuthServiceInterface { /** - * @var QtModel + * @var User */ - private $model; + private User $model; /** * @throws BaseException @@ -95,12 +94,11 @@ public function get(string $field, $value): ?AuthUser public function add(array $data): AuthUser { $data['uuid'] = $data['uuid'] ?? uuid_ordered(); - $data['created_at'] = date('Y-m-d H:i:s'); $this->createUserDirectory($data['uuid']); $user = $this->model->create(); - $user->fillObjectProps($data); + $user->fill($data); $user->save(); return (new AuthUser())->setData($data); @@ -121,9 +119,9 @@ public function update(string $field, ?string $value, array $data): ?AuthUser return null; } - $data['updated_at'] = date('Y-m-d H:i:s'); + unset($data['id']); - $user->fillObjectProps($data); + $user->fill($data); $user->save(); return (new AuthUser())->setData($this->model->findOneBy($field, $value)->asArray()); @@ -144,7 +142,7 @@ public function delete(string $uuid): bool */ public function deleteAllUsers() { - $this->model->deleteTable(); + $this->model->truncate(); } /** diff --git a/shared/Services/CommentService.php b/shared/Services/CommentService.php index 969601d1..98931fad 100644 --- a/shared/Services/CommentService.php +++ b/shared/Services/CommentService.php @@ -93,13 +93,12 @@ public function getComment(string $uuid): Comment public function addComment(array $data): array { $data['uuid'] = $data['uuid'] ?? uuid_ordered(); - $data['created_at'] = date('Y-m-d H:i:s'); $comment = $this->model->create(); - $comment->fillObjectProps($data); + $comment->fill($data); $comment->save(); - return $data; + return $comment->asArray(); } /** @@ -118,7 +117,7 @@ public function deleteComment(string $uuid): bool */ public function deleteAllComments() { - $this->model->deleteTable(); + $this->model->truncate(); } /** diff --git a/shared/Services/PostService.php b/shared/Services/PostService.php index b78b7e46..a7b26726 100644 --- a/shared/Services/PostService.php +++ b/shared/Services/PostService.php @@ -9,26 +9,25 @@ * @author Arman Ag. * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) * @link http://quantum.softberg.org/ - * @since 2.9.9 + * @since 3.0.0 */ namespace Shared\Services; -use Quantum\Libraries\Storage\Exceptions\FileUploadException; -use Quantum\Libraries\Storage\Exceptions\FileSystemException; -use Quantum\Libraries\Storage\Factories\FileSystemFactory; +use Quantum\Storage\Exceptions\FileUploadException; +use Quantum\Storage\Exceptions\FileSystemException; +use Quantum\Storage\Factories\FileSystemFactory; use Quantum\Environment\Exceptions\EnvException; use Quantum\Config\Exceptions\ConfigException; use Quantum\Model\Exceptions\ModelException; -use Quantum\Libraries\Storage\UploadedFile; use Quantum\App\Exceptions\BaseException; -use Quantum\Model\Factories\ModelFactory; use Shared\Transformers\PostTransformer; use Quantum\Di\Exceptions\DiException; use Quantum\Model\ModelCollection; +use Quantum\Storage\UploadedFile; use Gumlet\ImageResizeException; use Quantum\Service\QtService; -use Quantum\Model\QtModel; +use Quantum\Model\DbModel; use ReflectionException; use Shared\Models\User; use Shared\Models\Post; @@ -41,14 +40,14 @@ class PostService extends QtService { /** - * @var QtModel + * @var DbModel */ private $model; /** * @var PostTransformer */ - private $transformer; + private PostTransformer $transformer; /** * @param PostTransformer $transformer @@ -161,10 +160,9 @@ public function getMyPosts(string $userUuid): ?ModelCollection public function addPost(array $data): Post { $data['uuid'] = $data['uuid'] ?? uuid_ordered(); - $data['created_at'] = date('Y-m-d H:i:s'); $post = $this->model->create(); - $post->fillObjectProps($data); + $post->fill($data); $post->save(); return $this->getPost($data['uuid']); @@ -180,10 +178,8 @@ public function addPost(array $data): Post */ public function updatePost(string $uuid, array $data): Post { - $data['updated_at'] = date('Y-m-d H:i:s'); - $post = $this->model->findOneBy('uuid', $uuid); - $post->fillObjectProps($data); + $post->fill($data); $post->save(); return $this->getPost($post->uuid); @@ -193,7 +189,7 @@ public function updatePost(string $uuid, array $data): Post * Deletes post * @param string $uuid * @return bool - * @throws ModelException + * @throws ModelException|BaseException */ public function deletePost(string $uuid): bool { @@ -206,7 +202,7 @@ public function deletePost(string $uuid): bool */ public function deleteAllPosts() { - $this->model->deleteTable(); + $this->model->truncate(); } /** diff --git a/shared/Transformers/CommentTransformer.php b/shared/Transformers/CommentTransformer.php index f29c85c1..582e0077 100644 --- a/shared/Transformers/CommentTransformer.php +++ b/shared/Transformers/CommentTransformer.php @@ -9,12 +9,12 @@ * @author Arman Ag. * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) * @link http://quantum.softberg.org/ - * @since 2.9.9 + * @since 3.0.0 */ namespace Shared\Transformers; -use Quantum\Libraries\Transformer\Contracts\TransformerInterface; +use Quantum\Transformer\Contracts\TransformerInterface; /** * Class CommentTransformer diff --git a/shared/Transformers/PostTransformer.php b/shared/Transformers/PostTransformer.php index 930aacd5..f328cd34 100644 --- a/shared/Transformers/PostTransformer.php +++ b/shared/Transformers/PostTransformer.php @@ -9,12 +9,12 @@ * @author Arman Ag. * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) * @link http://quantum.softberg.org/ - * @since 2.9.0 + * @since 3.0.0 */ namespace Shared\Transformers; -use Quantum\Libraries\Transformer\Contracts\TransformerInterface; +use Quantum\Transformer\Contracts\TransformerInterface; /** * Class PostTransformer diff --git a/tests/Feature/AppTestCase.php b/tests/Feature/AppTestCase.php index cb862a35..0e7a4241 100644 --- a/tests/Feature/AppTestCase.php +++ b/tests/Feature/AppTestCase.php @@ -6,29 +6,27 @@ use Quantum\Http\Response; use Quantum\Http\Request; use Quantum\App\App; +use Quantum\Di\Di; class AppTestCase extends TestCase { - protected $defaultEmail = 'default@quantumphp.io'; + protected string $defaultEmail = 'default@quantumphp.io'; - protected $defaultPassword = 'password'; + protected string $defaultPassword = 'password'; - protected $firstname = 'John'; + protected string $firstname = 'John'; - protected $lastname = 'Doe'; + protected string $lastname = 'Doe'; - protected static $app; - - public static function setUpBeforeClass(): void - { - self::$app = createApp(App::WEB, PROJECT_ROOT); - } + protected static App $app; public function setUp(): void { parent::setUp(); ob_start(); + + self::$app = createApp(App::WEB, PROJECT_ROOT); } public function tearDown(): void @@ -45,6 +43,7 @@ public function request( array $files = [] ): Response { + Di::resetContainer(); Request::create($method, $url, $params, $headers, $files); self::$app->start(); return new Response(); diff --git a/tests/Feature/modules/Api/AuthControllerTest.php b/tests/Feature/modules/Api/AuthControllerTest.php index 75238a55..ac880d06 100644 --- a/tests/Feature/modules/Api/AuthControllerTest.php +++ b/tests/Feature/modules/Api/AuthControllerTest.php @@ -7,12 +7,13 @@ use Quantum\Http\Response; use Quantum\Http\Request; use Shared\Models\User; +use Faker\Generator; use Faker\Factory; class AuthControllerTest extends AppTestCase { - private $faker; + private Generator $faker; public function setUp(): void { @@ -180,16 +181,17 @@ public function testModuleApiResetEndpointWithIncorrectToken() public function testModuleApiVerifyEndpoint() { $email = $this->faker->email(); - $otp = $this->faker->uuid(); + $otp = random_int(100000, 999999); - $user = createUser([ - 'email' => $email, + createUser([ + 'email' => $email, 'otp_token' => base64_encode($otp), - 'otp' => $otp, + 'otp' => (string)$otp, + 'otp_expires' => date('Y-m-d H:i:s', time() + 300), // 5 minutes from now ]); $response = $this->request('post', '/api/en/verify', [ - 'otp' => $otp, + 'otp' => (string)$otp, 'code' => base64_encode($otp), ]); @@ -205,7 +207,7 @@ public function testModuleApiVerifyEndpoint() public function testModuleApiVerifyEndpointWithIncorrectOtp() { $response = $this->request('post', '/api/en/verify', [ - 'otp' => 'wrong-otp', + 'otp' => 'wrong-otp', 'code' => 'wrong-code', ]); diff --git a/tests/Feature/modules/Api/CommentControllerTest.php b/tests/Feature/modules/Api/CommentControllerTest.php index 7055e705..1180a8fd 100644 --- a/tests/Feature/modules/Api/CommentControllerTest.php +++ b/tests/Feature/modules/Api/CommentControllerTest.php @@ -2,7 +2,6 @@ namespace Quantum\Tests\Feature\modules\Api; - use Quantum\Model\Factories\ModelFactory; use Quantum\Tests\Feature\AppTestCase; use Shared\Models\Comment; @@ -12,13 +11,13 @@ class CommentControllerTest extends AppTestCase { - private $tokens = []; + private array $tokens = []; private $post = null; - public function setUp(): void - { - parent::setUp(); + public function setUp(): void + { + parent::setUp(); $this->tokens = $this->signInAndGetTokens(); @@ -30,15 +29,15 @@ public function setUp(): void Request::flush(); Response::flush(); - } + } public function tearDown(): void { parent::tearDown(); } - public function testModuleApiCommentCreateEndpoint() - { + public function testModuleApiCommentCreateEndpoint() + { $method = 'POST'; $endpoint = '/api/comments/create/'; $body = ['content' => 'My first comment']; @@ -58,19 +57,26 @@ public function testModuleApiCommentCreateEndpoint() $this->assertArrayHasKey('user_uuid', $comment); $this->assertArrayHasKey('post_uuid', $comment); $this->assertArrayHasKey('content', $comment); - $this->assertArrayHasKey('created_at', $comment); - } + + ModelFactory::get(Comment::class)->findOneBy('uuid', $comment['uuid'])->delete(); + } public function testModuleApiCommentDeleteEndpoint() { + $method = 'POST'; + $endpoint = '/api/comments/create/'; + $body = ['content' => 'Comment to be deleted']; + $headers = ['Authorization' => 'Bearer ' . $this->tokens['access_token']]; + + $response = $this->request($method, $endpoint . $this->post['uuid'], $body, $headers); + + $comment = $response->get('data'); + $method = 'DELETE'; $endpoint = '/api/comments/delete/'; $body = []; - $headers = ['Authorization' => 'Bearer ' . $this->tokens['access_token']]; - - $comment = ModelFactory::get(Comment::class)->first(); - $response = $this->request($method, $endpoint . $comment->uuid, $body, $headers); + $response = $this->request($method, $endpoint . $comment['uuid'], $body, $headers); $this->assertIsObject($response); diff --git a/tests/Feature/modules/Api/PostManagementControllerTest.php b/tests/Feature/modules/Api/PostManagementControllerTest.php index 5bc6ba93..cda567b8 100644 --- a/tests/Feature/modules/Api/PostManagementControllerTest.php +++ b/tests/Feature/modules/Api/PostManagementControllerTest.php @@ -91,6 +91,7 @@ public function testModuleApiPostCreateEndpoint() $this->assertArrayHasKey('date', $post); $this->assertArrayHasKey('author', $post); + ModelFactory::get(Post::class)->findOneBy('uuid', $post['uuid'])->delete(); } public function testModuleApiAmendPostEndpoint() @@ -127,20 +128,30 @@ public function testModuleApiAmendPostEndpoint() public function testModuleApiDeletePostEndpoint() { + $method = 'POST'; + $endpoint = '/api/my-posts/create'; + $body = [ + 'title' => 'Post to be deleted', + 'content' => 'Temporary post for deletion test', + 'image' => '', + ]; + $headers = ['Authorization' => 'Bearer ' . $this->tokens['access_token']]; + + $response = $this->request($method, $endpoint, $body, $headers); + + $post = $response->get('data'); + $method = 'DELETE'; $endpoint = '/api/my-posts/delete/'; $body = []; - $headers = ['Authorization' => 'Bearer ' . $this->tokens['access_token']]; - $post = ModelFactory::get(Post::class)->first(); + $response = $this->request($method, $endpoint . $post['uuid'], $body, $headers); - $response = $this->request($method, $endpoint . $post->uuid, $body, $headers); + $this->assertIsObject($response); - $this->assertIsObject($response); - - $this->assertEquals('success', $response->get('status')); + $this->assertEquals('success', $response->get('status')); - $this->assertEquals('Deleted successfully', $response->get('message')); + $this->assertEquals('Deleted successfully', $response->get('message')); } public function testModuleApiDeletePostImageEndpoint() diff --git a/tests/Helpers/functions.php b/tests/Helpers/functions.php index b08dab95..4c483dfd 100644 --- a/tests/Helpers/functions.php +++ b/tests/Helpers/functions.php @@ -1,18 +1,21 @@ addModuleConfig(); - Router::setCurrentRoute(['module' => $moduleName]); + $route = new Route(['GET'], 'dummy', null, null, function () {}); + $route->module($moduleName); + $matchedRoute = new MatchedRoute($route, []); + Request::setMatchedRoute($matchedRoute); ob_end_clean(); } diff --git a/tests/Unit/shared/Services/AuthServiceTest.php b/tests/Unit/shared/Services/AuthServiceTest.php index d986e324..ba56993d 100644 --- a/tests/Unit/shared/Services/AuthServiceTest.php +++ b/tests/Unit/shared/Services/AuthServiceTest.php @@ -2,8 +2,8 @@ namespace Quantum\Tests\Unit\shared\Services; -use Quantum\Libraries\Auth\User as AuthUser; use Quantum\Service\Factories\ServiceFactory; +use Quantum\Auth\User as AuthUser; use Quantum\Model\ModelCollection; use Shared\Services\AuthService; use PHPUnit\Framework\TestCase; @@ -21,8 +21,14 @@ public function setUp(): void $this->authService = ServiceFactory::create(AuthService::class); } + public function tearDown(): void + { + parent::tearDown(); + } + public function testAuthServiceGetAll() { + $this->authService = ServiceFactory::create(AuthService::class); $users = $this->authService->getAll(); $this->assertInstanceOf(ModelCollection::class, $users); diff --git a/tests/Unit/shared/Services/CommentServiceTest.php b/tests/Unit/shared/Services/CommentServiceTest.php index eae4da4e..f69b0bb9 100644 --- a/tests/Unit/shared/Services/CommentServiceTest.php +++ b/tests/Unit/shared/Services/CommentServiceTest.php @@ -2,7 +2,9 @@ namespace Quantum\Tests\Unit\shared\Services; +use Quantum\Model\Factories\ModelFactory; use Quantum\Service\Factories\ServiceFactory; +use Shared\Models\Comment; use Shared\Services\CommentService; use Quantum\Model\ModelCollection; use Shared\Services\AuthService; diff --git a/tests/Unit/shared/Services/PostServiceTest.php b/tests/Unit/shared/Services/PostServiceTest.php index a8d18c80..38b2e9df 100644 --- a/tests/Unit/shared/Services/PostServiceTest.php +++ b/tests/Unit/shared/Services/PostServiceTest.php @@ -2,9 +2,9 @@ namespace Quantum\Tests\Unit\shared\Services; -use Quantum\Libraries\Storage\UploadedFile; use Quantum\Service\Factories\ServiceFactory; use Quantum\Model\ModelCollection; +use Quantum\Storage\UploadedFile; use Quantum\Paginator\Paginator; use Shared\Services\AuthService; use Shared\Services\PostService; @@ -21,8 +21,6 @@ class PostServiceTest extends TestCase protected $postService; - private $initialPosts; - public function setUp(): void { parent::setUp(); @@ -84,7 +82,7 @@ public function testPostServiceGetPostsWithSearch() 'title' => $title, 'content' => $content, 'image' => '', - 'updated_at' => date('Y-m-d H:i:s'), + ]); $searchTerm = 'SEARCH_TOKEN'; @@ -115,8 +113,6 @@ public function testPostServiceGetSinglePost() $this->assertArrayHasKey('content', $postData); - $this->assertArrayHasKey('updated_at', $postData); - } public function testPostServiceGetMyPosts() @@ -145,7 +141,7 @@ public function testPostServiceAddNewPost() 'title' => 'Just another post', 'content' => 'Content of just another post', 'image' => '', - 'updated_at' => $date + ]); $post = $this->postService->getPost($newPost->uuid); @@ -154,7 +150,7 @@ public function testPostServiceAddNewPost() $this->assertEquals('Content of just another post', $post->content); - $this->assertEquals($date, $post->updated_at); + $this->postService->deletePost($newPost->uuid); } @@ -171,7 +167,6 @@ public function testPostServiceUpdatePost() 'title' => 'Walt Disney Jr.', 'content' => 'The best way to get started is to quit talking and begin doing.', 'image' => 'image.jpg', - 'updated_at' => $date ]); $post = $this->postService->getPost($uuid); @@ -184,7 +179,7 @@ public function testPostServiceUpdatePost() $this->assertEquals('image.jpg', $post->image); - $this->assertEquals($date, $post->updated_at); + } public function testPostServiceDeletePost() @@ -200,7 +195,6 @@ public function testPostServiceDeletePost() 'title' => 'Just another post', 'content' => 'Content of just another post', 'image' => '', - 'updated_at' => date('Y-m-d H:i:s') ]); $this->assertCount(11, $this->postService->getPosts()); @@ -214,12 +208,18 @@ public function testPostServiceSaveAndDeleteImage() { $this->fileMeta = [ 'size' => 500, - 'name' => 'foo.jpg', + 'name' => 'foo.jpeg', 'tmp_name' => base_dir() . DS . 'tmp' . DS . 'php8fe1.tmp', - 'type' => 'image/jpg', + 'type' => 'image/jpeg', 'error' => 0, ]; + if (!is_dir(base_dir() . DS . 'tmp')) { + mkdir(base_dir() . DS . 'tmp'); + } + + file_put_contents($this->fileMeta['tmp_name'], hex2bin('FFD8FFE0')); + $users = $this->authService->getAll(); $userUuid = $users->first()->uuid; diff --git a/tests/_root/.env.example b/tests/_root/.env.example index a48ae446..501460a9 100644 --- a/tests/_root/.env.example +++ b/tests/_root/.env.example @@ -1,3 +1,3 @@ -APP_KEY=XYZ1234567890 +APP_KEY=d4cf7e1c08d431d836dbc4bdc6f34bff4ac5988ee48ff4fdafe27e57b4156574 DEBUG=false TWO_FA=false \ No newline at end of file diff --git a/tests/_root/helpers/functions.php b/tests/_root/helpers/functions.php index c8888c8f..5bc6d94e 100644 --- a/tests/_root/helpers/functions.php +++ b/tests/_root/helpers/functions.php @@ -1,29 +1,8 @@ - * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) - * @link http://quantum.softberg.org/ - * @since 2.9.8 - */ - -use Quantum\Libraries\HttpClient\Exceptions\HttpClientException; -use Quantum\Libraries\Storage\Factories\FileSystemFactory; -use Quantum\Config\Exceptions\ConfigException; -use Quantum\Libraries\HttpClient\HttpClient; -use Quantum\App\Exceptions\BaseException; -use Quantum\Di\Exceptions\DiException; - -/** - * Gets the url with selected language - * @param string $lang - * @return string - */ +use Quantum\Storage\Factories\FileSystemFactory; +use Quantum\HttpClient\HttpClient; + function url_with_lang(string $lang): string { if (!in_array($lang, (array)config()->get('lang.supported'))) { @@ -57,23 +36,10 @@ function url_with_lang(string $lang): string $url .= '/' . ($index == $langSegmentIndex ? $lang : $segment); } } -dd($url); + return $url; } -/** - * Saves remote image - * @param string $imageUrl - * @param string $userDirectory - * @param string $imageName - * @return string - * @throws ErrorException - * @throws HttpClientException - * @throws ReflectionException - * @throws BaseException - * @throws ConfigException - * @throws DiException - */ function save_remote_image(string $imageUrl, string $userDirectory, string $imageName): string { $fs = FileSystemFactory::get(); @@ -91,11 +57,6 @@ function save_remote_image(string $imageUrl, string $userDirectory, string $imag return $imageName; } -/** - * Cleans up text for titles/descriptions. - * @param string $text - * @return array|string|string[] - */ function textCleanUp(string $text) { return str_replace(['"', '\'', '-'], '', $text); diff --git a/tests/_root/logs/.gitkeep b/tests/_root/logs/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/tests/_root/tmp/php8fe1.tmp b/tests/_root/tmp/php8fe1.tmp index e134ce2d..6255ad5e 100644 Binary files a/tests/_root/tmp/php8fe1.tmp and b/tests/_root/tmp/php8fe1.tmp differ diff --git a/tests/bootstrap.php b/tests/bootstrap.php index b3192e6b..930937ec 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -16,6 +16,8 @@ createApp(App::CONSOLE, PROJECT_ROOT); +removeModule(); + createModule('Api', 'DemoApi'); $user = createUser();