I am writing a code to make Unit test for each pages in my project and started with the Main page. The main page consists of shop lists, genre lists and area lists.
tests\Unit\ToppageTest.php :
<?php
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
// use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
use App\Models\Shop;
class TopPageTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testIndex()
{
$response = $this->get('/');
$this->assertContains('data', $response->content());
$response->assertSuccessful();
}
}
phpunit.xml:
<php>
<server name="APP_ENV" value="testing"/>
<server name="BCRYPT_ROUNDS" value="4"/>
<server name="CACHE_DRIVER" value="array"/>
<server name="MAIL_DRIVER" value="array"/>
<server name="DB_CONNECTION" value="sqlite"/>
<server name="DB_DATABASE" value=":memory:"/>
<server name="MAIL_MAILER" value="array"/>
<server name="QUEUE_CONNECTION" value="sync"/>
<server name="SESSION_DRIVER" value="array"/>
<server name="TELESCOPE_ENABLED" value="false"/>
</php>
App\Http\Controllers\TopController.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Services\ToppageService;
class TopController extends Controller
{
private $shop;
public function __construct(ToppageService $shop)
{
$this->shop = $shop;
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$data = $this->shop->getAllServicer();
return view('index', compact('data'));
}
}
App\Http\Services\ToppageService.php:
<?php
namespace App\Services;
use App\Services\BaseService;
use App\Models\Shop;
use App\Models\Genre;
use App\Models\Area;
class ToppageService extends BaseService
{
protected $shop, $genre, $area;
public function __construct(Shop $shop, Genre $genre, Area $area)
{
$this->shop = $shop;
$this->genre = $genre;
$this->area = $area;
}
public function getAllServicer()
{
$data = [] ;
$data['shoplist'] = $this->shop->with('attribute','content','coupon','menu_categoly','area','genre')->whereHas('servicer',function($query){$query->where('name', config('const.system.setting.servicer'));})->orderBy('created_at', 'DESC')->get();
$data['genrelist'] = $this->genre->get();
$data['arealist'] = $this->area->get();
return $data;
}
}
When I enter php artisan test or vendor/bin/phpunit in command prompt, I get an error like:
Call to undefined method Tests\Unit\TopPageTest::get()
at E:\tulsi\wparijat\laravel\laravel-awapass\app.awapass.com\tests\Unit\ToppageTest.php:20
16▕ */
17▕ public function testIndex()
18▕ {
19▕ //not working
➜ 20▕ $response = $this->get('/');
21▕ $this->assertContains('data', $response->content());
22▕ $response->assertSuccessful();
23▕ }
24▕ }
1 E:\tulsi\wparijat\laravel\laravel-awapass\app.awapass.com\vendor\phpunit\phpunit\phpunit:76
PHPUnit\TextUI\Command::main()
Tests: 1 failed
Time: 0.05s
when I add use Tests\TestCase; instead of use PHPUnit\Framework\TestCase;
I get error:
Warning: TTY mode is not supported on Windows platform.
I searched for solution for could't get the exact solution for this. Can anyone please help me?