Skip to content

Latest commit

 

History

History
165 lines (114 loc) · 3.69 KB

getting_started_01_create_table.md

File metadata and controls

165 lines (114 loc) · 3.69 KB

Getting Started - Creating a Table

After create the project you can start to create your own tables.

Create the table

You need to create a new file in the migrations folder. The file name must be in the format 0000X.sql where X is a number. The number is used to order the execution of the scripts.

Create a file db/migrations/up/00002.sql with the following content:

create table example_crud
(
    id int auto_increment not null primary key,
    name varchar(50) not null,
    birthdate datetime null,
    code int null
);

To have consistency, we need to create the down script. The down script is used to rollback the changes. Create a file db/migrations/down/00001.sql with the following content:

drop table example_crud;

Run the migration

APP_ENV=dev composer run migrate -- update

The result should be:

> Builder\Scripts::migrate
> Command: update
Doing migrate, 2

If you want to rollback the changes:

APP_ENV=dev composer run migrate -- update --up-to=1

The result should be:

> Builder\Scripts::migrate
> Command: update
Doing migrate, 1

Generate the CRUD

APP_ENV=dev composer run migrate -- update                              # Make sure DB is update
APP_ENV=dev composer run codegen -- --table example_crud --save all     # (can be rest, model, test, repo, config)

This will create the following files:

  • ./src/Rest/ExampleCrudRest.php
  • ./src/Model/ExampleCrud.php
  • ./src/Repository/ExampleCrudRepository.php
  • ./tests/Functional/Rest/ExampleCrudTest.php

To finalize the setup we need to generate the config. Run the command bellow copy it contents and save it into the file config/config-dev.php

APP_ENV=dev composer run codegen -- --table example_crud config

First test

The CodeGen is able to create the Unit Test for you.

It is available in the file tests/Functional/Rest/ExampleCrudTest.php.

And you can run by invoking the command:

composer run test

This first test will fail because we don't have the endpoint yet.

ERRORS!
Tests: 36, Assertions: 104, Errors: 2, Failures: 6.
Script ./vendor/bin/phpunit handling the test event returned with error code 2

Let's create them now.

Generate the endpoints from the OpenAPI Documentation

The OpenAPI documentation is generated automatically based on the code. It is an important step because the documentation is used to create the endpoints and map them to the code.

If we don't generate the OpenAPI documentation, the new endpoints will not be available.

composer run openapi

Fixing the unit test

Now, the endpoint errors passed, but the unit test still failing.

composer run test
PDOException: SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: 'birthdate' for column 'birthdate' at row 1

ERRORS!
Tests: 36, Assertions: 111, Errors: 1.
Script ./vendor/bin/phpunit handling the test event returned with error code 2

That's because the data used to test is not correct.

Let's open the file tests/Functional/Rest/ExampleCrudTest.php and change the data to:

    protected function getSampleData($array = false)
    {
        $sample = [

            'name' => 'name',
            'birthdate' => 'birthdate',
            'code' => 1,
        ];
...

Let's change the line:

            'birthdate' => 'birthdate',

to

            'birthdate' => '2023-01-01 00:00:00',

and run the unit test again:

composer run test

And voila! The test passed!

Continue the Tutorial

You can continue this tutorial by following the next step: Add a new field