Skip to content

Conversation

Pr3d4dor
Copy link
Contributor

@Pr3d4dor Pr3d4dor commented May 4, 2020

I haven't implemented the tests for the generated resources because of the problem in the issue #172

Also because of the issue #172 the generated tests looks incomplete.

Closes #171

@jasonmccreary
Copy link
Collaborator

I'm not sure I understand what/how this fixes something. It'd be nice to know what you found the problem. Otherwise, it looks like this just changes everything to kabab case.

@Pr3d4dor
Copy link
Contributor Author

Pr3d4dor commented May 5, 2020

@jasonmccreary I was waiting you comment, to point some things out.

The root of the problem is this piece of code:

private function generateResourceTokens(Controller $controller, array $methods)
    {
        return collect($this->resourceTokens())
            ->filter(function ($statements, $method) use ($methods) {
                return in_array($method, $methods);
            })
            ->mapWithKeys(function ($statements, $method) use ($controller) {
                return [
                    str_replace('api.', '', $method) => collect($statements)->map(function ($statement) use ($controller) {
                        $model = Str::singular($controller->prefix());

                        return str_replace(
                            ['[singular]', '[plural]'],
                            [Str::lower($model), Str::lower(Str::plural($model))],
                            $statement
                        );
                    }),
                ];
            })
            ->toArray();
    }

Because its using lower, the string generated is for example:

....
'index' => [
    'query' => 'all:certificatetypes',
    'render' => 'certificatetype.index with certificatetypes',
],
....

In the ResourceGenerator on the method buildData it uses the method modelForContext to find the correspondent model. The model is not found because it is generated using CamelCase format.

Changing it to camelCase fixes the problem.

After that, I noted the same problem in other places using Str::lower and searched all usages of Str::lower and replaced then with Str::camel.

This line needs to be in kebab case, because the generated routes are in kebab case:

$call = sprintf('$response = $this->%s(route(\'%s.%s\'', $this->httpMethodForAction($name), Str::kebab($context), $name);
Route::resource('certificate', 'CertificateController')->except('create', 'edit');

Route::resource('certificate-type', 'CertificateTypeController')->except('create', 'edit');

@jasonmccreary
Copy link
Collaborator

Thanks for the fuller explanation. In the future, you should put your findings in the PR description, not wait for a comment.

I'll review this once more and probably merge, then add some tweaks as I worry this is an issue in some other places.

@jasonmccreary jasonmccreary merged commit 79644cb into laravel-shift:master May 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Problems generating controllers/resources in PascalCase
3 participants