├── .github ├── ISSUE_TEMPLATE │ ├── bug.yml │ └── config.yml └── workflows │ └── run-tests.yml ├── .gitignore ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── SECURITY.md ├── app ├── Http │ └── Resources │ │ └── .gitkeep └── Models │ ├── Post.php │ └── User.php ├── build └── report.junit.xml ├── composer.json ├── composer.lock ├── config └── config.php ├── database └── migrations │ └── create_users_table.php.stub ├── phpunit.xml.dist ├── pint.json ├── src ├── Console │ ├── GenerateApiResourceCommand.php │ └── stubs │ │ └── api-resource.php.stub └── LaravelApiResourceGeneratorServiceProvider.php └── tests ├── Feature └── Test.php ├── Pest.php └── TestCase.php /.github/ISSUE_TEMPLATE/bug.yml: -------------------------------------------------------------------------------- 1 | name: Bug Report 2 | description: Report an Issue or Bug with the Package 3 | title: "[Bug]: " 4 | labels: ["bug"] 5 | body: 6 | - type: markdown 7 | attributes: 8 | value: | 9 | We're sorry to hear you have a problem. Can you help us solve it by providing the following details. 10 | - type: textarea 11 | id: what-happened 12 | attributes: 13 | label: What happened? 14 | description: What did you expect to happen? 15 | placeholder: I cannot currently do X thing because when I do, it breaks X thing. 16 | validations: 17 | required: true 18 | - type: textarea 19 | id: how-to-reproduce 20 | attributes: 21 | label: How to reproduce the bug 22 | description: How did this occur, please add any config values used and provide a set of reliable steps if possible. 23 | placeholder: When I do X I see Y. 24 | validations: 25 | required: true 26 | - type: input 27 | id: package-version 28 | attributes: 29 | label: Package Version 30 | description: What version of our Package are you running? Please be as specific as possible 31 | placeholder: 1.0.0 32 | validations: 33 | required: true 34 | - type: input 35 | id: php-version 36 | attributes: 37 | label: PHP Version 38 | description: What version of PHP are you running? Please be as specific as possible 39 | placeholder: 8.1.0 40 | validations: 41 | required: true 42 | - type: dropdown 43 | id: operating-systems 44 | attributes: 45 | label: Which operating systems does with happen with? 46 | description: You may select more than one. 47 | multiple: true 48 | options: 49 | - macOS 50 | - Windows 51 | - Linux 52 | - type: textarea 53 | id: notes 54 | attributes: 55 | label: Notes 56 | description: Use this field to provide any other notes that you feel might be relevant to the issue. 57 | validations: 58 | required: false 59 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | blank_issues_enabled: false 2 | contact_links: 3 | - name: Ask a question 4 | url: https://github.com/alibori/laravel-api-resource-generator/discussions/new?category=q-a 5 | about: Ask the community for help 6 | - name: Request a feature 7 | url: https://github.com/alibori/laravel-api-resource-generator/discussions/new?category=ideas 8 | about: Share ideas for new features 9 | - name: Report a security issue 10 | url: https://github.com/alibori/laravel-api-resource-generator/security/policy 11 | about: Learn how to notify us for sensitive bugs 12 | -------------------------------------------------------------------------------- /.github/workflows/run-tests.yml: -------------------------------------------------------------------------------- 1 | name: run-tests 2 | 3 | on: 4 | push: 5 | branches: [main] 6 | pull_request: 7 | branches: [main] 8 | 9 | jobs: 10 | test: 11 | runs-on: ${{ matrix.os }} 12 | strategy: 13 | fail-fast: true 14 | matrix: 15 | os: [ ubuntu-latest, windows-latest ] 16 | php: [ 8.2 ] 17 | laravel: [ 11.* ] 18 | stability: [ prefer-stable ] 19 | include: 20 | - laravel: 11.* 21 | testbench: 9.* 22 | carbon: ^2.63 23 | 24 | name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }} 25 | 26 | steps: 27 | - name: Checkout code 28 | uses: actions/checkout@v3 29 | 30 | - name: Setup PHP 31 | uses: shivammathur/setup-php@v2 32 | with: 33 | php-version: ${{ matrix.php }} 34 | extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo 35 | coverage: none 36 | 37 | - name: Setup problem matchers 38 | run: | 39 | echo "::add-matcher::${{ runner.tool_cache }}/php.json" 40 | echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" 41 | 42 | - name: Install dependencies 43 | run: | 44 | composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" "nesbot/carbon:${{ matrix.carbon }}" --no-interaction --no-update 45 | composer update --${{ matrix.stability }} --prefer-dist --no-interaction 46 | 47 | - name: List Installed Dependencies 48 | run: composer show -D 49 | 50 | - name: Execute tests 51 | run: vendor/bin/pest -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ### Laravel template 2 | vendor/ 3 | node_modules/ 4 | npm-debug.log 5 | yarn-error.log 6 | .phpunit.result.cache 7 | .idea/ -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `laravel-api-resource-generator` will be documented in this file. 4 | 5 | ## 1.4.2 - 2024-12-09 6 | 7 | - Fix bug on multiple resource generation. 8 | 9 | ## 1.4.1 - 2024-12-07 10 | 11 | - Fix dependencies to work with **Laravel 11** last version. 12 | 13 | ## 1.4.0 - 2024-04-16 14 | 15 | - Added option to generate multiple resources at once. 16 | 17 | ## 1.3.2 - 2024-03-08 18 | 19 | ### Added 20 | 21 | - Added support for **Laravel 11** 22 | 23 | ## 1.3.1 - 2023-07-29 24 | 25 | ### Fixed 26 | 27 | - Create resource's directory if it does not exist. 28 | 29 | ## 1.3.0 - 2023-03-24 30 | 31 | ### Added 32 | 33 | - Added option to set the array keys of the resource as *camelCase*. 34 | 35 | ## 1.2.0 - 2023-03-18 36 | 37 | ### Added 38 | 39 | - Added support for Laravel 10 40 | 41 | ### Changed 42 | 43 | - Updated command signature to use `api-resource:generate {model}`. 44 | 45 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | Copyright (c) Axel Libori Roch a.libori@gmail.com 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel API Resource Generator Package 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/alibori/laravel-api-resource-generator.svg?style=flat-square)](https://packagist.org/packages/alibori/laravel-api-resource-generator) 4 | ![Tests Passed](https://github.com/alibori/laravel-api-resource-generator/actions/workflows/run-tests.yml/badge.svg?event=push) 5 | [![Total Downloads](https://img.shields.io/packagist/dt/alibori/laravel-api-resource-generator.svg?style=flat-square)](https://packagist.org/packages/alibori/laravel-api-resource-generator) 6 | 7 | This package will help you to generate API resources for your Laravel project. 8 | 9 | ## Installation 10 | 11 | You can install the package via composer: 12 | 13 | ```bash 14 | composer require alibori/laravel-api-resource-generator --dev 15 | ``` 16 | 17 | ## Usage 18 | 19 | All you need to do is run the following command: 20 | 21 | ``` bash 22 | php artisan api-resource:generate 23 | ``` 24 | 25 | If you want to generate multiple resources at once, you can pass multiple model names separated by a comma: 26 | 27 | ``` bash 28 | php artisan api-resource:generate ,, 29 | ``` 30 | 31 | This command will generate a new resource for the given model/s name/s with the properties defined in the model. 32 | 33 | If you want to set the array keys of the resource as *camelCase*, you will be prompted to do it. 34 | 35 | For example, for the model named `User` you will get the following resource: 36 | 37 | ``` php 38 | $this->id, 71 | 'name' => $this->name, 72 | 'email' => $this->email, 73 | 'email_verified_at' => $this->email_verified_at, 74 | 'remember_token' => $this->remember_token, 75 | 'created_at' => $this->created_at, 76 | 'updated_at' => $this->updated_at 77 | ]; 78 | } 79 | } 80 | 81 | ``` 82 | 83 | ## Publish config file 84 | 85 | If you want to publish the config file to change the default namespace for the generated resources and the directory where the resources will be generated, you can run the following command: 86 | 87 | ``` bash 88 | php artisan vendor:publish --provider="Alibori\LaravelApiResourceGenerator\LaravelApiResourceGeneratorServiceProvider" --tag="config" 89 | ``` 90 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | ## Reporting a Vulnerability 4 | 5 | Please, feel free to report any security vulnerability you find in this package sending an email to [a.libori@gmail.com](mailto:a.libori@gmail.com). All security vulnerabilities will be promptly addressed. 6 | 7 | Thank you for helping make this package safe for everyone. 8 | -------------------------------------------------------------------------------- /app/Http/Resources/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alibori/laravel-api-resource-generator/3fc7139edc2d164063a889720ac975e49b357b7c/app/Http/Resources/.gitkeep -------------------------------------------------------------------------------- /app/Models/Post.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "alibori/laravel-api-resource-generator", 3 | "description": "Package to generate API resources from models.", 4 | "authors": [ 5 | { 6 | "name": "Axel Libori Roch", 7 | "email": "a.libori@gmail.com" 8 | } 9 | ], 10 | "type": "library", 11 | "autoload": { 12 | "psr-4": { 13 | "Alibori\\LaravelApiResourceGenerator\\": "src/" 14 | } 15 | }, 16 | "autoload-dev": { 17 | "psr-4": { 18 | "Alibori\\LaravelApiResourceGenerator\\App\\": "app/", 19 | "Alibori\\LaravelApiResourceGenerator\\Tests\\": "tests" 20 | } 21 | }, 22 | "extra": { 23 | "laravel": { 24 | "providers": [ 25 | "Alibori\\LaravelApiResourceGenerator\\LaravelApiResourceGeneratorServiceProvider" 26 | ] 27 | } 28 | }, 29 | "keywords": [ 30 | "laravel", 31 | "resources", 32 | "generator" 33 | ], 34 | "license": "MIT", 35 | "require": { 36 | "php": ">=8.2", 37 | "illuminate/support": "^11.15", 38 | "illuminate/console": "^11.15", 39 | "illuminate/filesystem": "^11.15", 40 | "illuminate/database": "^11.15", 41 | "barryvdh/reflection-docblock": "^2.1" 42 | }, 43 | "minimum-stability": "dev", 44 | "prefer-stable": true, 45 | "require-dev": { 46 | "orchestra/testbench": "^9.6", 47 | "laravel/pint": "^1.18", 48 | "pestphp/pest": "^3.6", 49 | "pestphp/pest-plugin-laravel": "^3.0" 50 | }, 51 | "scripts": { 52 | "test": "vendor/bin/pest", 53 | "test-coverage": "vendor/bin/pest --coverage-html coverage", 54 | "pint": "vendor/bin/pint" 55 | }, 56 | "config": { 57 | "allow-plugins": { 58 | "pestphp/pest-plugin": true 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /config/config.php: -------------------------------------------------------------------------------- 1 | [ 11 | 'dir' => 'app/Http/Resources', 12 | 'namespace' => 'App\\Http\\Resources', 13 | ], 14 | 'models' => [ 15 | 'dir' => 'app/Models', 16 | 'namespace' => 'App\\Models', 17 | ], 18 | ]; 19 | -------------------------------------------------------------------------------- /database/migrations/create_users_table.php.stub: -------------------------------------------------------------------------------- 1 | bigIncrements('id'); 13 | 14 | $table->string('name'); 15 | $table->string('email'); 16 | 17 | $table->timestamps(); 18 | }); 19 | 20 | Schema::create('posts', function (Blueprint $table) { 21 | $table->bigIncrements('id'); 22 | 23 | $table->string('title'); 24 | 25 | $table->timestamps(); 26 | }); 27 | } 28 | } -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | src/ 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | tests 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /pint.json: -------------------------------------------------------------------------------- 1 | { 2 | "preset": "psr12", 3 | "rules": { 4 | "align_multiline_comment": true, 5 | "array_indentation": true, 6 | "array_syntax": true, 7 | "blank_line_after_namespace": true, 8 | "blank_line_after_opening_tag": true, 9 | "combine_consecutive_issets": true, 10 | "combine_consecutive_unsets": true, 11 | "concat_space": true, 12 | "declare_parentheses": true, 13 | "declare_strict_types": true, 14 | "explicit_string_variable": true, 15 | "final_class": false, 16 | "final_internal_class": false, 17 | "fully_qualified_strict_types": true, 18 | "global_namespace_import": { 19 | "import_classes": true, 20 | "import_constants": true, 21 | "import_functions": true 22 | }, 23 | "is_null": true, 24 | "lambda_not_used_import": true, 25 | "logical_operators": true, 26 | "mb_str_functions": true, 27 | "method_chaining_indentation": true, 28 | "modernize_strpos": true, 29 | "new_with_braces": true, 30 | "no_empty_comment": true, 31 | "not_operator_with_space": true, 32 | "ordered_traits": true, 33 | "protected_to_private": true, 34 | "simplified_if_return": true, 35 | "strict_comparison": true, 36 | "ternary_to_null_coalescing": true, 37 | "trim_array_spaces": true, 38 | "use_arrow_functions": true, 39 | "void_return": true, 40 | "yoda_style": true 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/Console/GenerateApiResourceCommand.php: -------------------------------------------------------------------------------- 1 | files = $files; 49 | } 50 | 51 | /** 52 | * @throws BindingResolutionException 53 | * @throws FileNotFoundException 54 | */ 55 | public function handle(): void 56 | { 57 | $this->return_case = $this->choice('Which case do you want to use for the returned parameters?', ['snake_case', 'camelCase'], 0); 58 | 59 | $this->dir = $this->defaultResourcesDir(); 60 | $this->namespace = config('apiresourcegenerator.resources.namespace'); 61 | 62 | // If the model argument contains a comma, we assume it's a list of models. We will generate a resource for each model. 63 | if (Str::contains($this->argument('model'), ',')) { 64 | $models = explode(',', $this->argument('model')); 65 | 66 | foreach ($models as $model_classname) { 67 | $model = $this->loadModel($model_classname); 68 | 69 | $this->getPropertiesFromTable($model); 70 | 71 | $this->generateResource($model); 72 | } 73 | } else { 74 | $model = $this->loadModel($this->argument('model')); 75 | 76 | $this->getPropertiesFromTable($model); 77 | 78 | $this->generateResource($model); 79 | } 80 | } 81 | 82 | protected function getArguments(): array 83 | { 84 | return [ 85 | ['model', InputArgument::REQUIRED, 'The model class name.'], 86 | ]; 87 | } 88 | 89 | protected function defaultResourcesDir(): string 90 | { 91 | return config('apiresourcegenerator.resources.dir'); 92 | } 93 | 94 | /** 95 | * @throws BindingResolutionException 96 | */ 97 | protected function loadModel(string $model): Model 98 | { 99 | return $this->laravel->make(config('apiresourcegenerator.models.namespace').'\\'.$model); 100 | } 101 | 102 | protected function getPropertiesFromTable(Model $model): void 103 | { 104 | $table = $model->getTable(); 105 | $schema = $model->getConnection()->getSchemaBuilder(); 106 | $columns = $schema->getColumns($table); 107 | 108 | 109 | if (!$columns) { 110 | return; 111 | } 112 | 113 | $this->properties = []; 114 | $this->php_docs_properties = []; 115 | 116 | foreach ($columns as $column) { 117 | $name = $column['name']; 118 | 119 | if (in_array($name, $model->getDates())) { 120 | $type = 'string'; 121 | } else { 122 | // Match types to php equivalent 123 | $type = match ($column['type_name']) { 124 | 'tinyint', 'bit', 125 | 'integer', 'int', 'int4', 126 | 'smallint', 'int2', 127 | 'mediumint', 128 | 'bigint', 'int8' => 'int', 129 | 130 | 'boolean', 'bool' => 'bool', 131 | 132 | 'float', 'real', 'float4', 133 | 'double', 'float8' => 'float', 134 | 135 | default => 'string', 136 | }; 137 | } 138 | 139 | $this->properties[$name] = $name; 140 | $this->php_docs_properties[$name] = $type.' '.$name; 141 | } 142 | } 143 | 144 | /** 145 | * @throws FileNotFoundException 146 | */ 147 | protected function generateResource(Model $model): void 148 | { 149 | $class = get_class($model); 150 | $name = class_basename($class); 151 | $path = $this->dir.'/'.$name.'Resource'.'.php'; 152 | 153 | if ( ! $this->files->isDirectory($this->dir)) { 154 | $this->files->makeDirectory($this->dir, 0755, true); 155 | } 156 | 157 | if ($this->files->exists($path)) { 158 | if ($this->confirm('The resource already exists. Do you want to overwrite it? [y|N]')) { 159 | $this->files->delete($path); 160 | } else { 161 | return; 162 | } 163 | } 164 | 165 | $this->files->put($path, $this->buildResource($name)); 166 | 167 | $this->info("API Resource for {$class} created successfully."); 168 | } 169 | 170 | /** 171 | * @throws FileNotFoundException 172 | */ 173 | protected function buildResource(string $name): string 174 | { 175 | $properties = $this->properties; 176 | $doc_block = $this->generatePHPDocs(); 177 | $fields = ''; 178 | 179 | $properties_length = count($properties); 180 | $count = 0; 181 | foreach ($properties as $property) { 182 | $array_key = $property; 183 | 184 | if ('camelCase' === $this->return_case) { 185 | $array_key = Str::camel($property); 186 | } 187 | 188 | if ($count < 1) { 189 | $fields .= "'{$array_key}' => \$this->{$property},\n"; 190 | } elseif ($count < $properties_length - 1) { 191 | $fields .= "\t\t\t'{$array_key}' => \$this->{$property},\n"; 192 | } else { 193 | $fields .= "\t\t\t'{$array_key}' => \$this->{$property}"; 194 | } 195 | 196 | $count++; 197 | } 198 | 199 | $stub = $this->files->get($this->stub); 200 | 201 | $stub = str_replace('{{ docblock }}', $doc_block, $stub); 202 | $stub = str_replace('{{ class }}', $name.'Resource', $stub); 203 | $stub = str_replace('{{ namespace }}', $this->namespace, $stub); 204 | 205 | return str_replace('{{ fields }}', $fields, $stub); 206 | } 207 | 208 | protected function generatePHPDocs(): string 209 | { 210 | $phpdoc = new DocBlock('Resource generated by alibori/laravel-api-resource-generator'); 211 | 212 | foreach ($this->php_docs_properties as $name => $property) { 213 | $type = explode(' ', $property); 214 | $name = "\${$name}"; 215 | 216 | $attr = 'property'; 217 | 218 | $tag_line = trim("@{$attr} {$type[0]} {$name}"); 219 | $tag = Tag::createInstance($tag_line, $phpdoc); 220 | $phpdoc->appendTag($tag); 221 | } 222 | 223 | $serializer = new DocBlockSerializer(); 224 | $doc_comment = $serializer->getDocComment($phpdoc); 225 | 226 | return "{$doc_comment}"; 227 | } 228 | } 229 | -------------------------------------------------------------------------------- /src/Console/stubs/api-resource.php.stub: -------------------------------------------------------------------------------- 1 | mergeConfigFrom(__DIR__.'/../config/config.php', 'apiresourcegenerator'); 20 | } 21 | 22 | /** 23 | * Bootstrap the application services. 24 | * 25 | * @return void 26 | */ 27 | public function boot(): void 28 | { 29 | if ( ! $this->app->runningInConsole()) { 30 | return; 31 | } 32 | 33 | $this->commands([GenerateApiResourceCommand::class]); 34 | 35 | // Publish config file 36 | $this->publishes([ 37 | __DIR__.'/../config/config.php' => config_path('apiresourcegenerator.php'), 38 | ], 'config'); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /tests/Feature/Test.php: -------------------------------------------------------------------------------- 1 | get())->toBeEmpty(); 13 | }); 14 | 15 | it('database posts table has been created', function (): void { 16 | expect(DB::table('posts')->get())->toBeEmpty(); 17 | }); 18 | 19 | // Test to check if GenerateApiResourceCommand is working well 20 | it('can generate a resource', function (): void { 21 | $command = $this->app->make(GenerateApiResourceCommand::class); 22 | 23 | $this->runCommand($command, ['model' => 'User']); 24 | 25 | expect(file_exists(__DIR__.'/../../app/Http/Resources/UserResource.php'))->toBeTrue(); 26 | 27 | unlink(__DIR__.'/../../app/Http/Resources/UserResource.php'); 28 | }); 29 | 30 | // Test to check if GenerateApiResourceCommand is working well with multiple models 31 | it('can generate a resource for multiple models', function (): void { 32 | $command = $this->app->make(GenerateApiResourceCommand::class); 33 | 34 | $this->runCommand($command, ['model' => 'User,Post']); 35 | 36 | expect(file_exists(__DIR__.'/../../app/Http/Resources/UserResource.php'))->toBeTrue() 37 | ->and(file_get_contents(__DIR__.'/../../app/Http/Resources/UserResource.php'))->toContain('string $email') 38 | ->and(file_get_contents(__DIR__.'/../../app/Http/Resources/UserResource.php'))->not->toContain('string $title') 39 | ->and(file_exists(__DIR__.'/../../app/Http/Resources/PostResource.php'))->toBeTrue() 40 | ->and(file_get_contents(__DIR__.'/../../app/Http/Resources/PostResource.php'))->toContain('string $title') 41 | ->and(file_get_contents(__DIR__.'/../../app/Http/Resources/PostResource.php'))->not->toContain('string $name'); 42 | 43 | unlink(__DIR__.'/../../app/Http/Resources/UserResource.php'); 44 | unlink(__DIR__.'/../../app/Http/Resources/PostResource.php'); 45 | }); 46 | -------------------------------------------------------------------------------- /tests/Pest.php: -------------------------------------------------------------------------------- 1 | in('.'); 8 | -------------------------------------------------------------------------------- /tests/TestCase.php: -------------------------------------------------------------------------------- 1 | set('database.default', 'sqlite'); 30 | $config->set('database.connections.sqlite', [ 31 | 'driver' => 'sqlite', 32 | 'database' => ':memory:', 33 | 'prefix' => '', 34 | ]); 35 | 36 | // Set config values 37 | $config->set('apiresourcegenerator.resources.namespace', 'Alibori\\LaravelApiResourceGenerator\\App\\Http\\Resources'); 38 | $config->set('apiresourcegenerator.resources.dir', 'app/Http/Resources'); 39 | $config->set('apiresourcegenerator.models.namespace', 'Alibori\\LaravelApiResourceGenerator\\App\\Models'); 40 | $config->set('apiresourcegenerator.models.dir', 'app/Models'); 41 | 42 | // Create the users table 43 | include_once __DIR__.'/../database/migrations/create_users_table.php.stub'; 44 | (new CreateUsersTable())->up(); 45 | } 46 | 47 | protected function runCommand(Command $command, array $arguments = [], array $interactiveInput = []): CommandTester 48 | { 49 | $this->withoutMockingConsoleOutput(); 50 | 51 | $command->setLaravel($this->app); 52 | 53 | $tester = new CommandTester($command); 54 | $tester->setInputs($interactiveInput); 55 | 56 | $tester->execute($arguments); 57 | 58 | return $tester; 59 | } 60 | } 61 | --------------------------------------------------------------------------------