Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions boilerplate/stubs/backend/advanced-api-resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/**
* @extends <% if (modelClassName) { %>Resource\AbstractDatabaseResource<<%= modelClassName %>><% } else { %>Resource\AbstractResource<object><% } %>
*/
class <%= className %> extends <% if (modelClassName) { %>Resource\AbstractDatabaseResource<% } else { %>Resource\AbstractResource<% if (interfaces.length > 0) %> implements <%= interfaces.join(', ') %><% } %>
class <%= className %> extends <% if (modelClassName) { %>Resource\AbstractDatabaseResource<% } else { %>Resource\AbstractResource<% if (interfaces.length > 0) { %> implements <%= interfaces.join(', ') %><% } %><% } %>
{
public function type(): string
{
Expand All @@ -35,7 +35,7 @@ public function endpoints(): array
{
return [<% if (endpoints.includes('create')) { %>
Endpoint\Create::make()<% if (modelClassName) { %>
->can('create<%= modelClassName %>'),<% } %><% } %><% if (endpoints.includes('update')) { %>
->can('create<%= modelClassName %>')<% } %>,<% } %><% if (endpoints.includes('update')) { %>
Endpoint\Update::make()
->can('update'),<% } %><% if (endpoints.includes('delete')) { %>
Endpoint\Delete::make()
Expand Down
2 changes: 1 addition & 1 deletion src/steps/stubs/backend/advanced-api-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class GenerateAdvancedApiResourceStub extends BasePhpStubStep {
type: 'multiselect',
message: 'Endpoints',
choices: [
{ title: 'index', value: 'index', selected: true },
{ title: 'index', value: 'list', selected: true },
{ title: 'show', value: 'show', selected: true },
{ title: 'create', value: 'create', selected: true },
{ title: 'update', value: 'update', selected: true },
Expand Down
40 changes: 40 additions & 0 deletions test/steps/generate/stubs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getExtFileContents, getFsPaths, runStep } from '../../boilersmith/utils
import { GenerateEventListenerStub } from '../../../src/steps/stubs/backend/event-listener';
import { GenerateControllerStub } from '../../../src/steps/stubs/backend/controller';
import { GenerateApiResourceStub } from '../../../src/steps/stubs/backend/api-resource';
import { GenerateAdvancedApiResourceStub } from '../../../src/steps/stubs/backend/advanced-api-resource';
import { GenerateHandlerStub } from '../../../src/steps/stubs/backend/handler';
import { GenerateHandlerCommandStub } from '../../../src/steps/stubs/backend/handler-command';
import { GenerateIntegrationTestStub } from '../../../src/steps/stubs/backend/integration-test';
Expand Down Expand Up @@ -526,3 +527,42 @@ describe('A model and its factory', function () {
expect(model).toContain(' use HasFactory;');
});
});

describe('An API resource made by the 2.0 upgrade', function () {
const stubDir = resolve(__dirname, '../../../boilerplate/stubs');

async function resource(params: Record<string, unknown>): Promise<string> {
const { fs } = await runStep(
new GenerateAdvancedApiResourceStub(stubDir, scaffolder),
{ php: stubPhpProviderFactory() },
{ usePrompts: false, initialParams: { className: 'PotatoResource', relations: [], ...params } },
initialFilesCallback
);

return getExtFileContents(fs, 'src/Api/Resource/PotatoResource.php');
}

test('Without a model or endpoints, it implements nothing', async function () {
const code = await resource({ modelClass: '', modelClassName: '', modelType: 'potatoes', endpoints: [] });

expect(code).toContain('class PotatoResource extends Resource\\AbstractResource\n{');
});

test('Without a model, it implements a contract for each endpoint', async function () {
const code = await resource({ modelClass: '', modelClassName: '', modelType: 'potatoes', endpoints: ['create', 'list'] });

expect(code).toContain(
'class PotatoResource extends Resource\\AbstractResource implements Resource\\Contracts\\Creatable, Resource\\Contracts\\Listable\n{'
);
expect(code).toContain(' Endpoint\\Create::make(),\n Endpoint\\Index::make()\n ->paginate(),\n');
});

test('With a model, only creating needs a permission', async function () {
const code = await resource({ modelClass: '\\Flarum\\Potato\\Potato', endpoints: ['create', 'list'] });

expect(code).toContain('class PotatoResource extends Resource\\AbstractDatabaseResource\n{');
expect(code).toContain(
" Endpoint\\Create::make()\n ->can('createPotato'),\n Endpoint\\Index::make()\n ->paginate(),\n"
);
});
});
Loading