<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Laravel測試 * Laravel 5.4.24 ## 測試前的準備 ### 安裝Laravel ``` composer create-project laravel/laravel testing-laravel ``` ### 安裝依賴 ``` composer require mockery/mockery --dev ``` ### 修改配置 修改 `phpunit.xml` 定義測試數據庫 ``` <env name="DB_CONNECTION" value="sqlite"/> <env name="DB_DATABASE" value=":memory:"/> ``` > 使用`sqlite`的連接方式,使用`memory`進行數據存儲。 ## 針對Model進行測試 ### 測試前的準備 * 建立數據庫模型和數據庫遷移文件 ``` php artisan make:model Article -m ``` * 編輯遷移文件 ``` public function up() { Schema::create('articles', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->text('body'); $table->timestamps(); }); } ``` * 修改ModelFactory 文件在`databases\factories\ModelFactory.php` ``` $factory->define(App\Article::class, function (Faker\Generator $faker) { return [ 'name' => $faker->name, 'body' => $faker->sentence, ]; }); ``` * 修改模型文件 ``` namespace App; use Illuminate\Database\Eloquent\Model; class Article extends Model { // 定義使用 __construct($data) 或 create($data) 時,可以被修改的字段,從而保證其他字段不被修改 protected $fillable = ['title', 'body']; } ``` ### 新增并修改測試文件 * 測試有沒有文章 ``` use Illuminate\Foundation\Testing\DatabaseMigrations; class ArticleTest extends TestCase { use DatabaseMigrations; /** @test */ public function the_articles_is_empty_result() { // 取得所有文章 $articles = Article::all(); // 確認 $articles 是 Collection $this->assertInstanceOf(Collection::class, $articles); // 而文章數量為 0 $this->assertCount(0, $articles); } } ``` * 測試新增數據并列出數據 ``` use App\Article; class ArticleTest extends TestCase { // ... /** @test */ public function the_articles_create_and_list_the_data() { // 新增 10 條記錄 for ($i = 1; $i <= 10; $i++) { factory(Article::class,10)->make([ 'name' => 'name ' . $i, 'body' => 'body ' . $i ]); } // 確認數據庫中有10條記錄 $articles = Article::all(); $this->assertCount(10, $articles); } } ``` > 在實際使用Laravel項目中,我們常常不會直接通過Controller向模型獲取數據,而是會使用到Repository的開發模式,那么如何測試Repository呢? ## 使用Repository模式 ### 新建Repository文件 `app/Repositories/ArticleRepository.php` ``` <?php namespace App\Repositories; /** * Class ArticleRepository * @package App\Repositories * @method latest($limit) * @method create($attributes = array()) */ class ArticleRepository { } ``` > 先建立一個空的Repository文件,后期在測試的過程中添加方法。 ### 建立Repository測試文件 #### 測試Repository查詢 * 建立文件并編寫測試代碼,文件在`tests/Feature/ArticleRepositoryTest.php` ``` <?php namespace Tests\Feature; use App\Article; use App\Repositories\ArticleRepository; use Tests\TestCase; use Illuminate\Foundation\Testing\DatabaseMigrations; class ArticleRepositoryTest extends TestCase { use DatabaseMigrations; /** * @var ArticleRepository */ protected $repository; protected function setUp() { parent::setUp(); // 填充數據 $this->repository = new ArticleRepository(); } // 測試獲取最后的10條資料 /** @test */ public function fetch_latest_10_articles() { // given data. for ($i = 1; $i <= 100; $i++) { factory(Article::class)->create([ 'name' => 'name ' . $i, 'body' => 'body ' . $i, ]); } // 從 repository 中取得最新 10 條數據 $articles = $this->repository->latest(10)->get(); $this->assertCount(10, $articles); // 確認標題是從 100 .. 91 倒數 // "title 100" .. "title 91" $i = 10; /** @var \App\Article[] $articles */ foreach ($articles as $article) { $this->assertEquals('name ' . $i, $article->name); --$i; } } } ``` * 拋出錯誤并修復 ``` Error: Call to undefined method App\Repositories\ArticleRepository::latest() ``` 1. 先向Repository中添加`latest()`方法 ``` public function latest10() { } ``` > 執行phpunit報錯。 > `Error: Call to a member function get() on null` 2. 再向Repository添加`latest()`方法 ``` /** * @param int $limit * @return ArticleRepository|\Illuminate\Database\Eloquent\Builder */ public function latest($limit) { return Article::query()->orderBy('id', 'desc')->limit($limit); } ``` > 此時測試通過。 #### 測試Repository添加操作 1. 編寫測試代碼 ``` /** @test */ public function create_article() { $article = $this->repository->create([ 'name' => 'name', 'body' => 'body', ]); $this->assertEquals(1, $article->id); } ``` 拋出如下錯誤: ``` Error: Call to undefined method App\Repositories\ArticleRepository::create() ``` 2. 修改Repository中代碼,新增`create()`方法: ``` use App\Article; public function create() { } ``` 拋出如下錯誤: ``` ErrorException: Trying to get property of non-object ``` 3. 修改Repository中`create()`方法,添加邏輯代碼: ``` use App\Article; /** * Create new data. * @param array $attributes * @return mixed */ public function create(array $attributes = array()) { return Article::create($attributes); } ``` > 以上測試過程即為TDD測試流程 ## Reference [Web Development with Laravel 5](https://gist.github.com/jaceju/c415c1b42daf4c589f2a)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看