yii2-app-api icon indicating copy to clipboard operation
yii2-app-api copied to clipboard

Improve FakerController to work with relations

Open cebe opened this issue 1 year ago • 1 comments

Faker Controller should be able to detect relations and set the foreign key values accordingly.

Inside this for loop:

https://github.com/cebe/yii2-app-api/blob/f3facab5d0978a30f77535913a442547a4ab42e7/console/commands/FakerController.php#L26-L32

Check for model relations (yii2-openapi could add a list of known relations to the Faker class for this) and add relations to existing models selecting the existing foreign keys.

e.g.

$model->user_id = $faker->randomElement(User::find()->select('id')->column());

The perfect implementation would event sort models by relation dependencies before generating fake data.

cebe avatar Oct 18 '22 15:10 cebe

You may use the following schema for testing:

#
# Database Schema file
#
# if you change this file, run
#
#   make cli
#   ./yii gii/api
#
# to generate migrations and update base model classes.
#
openapi: 3.0.3
info:
  title: 'Proxy-Service'
  description: ""
  version: 1.0.0
  contact:
    name: 'Carsten Brandt'
    email: [email protected]
servers:
  - url: 'http://localhost:8937'
    description: 'Local Dev API'
security:
  - BasicAuth: []
components:
  securitySchemes:
    BasicAuth:
      type: http
      scheme: basic
  schemas:
    Account:
      description: user account
      type: object
      required:
        - id
        - name
      properties:
        id:
          type: integer
        name:
          description: account name
          type: string
          maxLength: 40
          x-faker: 'substr($faker->userName(), 0, 40)'

    Domain:
      description: domain
      type: object
      required:
        - id
        - name
        - account
        - created_at
      properties:
        id:
          type: integer
        name:
          description: domain or sub-domain name, in DNS syntax, IDN are converted
          type: string
          maxLength: 128
          x-faker: '$faker->domainName'
        account:
          $ref: '#/components/schemas/Account'

        routings:
          type: array
          items:
            $ref: '#/components/schemas/Routing'

        created_at:
          readOnly: true
          type: string
          format: datetime
          x-db-type: datetime
          nullable: false

    Routing:
      description: rounting specification
      type: object
      required:
        - id
        - domain
      properties:
        id:
          type: integer
        domain:
          $ref: '#/components/schemas/Domain'
        path:
          type: string
          maxLength: 255
          x-faker: '$faker->randomElement(["/", "/", "/", "/", "/api", "/tools", "/assets/web"])'

        ssl:
          type: boolean
        redirect_to_ssl:
          type: boolean

        service:
          type: string
          maxLength: 255
          x-faker: '"http://tador.cebe.net/" . $faker->domainName'

        created_at:
          readOnly: true
          type: string
          format: datetime
          x-db-type: datetime
          nullable: true # todo check here: "nullable: false"


paths:
  /:
    get:
      responses: []
      description: none

cebe avatar Oct 18 '22 16:10 cebe

The perfect implementation would event sort models by relation dependencies before generating fake data.

Can you please give more details about this?

SOHELAHMED7 avatar Nov 01 '22 13:11 SOHELAHMED7