Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call to undefined method TestCase::get() #1192

Open
SuheylZ opened this issue Jul 14, 2024 · 8 comments
Open

Call to undefined method TestCase::get() #1192

SuheylZ opened this issue Jul 14, 2024 · 8 comments

Comments

@SuheylZ
Copy link

SuheylZ commented Jul 14, 2024

While running the tests I'm getting this error

ReflectionException: Call to undefined method PHPUnit\Framework\TestCase::get()
at vendor/pestphp/pest-plugin-laravel/src/Http.php:189
at tests/Feature/HealthTest.php:8

I tried many things but nothing works. My Unit tests run correctly with no issue. there is no get, post delete request. but my feature tests don't run at all. I tried searching for solution for days and now I'm posting my issue here. Can anyone help me identify the cause and how can I solve it?

Directory Structure

|---tests
|--- Unit
|----Features
|---------HealthTest.php
|----Pest.php
|----TestCase.php
|-phpunit.xml

Pest.php

<?php declare(strict_types=1);
/*
|--------------------------------------------------------------------------
| Test Case
| The closure you provide to your test functions is always bound to a specific PHPUnit test
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
| need to change it using the "uses()" function to bind a different classes or traits.
|
*/

use PHPUnit\Framework\TestCase;

uses(TestCase::class)->in('Feature');
uses(TestCase::class)->in('Unit');


/*
|--------------------------------------------------------------------------
| Expectations
|--------------------------------------------------------------------------
|
| When you're writing tests, you often need to check that values meet certain conditions. The
| "expect()" function gives you access to a set of "expectations" methods that you can use
| to assert different things. Of course, you may extend the Expectation API at any time.
|
*/

expect()->extend('toBeOne', function () {
    return $this->toBe(1);
});

/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
|
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
| project that you don't want to repeat in every file. Here you can also expose helpers as
| global functions to help you to reduce the number of lines of code in your test files.
|
*/

function something()
{
    // ..
}

HealthTest.php

<?php declare(strict_types=1);

//uses(Tests\TestCase::class);

use function Pest\Laravel\{get};

test('service healthy', function () {
    $response = get('/');
    $response->assertStatus(503);
});


test('pings', function (){
    $res = get('/ping', ['Device-Id'=> '3984732984792420934231']);
    $res->assertStatus(200);
});

phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         color="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         stopOnFailures="false"
         syntaxCheck="true">
    <source>
        <include>
            <directory suffix=".php">.</directory>
        </include>
    </source>
    <testsuites>
        <testsuite name="Middlewares">
            <directory phpVersion="8.3">./tests/Unit</directory>
        </testsuite>
        <testsuite name="Features">
            <directory phpVersion="8.3">./tests/Feature</directory>
        </testsuite>
    </testsuites>
</phpunit>
@gregmsanderson
Copy link

Yep, I've just run into this issue too.

I switched back to phpunit. The demo tests (unit and feature) are working correctly using that.

@nunomaduro
Copy link
Member

You guys still facing this issue? typically just means you forgot to use the "uses" function.

@gregmsanderson
Copy link

@nunomaduro Sadly I gave up trying, but at the time, yes it was still not working for me. I did add that "uses" function.

As mentioned above, unit tests worked but feature tests would not. Strange.

@jslirola
Copy link

I am facing this issue and I have no idea how to tackle it.

Environment:

  • PHP v8.3.10
  • Symfony v7.1.3
  • Pest v2.35.1
   FAILED  Tests\Feature\RegisterTest > it gets something                                                                                                                               Error   
  Call to undefined method Tests\Feature\RegisterTest::get()

  at tests/Feature/RegisterTest.php:4
      1▕ <?php
      2▕ 
      3▕ it ('gets something', function () {
  ➜   4▕     $this->get('/')->assertOk();

  1   tests/Feature/RegisterTest.php:4

  Tests:    1 failed, 1 passed (1 assertions)
  Duration: 9.14s

I tried the solutions suggested here but no luck either: #415

And finally, I downgraded in case the problem was due to a change in current versions. I installed Pest v2.34.0 and v2v.33.0 but the problem is still with us. 👎

@gregmsanderson
Copy link

Hi,

Ok I got a chance to try it again. I installed all the latest versions of everything today. Sure enough, I still got that same error Call to undefined method Tests\Feature\ExampleTest::get(). Sigh.

I started experimenting and found that is did work if I swapped out the BaseTestCase (in TestCase.php):

<?php

namespace Tests;

//use PHPUnit\Framework\TestCase as BaseTestCase;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    //
}

... and now it works as documented on https://laravel.com/docs/11.x/http-tests ....

   PASS  Tests\Feature\ExampleTest                                                                                               0.27s  
  ✓ the application returns a successful response  

The question is: what are the implications of using that as the BaseTestCase instead?

I initially wondered if it was because phpunit was no longer a dev dependency (based on the https://pestphp.com/docs/installation which says to remove it). However it is still in /vendor, so the files do exist.

@owenvoke
Copy link
Member

owenvoke commented Sep 1, 2024

For a Laravel application, that is the testcase class you are meant to use (for tests that require the framework). So everything is working as expected.

If you extend the PHPUnit testcase, it won't have any Laravel functionality, so the get() method won't exist.

I'm unsure why you ended up with a tests/TestCase.php class that was extending the PHPUnit one in a Laravel application. Was that after running vendor/bin/pest --init?

@gregmsanderson
Copy link

@owenvoke Ah, ok, I wondered if it was something like that.

Yep, that's the TestCase.php that command generates. I just renamed the tests/ folder to get rid of it, then tried running it again:

$ vendor/bin/pest --init

   INFO  Preparing tests directory.

  phpunit.xml ............................................................................... File already exists.  
  tests/Pest.php ................................................................................... File created.  
  tests/TestCase.php ............................................................................... File created.  
  tests/Unit/ExampleTest.php ....................................................................... File created.  
  tests/Feature/ExampleTest.php .................................................................... File created. 

The created tests/TestCase.php is:

<?php

namespace Tests;

use PHPUnit\Framework\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    //
}

So that appears to explain what is happening and why feature tests don't work.

@gregmsanderson
Copy link

I don't know how it works internally but I'd assume it is not correctly detecting Laravel during init and so it's defaulting to the phpunit TestCase.php. Which then causes the undefined error.

https://github.com/pestphp/pest/blob/2.x/stubs/init-laravel/TestCase.php.stub
https://github.com/pestphp/pest/blob/2.x/stubs/init/TestCase.php.stub

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants