← Back to journalTechnical

Cài đặt phpspec vào laravel

How to setting phpspec into laravel

Cài đặt phpspec vào laravel

Nếu linh hồn bị bỏ lại ở chốn tối tăm, tội ác sẽ được thực thi. Kẻ có tội không phải là kẻ phạm tội mà là kẻ gây ra cảnh tối tăm.

Victor Hugo

1. Giới thiệu.

Phpspec là một thư viện hỗ trợ test theo design pattern emergent cho PHP: http://www.phpspec.net/en/stable/

2. Mục tiêu.

Giả sử cấu trúc thư mục của project như sau:

/myproject
  /app
    /Http
      /Controllers
         TestController.php
    /Models
       Test.php
    /Services
       TestService.php
  /public
  /resources
  /spec
    /Http
      /Controllers
         TestControllerSpec.php
    /Models
       TestSpec.php
    /Services
       TestServiceSpec.php

Mục tiêu cần đạt được là:

  • Tự động tạo được các file test và các class liên quan bằng command line:
# In terminal

vendor/bin/phpspec desc Models/Test
# => Tạo file model test tại spec/Models/TestSpec.php
# => model tương ứng tại app/Models/Test.php


vendor/bin/phpspec desc Services/TestService
# => Tạo file service test tại spec/Services/TestServiceSpec.php
# => service tương ứng tại app/Services/TestService.php


vendor/bin/phpspec desc Controllers/TestController
# => Tạo file controller test tại spec/Http/Controllers/TestControllerSpec.php
# => controller tương ứng tại app/Http/Controllers/TestController.php

..............


vendor/bin/phpspec run
# => Prompt sẽ hỏi bạn muốn tự động tạo class tương ứng không khi chưa có class đó.

File được sinh ra sẽ có nội dung như sau:

// myproject/spec/Models/TestSpec.php

3. Cài đặt.

3.1. Cài đặt phpspec.

  • Sửa file composer.json như sau:
# myproject/composer.json

    "require": {
       ...
    },
    "require-dev": {
        ...
        "phpspec/phpspec": "^4.0"
    },
    "autoload-dev": {
        "psr-4": {
            "App\\": "app/",
            "Models\\": "app/Models/",
            "Controllers\\": "app/Http/Controllers/",
            "Services\\": "app/Services/"
        }
    },
    "config": {
        ...
        "bin-dir": "vendor/bin"
    }
  • Run composer:
composer install
  • Tạo file phpspec.ymlvới nội dung như sau:
# myproject/phpspec.yml

suites:
  model_suite:
    namespace: Models
    src_path: App

  controller_suite:
    namespace: Controllers
    src_path: App/Http
    spec_prefix: spec\Http

  service_suite:
    namespace: Services
    src_path: App

3.2. Cài đặt phpspec-watcher để tự động test khi code thay đổi.

  • Thêm vào file composer.json như sau:
# myproject/composer.json

    "require": {
       ...
    },
    "require-dev": {
        ...
        "phpspec/phpspec": "^4.0",
        "fetzi/phpspec-watcher": "^1.0"
    },
    ...
  • Run composer
composer install
  • Khởi tạo phpspec-watcher
# In terminal

vendor/bin/phpspec-watcher init
  • Sửa file .phpspec-watcher.yml theo config của bạn
# myproject/.phpspec-watcher.yml

fileMask: '*.php'
checkInterval: 1
directories:
    - app
    - spec
phpspec:
    binary: vendor/bin/phpspec
    arguments: [format=dot]
notifications:
    onError: true
    onSuccess: true
  • Chạy lệnh test:
# In terminal

vendor/bin/phpspec-watcher watch

Sau khi chạy câu lệnh trên xong, khi sửa code thì test sẽ tự động được thực thi.