Home > Backend Development > PHP Tutorial > [PHP][Laravel Test]单元化测试:深入测试篇

[PHP][Laravel Test]单元化测试:深入测试篇

WBOY
Release: 2016-06-23 13:01:42
Original
940 people have browsed it

本文适用于对 PHP 和 laravel 框架有一定了解并已经看完laravel入门视频:Laravel 5 Fundamentals 的初学者。本文内容主要讲解如何使用单元化测试工具测试应用。

相关视频 laravel test 已经下载到网盘如果还没看请先下载观看:

  • 链接: http://pan.baidu.com/s/1sjXeLQH
  • 提取密码:jjb5

1.在 tests 目录下,新建测试文件 OrderTest.php 。

<?php use App\Product;use App\Order; class ProductTest extends PHPUnit_Framework_TestCase {     /** @test */    function an_order_consists_of_products()    {        $order = new Order;         $product = new Product('Fallout4',59);        $product2 = new Product('Pillowcase',7);         $order->add($product);        $order->add($product2);         $this->assertEquals(2,count($order->products()));    } } 
Copy after login

在 App 目录下创建 Order.php 文件

<?php namespace App; class Order{    protected $products = [];     public function add(Product $product)    {        $this->products[] = $product;    }     public function products()    {        return $this->products;    }} 
Copy after login

优化最后一行代码使用 assertCount() 方法。

<?php use App\Product;use App\Order; class ProductTest extends PHPUnit_Framework_TestCase {    /** @test */    function an_order_consists_of_products()    {        $order = new Order;         $product = new Product('Fallout4',59);        $product2 = new Product('Pillowcase',7);         $order->add($product);        $order->add($product2);         $this->assertCount(2,$order->products());    } } 
Copy after login

输入命令: phpunit tests/unit 测试

2.测试订单总额。

输入命令: phpunit --filter an_order_can_determine_the_total_cost_of_all_its_products

只测试一个方法

<?php use App\Product;use App\Order; class ProductTest extends PHPUnit_Framework_TestCase {    /** @test */    function an_order_consists_of_products()    {        $order = new Order;         $product = new Product('Fallout4',59);        $product2 = new Product('Pillowcase',7);         $order->add($product);        $order->add($product2);         $this->assertCount(2,$order->products());    }     /** @test */    function an_order_can_determine_the_total_cost_of_all_its_products()    {        $order = new Order;         $product = new Product('Fallout4',59);        $product2 = new Product('Pillowcase',7);         $order->add($product);        $order->add($product2);         $this->assertEquals(66,$order->total());    }} 
Copy after login

测试文件 Order.php

<?php namespace App; class Order{    protected $products = [];     public function add(Product $product)    {        $this->products[] = $product;    }     public function products()    {        return $this->products;    }     public function total()    {        total = 0;         foreach($this->productsas $product){            $total += $product->cost();        }         return $total;    }} 
Copy after login

通过测试

使用 createOrderWithProducts() 方法简化代码。

<?php use App\Product;use App\Order; class ProductTest extends PHPUnit_Framework_TestCase {    /** @test */    function an_order_consists_of_products()    {         $order = $this->createOrderWithProducts();         $this->assertCount(2,$order->products());    }     /** @test */    function an_order_can_determine_the_total_cost_of_all_its_products()    {        $order = $this->createOrderWithProducts();         $this->assertEquals(66,$order->total());    }     protected function createOrderWithProducts()    {         $order = new Order;         $product = new Product('Fallout4',59);        $product2 = new Product('Pillowcase',7);         $order->add($product);        $order->add($product2);         return $order;    }} 
Copy after login

总结

本篇稍微深入的测试了一下 Order 类,后续继续作详尽阐述。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template