PHPUnit_Framework_TestCase Found but undefined method ::call() - php

Hi i am working with Laravel 4.2.17 , PHPUnit 5.1.1 and PHP 5.6.16 .
I am using PHPStorm as My Editor .
When i go to app/tests/ExampleTest.php and run it i am getting the error .
PHP Fatal error: Class 'TestCase' not found in /..../app/tests/ExampleTest.php on line 3
Then i changed TestCase to PHPUnit_Framework_TestCase Now the ExampleTest.php looks like this
class ExampleTest extends PHPUnit_Framework_TestCase {
/**
* A basic functional test example.
*
* #return void
*/
public function testBasicExample()
{
$crawler = $this->client->request('GET', '/');
$this->assertTrue($this->client->getResponse()->isOk());
}
}
Now the PHPUnit_Framework_TestCase calls is identified .
But when i try to do
public function testBasicExample()
{
$this->call("POST","test");
}
It is saying that
Fatal error: Call to undefined method ExampleTest::call()
I checked the larval documents and in there
Calling A Route From A Test
You may easily call one of your routes for a test using the call method:
$response = $this->call('GET', 'user/profile');
$response = $this->call($method, $uri, $parameters, $files, $server, $content);
SO i am not sure what i am doing wrong .
I also tried by updating the composer , but no luck . :( :(

The reason you cannot access call is because you are no longer extending TestCase which provides it. I suggest you revert back to extends TestCase and run your tests again. You should make sure to run the test from the root directory by using phpunit app/tests.
Running composer dump-autoload also first, just in case it's an autoloading issue.

Related

I am trying TDD(Test Driven Development) using Laravel8 inbuilt PHPUnit

I am receiving this error:
Call to undefined method Tests\Feature\ExampleTest::visit()
while running my test cases. I am a bit new to TDD.
Here is my ExampleTest Code
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* #return void
*/
public function test_example()
{
$response = $this->visit('/')->see('Laravel');
$response->assertStatus(200);
}
}
From the video tutorial that I am using to learn about TDD the code above runs well without any issue but when it comes to running the code on my side I am faced with error as shown below :
• Tests\Feature\ExampleTest > example
Error
Call to undefined method Tests\Feature\ExampleTest::visit()
I am currently running Laravel 8.6 and PHPUnit 9.510
Any ideas on how I can resolve this are highly welcomed.
It looks like you are mixing up Laravel's built in browser testing methods (Dusk) - like visit() that you are using above - with the unit testing and feature testing methods.
As you are in the Tests\Feature namespace you need to follow the guide for unit and feature testing, the equivalent of which are:
public function test_example()
{
$response = $this->get('/');
$response->assertStatus(200);
}

PHP Error: Call to a member function connection() on null in /vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 1344 [duplicate]

Try write a unit test and i need do sql query
class UpdateThrowsTest extends TestCase
{
protected $bgame;
protected $game_id = 95;
public function setUp(){
$game = new Game();
$game = $game::find($this->game_id);
}
}
and then i write "phpunit" in console and try exception
Call to a member function connection() on null.
If anyone bounce to this error during test with Laravel 6 project.
Try to check if the extends TestCase is using the right TestCase.
It could be due to Laravel 6 make:test generated test using the wrong TestCase.
Change
use PHPUnit\Framework\TestCase;
To
use Tests\TestCase;
The problem should solve.
I had this error on laravel 7 (nothing worked even php artisan serve) and fixed it with
composer dumpautoload
Before that update my vendor with composer update and then everything worked fine.

Laravel 5.4 - Basic Tests - NotFoundHttpException

I have a problem running the basic test that ships with Laravel:
app/tests/Feature/ExampleTest.php
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* #return void
*/
public function testBasicTest()
{
$response = $this->get('/');
$response->assertStatus(200);
$this->assertTrue(true);
}
}
When I run it I get the exception NotFoundHttpException. I can access my website in the browser without problems. This problem appears to apply to all my routes.
Using Laravel 5.4
The route / is defined in app/routes/web.php.
It seems that in Laravel 5.4 $baseUrl property was removed from TestCase class.
You may add some setUp to your ExampleTest:
function setUp()
{
parent::setUp();
config(['app.url' => 'https://myserver']);
}
Hope this helps!

Laravel Tests in custom directory - Getting error "the name is already in use"

I'm trying to build a application that uses laravel. There is a test directory and I use it for application testing.
But I have an service in app/MyApp/Service directory. Here is the directory structure.
app/MyApp/Service
|-->Services
|-->Interfaces
|-->Test
|------->OLSServiceTest.php
Here is OLSServiceTest.php content
<?php
namespace App\MyApp\Service\Test;
class OLSServiceTest extends TestCase
{
public function testInfo()
{
$this->assertTrue(true);
}
public function testLogin()
{
$this->assertTrue(false);
}
/**
* Creates the application.
*
* Needs to be implemented by subclasses.
*
* #return \Symfony\Component\HttpKernel\HttpKernelInterface
*/
public function createApplication()
{
// TODO: Implement createApplication() method.
}
}
Then I'm trying to run the test manually
$ vendor/phpunit/phpunit/phpunit App\\MyApp\\Service\\Test\\OLSServiceTest
But getting this error :
[Symfony\Component\Debug\Exception\FatalErrorException]
Cannot declare class App\MyApp\Service\Test\OLSServiceTest, because
the name is already in use
Fatal error: Cannot declare class
App\MyApp\Service\Test\OLSServiceTest, because the name is already in
use in /var/www/app/MyApp/Service/Test/OLSServiceTest.php on line 29
What I am doing wrong?
Seems like you are trying to run test with wrong command. Try to use PHPUnit from Vendor bin directory I think it will solve your issue.
vendor/bin/phpunit app/MyApp/Service/Test/OLSServiceTest.php

Simple PHP Selenium test doesn't work

I have the following Selenium test in PHP:
<?php
require_once('PHPUnit/Extensions/SeleniumTestCase.php');
class Example extends PHPUnit_Extensions_SeleniumTestCase
{
protected function setUp()
{
$this->setBrowser("*chrome");
$this->setBrowserUrl("http://change-this-to-the-site-you-are-testing/");
}
public function testMyTestCase()
{
$this->open("/frontend_dev.php");
try {
$this->assertTrue($this->isTextPresent("Local Coupons"));
} catch (PHPUnit_Framework_AssertionFailedError $e) {
array_push($this->verificationErrors, $e->toString());
}
}
}
When I try to run it (by running "php filename.php") I get the error:
PHP Fatal error: Class 'PHPUnit_Framework_TestCase' not found in /usr/share/php/PHPUnit/Extensions/SeleniumTestCase.php on line 60
Which makes sense because the class isn't defined anywhere, but why not? I installed PHPUnit. It seems a little weird that a class called PHPUnit_Framework_TestCase wouldn't be included with PHPUnit. Any ideas?
You should read the docs for PHPUnit for the command line test runner. You can also just run 'phpunit --help' from the command line. I believe you'll find that you need to instead run something like
phpunit path/to/filename.php

Categories