Warm tip: This article is reproduced from serverfault.com, please click

How do I run the phpunit in a single file for all the model/controller

发布于 2020-12-02 11:24:38

I am having a challenge with the phpunit, the test runs perfectly for the user inputs but doesn't recognise the product function. I have tried to delete the user authentication functions to run the product insertion, etc but it seems not working.

<?php

namespace Tests\Feature\Auth;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use \App\Models\Product;
use \App\Models\User;
class LoginTest extends TestCase
{
    /**
     * A basic feature test example.
     *
     * @return void
     */
    
    
    public function test_user_can_view_a_login_form(){
        $response = $this->get('user/signin');
        $response->assertStatus(200);
        $response->assertViewIs('user.signin');
    }
    public function test_user_cannot_view_a_login_from_when_authenticated() {

       $user = new User([
           'email' => 'jobtest@test.com',
           'password' => bcrypt($password = 'i-want-this-job'),
       ]);
       if ($user === null) {
           $user->save();
       }
       else{
           echo "User exists";
       }
       
       $response = $this->post('user/signin', [
           'email' => $user->email,
           'password' => $password,
       ]);
      
        $response = $this->actingAs($user)->get('user/signin');
        $this->assertAuthenticatedAs($user);
      
    }

**THE PRODUCT METHODS START FROM HERE** 
    public function user_can_access_home_page() {
        $response = $this->get('/');
        $response->assertStatus(200);
        $response->assertViewIs('/');
     }
   public function can_user_insert_product() {
        $product = new Product([
        'imagePath' => 'https://i0.wp.com/ae01.alicdn.com/kf/HLB1V4vwbjLuK1Rjy0Fhq6xpdFXaJ/Elegant-Women-Dresses-Bodycon-Office-Formal-Business-Work-Party-Sheath-Tunic-Pencil-Midi-O-neck-Short.jpg?fit=800%2C800&ssl=1',
        'title' => 'Elegant Women Dresses',
        'description' => 'Some quick example text to build on the card title and make up the bulk of the content',
        'price' => '8',
        'quantity' => '3'
    ]);  
    $product->save();
    if($product) {
        echo "Product inserted";
    }
  

    $response = $this->post('/', [
        'imagePath' => $product->imagePath,
        'title' => $product->title,
        'description' => $product->description,
        'price' => $product->price,
        'quantity' => $product->quantity,
    ]);
    $response = assertRedirect('/');
}


}

I have also tried to create another test file for it, but still showing No tests found in class "Tests\Feature\Auth\LoginTest".enter image description here

Whereas when appended in the same test file, it will run and ignore the product methods. Or do I need to run php artisan make:test Auth/LoginTest for user login separately and run php artisan make:test Product/ProductTest because I don't know why the user will work and the product will not enter image description here

Questioner
Payne
Viewed
0
mrhn 2020-12-02 19:42:52

For phpunit tests to run they either start with the word test or have a @test annotation. Your product tests does not follow these rules.

The usually solution

public function test_can_user_insert_product(){

Or the annotation solution

/** @test **/
public function can_user_insert_product