前提・実現したいこと
DIコンテナの概念がわからず適当な言葉でぐぐっていた所、
以下のページにたどり着きました。
Laravel4、依存注入とコンテナ(1)
そういえばテストもわからないなあと思い、
感覚だけでも掴めないかととりあえず写経していたのですが、
いざPHPUnitコマンドを叩いてみたところ、エラーが発生しました。
発生している問題・エラーメッセージ
cmd PHPUnit 7.1.2 by Sebastian Bergmann and contributors. E 1 / 1 (100%) Time: 201 ms, Memory: 10.00MB There was 1 error: 1) Tests\Unit\AppleTest::testGet Error: Class 'App\Models\Water' not found E:\xampp\htdocs\tmp\app\Models\Apple.php:11 E:\xampp\htdocs\tmp\tests\Unit\AppleTest.php:29 ERRORS!
該当のソースコード
php
1<?php 2 3namespace App\Models; 4 5use Illuminate\Database\Eloquent\Model; 6 7class Apple extends Model 8{ 9 public function get() 10 { 11 $water = new Water; 12 $sun = new Sun; 13 14 return $water->get() + $sun->get(); 15 } 16}
php
1<?php 2 3namespace Tests\Unit; 4 5use \Mockery as m; 6use Tests\TestCase; 7use Illuminate\Foundation\Testing\WithFaker; 8use Illuminate\Foundation\Testing\RefreshDatabase; 9 10class AppleTest extends TestCase 11{ 12 /** 13 * A basic test example. 14 * 15 * @return void 16 */ 17 public function testGet() 18 { 19 $waterMock = m::mock( 'overload:Water' ); 20 $waterMock->shouldReceive( 'get' ) 21 ->once() 22 ->withNoArgs() 23 ->andReturn( 50 ); 24 25 $sunMock = m::mock( 'overload:Sun' ); 26 $sunMock->shouldReceive( 'get' ) 27 ->once() 28 ->withNoArgs() 29 ->andReturn( 50 ); 30 31 // 参考にしたサイトではnew Appleで動くようだった 32 $apple = new \App\Models\Apple; 33 34 $this->assertEquals( 100, $apple->get() ); 35 } 36} 37
試したこと
初期状態ではPHPUnitが起動しなかったので
バージョンをいろいろと弄ってしまっています。
json
1"require-dev": { 2 "phpunit/phpunit": "~7.0", 3 },
補足情報(FW/ツールのバージョンなど)
PHP 7.2.3
Laravel 5.6.3(現在移行作業中)
回答1件
あなたの回答
tips
プレビュー