2

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?

Tulsi Acharya
  • 31
  • 1
  • 8
  • What is `$this->get('/');`, where's the `get` function? – duncan Jul 06 '21 at 08:26
  • its in the routes/web.php as Route::get('/', 'App\Http\Controllers\TopController@index'); – Tulsi Acharya Jul 06 '21 at 08:46
  • Likely not a harsh error, but just a warning. I'm pretty sure it would go away if you're not running the test-suite under Windows. Have you tried that? Otherwise perhaps not worth to worry about. – hakre Jul 06 '21 at 15:38
  • And for the fatal error, check `$this->get('/');` . In your current configuration it does not work. Perhaps an error setting up your testsuite, likely not Phpunit, but the extensions for Laravell (perhaps missing?). – hakre Jul 06 '21 at 15:44

2 Answers2

1

You seem to be mixing syntax between whatever Laravel's TestCase is doing with PHPUnit's TestCase. If you're just using PHPUnit you probably need to do is something more like

public function testIndex()
{
    $mockService = $this->createMock(ToppageService::class);
    $controller = new TopController($mockService);
    
    $mockData = 'MOCK DATA';
    
    $mockService
        ->expects($this->once()
        ->method('getAllServicer')
        ->willReturn($mockData);
    
    $response = $controller->index();
    
    $this->assertContains('data', $response->content());
}

This is purely a Unit Test, not a Functional Test, so it's just mocking whatever the service is doing. It's probably also not exactly what you need; I've no idea what view or compact are doing, and you probably need to mock up something more accurate in your data.

duncan
  • 31,401
  • 13
  • 78
  • 99
  • It gives an error : Method Illuminate\View\View::content does not exist. Don't we need to create factory for this? view is returning to index blade with $data consisting of shops, genres and areas list. – Tulsi Acharya Jul 06 '21 at 09:00
  • 1
    @TulsiAcharya: This is example code. When you just copy and paste it, you may see errors because it is not intended for that. You read it, try to understand what it does and why (hence the example) and if there is something therein, you can adapt your own code accordingly or start a test to dig deeper into that direction. – hakre Jul 06 '21 at 15:36
1

uncomment this line:

// use Tests\TestCase;

and comment this one:

use PHPUnit\Framework\TestCase;