月度归档:2019年03月

composer不安装dev包

通常情况下在pro环境上中安装composer包时,我们并不希望安装require-dev下面的包,以laravel的composer.json为例:

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": ">=5.6.4",
        "laravel/framework": "5.4.*",
        "laravel/tinker": "~1.0"
    },
    "require-dev": {
        "fzaninotto/faker": "~1.4",
        "mockery/mockery": "0.9.*",
        "phpunit/phpunit": "~5.7"
    },
    "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\\": "app/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "scripts": {
        "post-root-package-install": [
            "php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "php artisan key:generate"
        ],
        "post-install-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postInstall",
            "php artisan optimize"
        ],
        "post-update-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postUpdate",
            "php artisan optimize"
        ]
    },
    "config": {
        "preferred-install": "dist",
        "sort-packages": true,
        "optimize-autoloader": true
    }
}

如果是线上环境的话,我们不需要安装phpunit、mockery、faker则可以执行composer install --no-dev

0

phpunit基础写法

<?php
/**
 * Created by PhpStorm.
 * User: Connie
 * Date: 2019/3/30
 * Time: 16:20
 */

namespace Tests\Unit;


use App\Order;
use App\Product;
use Tests\TestCase;

class OrderTest extends TestCase
{
    /** @var Order $order */
    protected $order;
    protected $iWatch;
    protected $macBook;

    public function setUp()
    {
        $this->order = new Order();
        $this->iWatch = new Product();
        $this->iWatch->setName('i watch');
        $this->iWatch->setPrice(5000);
        $this->macBook = new Product();
        $this->macBook->setName('macBook Pro');
        $this->macBook->setPrice(12000);

        $this->order->add($this->iWatch);
        $this->order->add($this->macBook);
    }

    /** @test */
    public function order_can_consists_products()
    {
        $this->assertEquals(2, count($this->order->getProducts()));
    }

    /** @test */
    public function we_can_get_price_from_an_order()
    {
        $this->assertEquals(17000,$this->order->total());
    }

}

0