I have a WebTestCase class:
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Bundle\FrameworkBundle\Client;
class ApiAdminInvoiceControllerTest extends WebTestCase
{
public function test()
{
...
$this->assertArrayHasKey('id', $array);
...
}
}
I have this error in all asserts:Method 'assertArrayHasKey' not found.
Try using PHPUnit\Framework\TestCase; as base class
Related
I've encountered an error when refactoring my Laravel tests by adding at setUp() method.
This works as expected:
<?php
namespace Tests\Feature;
use App\Models\Person;
use Tests\TestCase;
class MembershipTicketsTest extends TestCase
{
public function test_a_non_member_cannot_list_redeemable_events()
{
$user = Person::factory()->create();
$this->actingAs($user);
$this->get(route('event.index'))->assertStatus(302)->assertRedirect(route('membership.create'));
}
Where as this returns the error Unknown formatter "firstName":
<?php
namespace Tests\Feature;
use App\Models\Person;
use Tests\TestCase;
class MembershipTicketsTest extends TestCase
{
protected $user;
protected function setUp() : void
{
$this->user = Person::factory()->create();
}
public function test_a_non_member_cannot_list_redeemable_events()
{
$this->actingAs($this->user);
$this->get(route('event.index'))->assertStatus(302)->assertRedirect(route('membership.create'));
}
Every solution to this error I've found says to make sure that you have use Tests\TestCase; in your file, which I do.
I presume this means I cannot use factories in the setUp() method of a test in Laravel. Is this true, and why is it the case?
I have the following console application:
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Services\Myservice;
class MyCommand extends Command
{
protected $signature = 'test';
public function handle(Myservice $service){
dump($service->dummy());
}
}
And I have the following service:
namespace App\Services;
class Myservice
{
public function dummy()
{
return false;
}
}
I also made the following test:
namespace Tests\Console;
use Illuminate\Foundation\Testing\TestCase;
use Mockery;
class TestMyCommand extends TestCase
{
public function testCommand()
{
$service = Mockery::mock(Myservice::class);
$service->shouldReceive('dummy')->andReturn(true);
app()->bind(MyService::class,$service);
$this->artisan('test')
$service->shouldHaveReceived('dummy')->andReturn(true);
}
}
But I retrieve the following error:
There was 1 error:
1) Tests\Console\TestMyCommand::testCommand
Mockery\Exception\InvalidCountException: Method dummy(<Any Arguments>) from Mockery_0__Tests_Console_MyService should be called
at least 1 times but called 0 times.
/var/www/html/api/vendor/mockery/mockery/library/Mockery/CountValidator/AtLeast.php:47
/var/www/html/api/vendor/mockery/mockery/library/Mockery/Expectation.php:312
/var/www/html/api/vendor/mockery/mockery/library/Mockery/ReceivedMethodCalls.php:46
/var/www/html/api/vendor/mockery/mockery/library/Mockery/VerificationDirector.php:36
/var/www/html/api/tests/Console/MyCommand.php:20
So why I am unable to provide a mocked service to the command?
#Edit 1:
I tried the following:
<?php
namespace Tests\Console;
use Illuminate\Foundation\Testing\TestCase;
use Tests\CreatesApplication;
use Mockery;
class TestMyCommand extends TestCase
{
use CreatesApplication;
public function testCommand()
{
$service = Mockery::mock(Myservice::class);
$service->shouldReceive('dummy')->andReturn(true);
app()->instance(MyService::class,$service);
$this->artisan('test');
$service->shouldHaveReceived('dummy')->andReturn(true);
}
}
And the following:
<?php
namespace Tests\Console;
use Illuminate\Foundation\Testing\TestCase;
use Tests\CreatesApplication;
use Mockery;
class TestMyCommand extends TestCase
{
use CreatesApplication;
public function testCommand()
{
$service = Mockery::mock(Myservice::class);
$service->shouldReceive('dummy')->andReturn(true);
$this->app->instance(MyService::class,$service);
$this->artisan('test');
$service->shouldHaveReceived('dummy')->andReturn(true);
}
}
But still get the error:
Time: 3.32 seconds, Memory: 38.00 MB
There was 1 error:
1) Tests\Console\TestMyCommand::testCommand
Mockery\Exception\InvalidCountException: Method dummy(<Any Arguments>) from Mockery_0__Tests_Console_Myservice should be called
at least 1 times but called 0 times.
/var/www/html/api/vendor/mockery/mockery/library/Mockery/CountValidator/AtLeast.php:47
/var/www/html/api/vendor/mockery/mockery/library/Mockery/Expectation.php:312
/var/www/html/api/vendor/mockery/mockery/library/Mockery/ReceivedMethodCalls.php:46
/var/www/html/api/vendor/mockery/mockery/library/Mockery/VerificationDirector.php:36
/var/www/html/api/tests/Console/TestMyCommand.php:21
ERRORS!
Tests: 1, Assertions: 2, Errors: 1.
Generating code coverage report in HTML format ... done
I am trying to load my fixtures (php file) from phpunit.
I installed LiipTestFixture, and in my AbstractControllerTest.php :
<?php
namespace App\Tests\Controller;
use App\Entity\App\User;
use App\Services\App\EntrepriseServiceTest;
use Liip\TestFixturesBundle\Test\FixturesTrait;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\BrowserKit\Cookie;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
abstract class AbstractControllerTest extends WebTestCase
{
use FixturesTrait;
protected function setUp(): void
{
if (!$this->client) {
$this->client = static::createClient();
}
$this->loadFixtures([
"App\DataFixtures\AppFixtures",
])->getReferenceRepository();
}
My fixtures class :
class AppFixtures extends Fixture implements FixtureInterface
{
//...
}
But when I launch my tests, I've no error, but the fixtures don't charge.
Can someone help me please ?
PS: I'm using MySql database
I'm tries instance a class that extends Controller class
This is my Controller:
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class SalaryController extends Controller
{
public function calculateTotalSalary($id)
{
return $this->render('resources/view.html.twig');
}
}
and this is where I want to call the controller
<?php
namespace AppBundle\Tests\Units\Controller\Controller;
use AppBundle\Controller\SalaryController;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
class SalaryControllerTest extends KernelTestCase
{
public function testExample()
{
$controller = new SalaryController;
$controller->calculateTotalSalary(1);
}
}
When executed the code give me this:
Starting test 'AppBundle\Tests\Units\Controller\Controller\SalaryControllerTest::testExample'.
PHP Fatal error: Call to a member function get() on null \vendor
symfony\src\Symfony\Bundle\FrameworkBundle\Controller\Controller.php on line 176
Line 176 in Controller class:
return $this->container->get('templating')->renderResponse($view, $parameters, $response);
the $container variable is null, Why ?
I am getting the following error:
The autoloader expected class "Acme\HelloBundle\Controller\HelloController" to be defined in file "/var/www/Symfony/app/../src/Acme/HelloBundle/Controller/HelloController.php". The file was found but the class was not in it, the class name or namespace probably has a typo.
The controller code I have is actually:
namespace Acme\HelloBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
class HelloController
{
public function indexAction($name)
{
return new Response('<html><body>Hello '.$name.'!</body></html>');
}
}
any idea why this is?
<?php namespace Acme\HelloBundle\Controller;
....
Just add the "*LESS_THAN*"?php tag at the beginning. Try if works.
Your controller should extend Symfony\Bundle\FrameworkBundle\Controller\Controller
namespace Acme\HelloBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
use namespace Acme\HelloBundle\Controller;
class HelloController extends Controller
{
public function indexAction($name)
{
return new Response('<html><body>Hello '.$name.'!</body></html>');
}
}