PHPunit 9.23 PHP 7.4.7 Warning no test found in class - phpunit

I'm trying to write some test using PHPUNit but somehow the tests can't be recognized when I try to run .\vendor\bin\phpunit.
Here is an example of my Test class
use \App\Controllers\city_controller;
use PHPUnit\Framework\TestCase;
class city_controllerTest extends TestCase
{
/**
* #test
*/
public function test_AddCity(){
$this->assertTrue(true);
}
}
I always receive the "Warning No tests found in class".
Can anyone help me?
Thanks a lot.

In the doc the method name must start by "test".
I'd rename test_AddCity() by testAddCity()
And your class to TestCityController
Hope that'll work form you as it did for me!

Related

The "" directory does not exist - Laravel 7 + PhpUnit

I am starting to test laravel 7 application. I have created a simple test:
namespace Tests\Unit;
use Tests\TestCase;
class LoginTest extends TestCase
{
/** #test */
public function user_login() {
$response = $this->get('/');
$response->assertStatus(200);
}
}
but, when I run vendor/bin/phpunit, I get this error:
1) Tests\Feature\LoginTest::user_login
Symfony\Component\Finder\Exception\DirectoryNotFoundException: The "" directory does not exist.
When I change this line
use Tests\TestCase;
to
use PHPUnit\Framework\TestCase;
I get a different error:
1) Tests\Unit\LoginTest::user_login
Error: Call to undefined method Tests\Unit\LoginTest::get()
What I am doing wrong? Can someone help me figure it out, please?
Thank you very much.

(Symfony 4) Unable to inject a github library that I installed with composer

I installed the following library from GitHub: https://github.com/rosell-dk/webp-convert
The location of the main class that I need to is (from project root) :
\vendor\rosell-dk\webp-convert\src\WebPConvert.php
Here is how the WebPConvert.php class starts out:
namespace WebPConvert;
use WebPConvert\Converters\ConverterHelper;
use WebPConvert\ServeExistingOrConvert;
use WebPConvert\Serve\ServeExistingOrHandOver;
class WebPConvert
{
In the repository class where I use it, here is how I tried to do my dependency injection:
use WebPConvert\WebPConvert;
class PhotoRepository extends ServiceEntityRepository
{
/**
* #var WebPConvert
*/
protected $webPConverter;
public function __construct(WebPConvert $webPConverter)
{
$this->webPConverter = $webPConverter;
}
I followed the instructions from https://symfony.com/doc/current/service_container.html
But I still get this message:
Cannot autowire service "App\Repository\PhotoRepository": argument "$webPConverter" of method "__construct()" references class "WebPConvert\WebPConvert" but no such service exists.
I have even tried putting this in my services.yaml and it doesn't work:
App\Repository\PhotoRepository:
arguments:
- WebPConvert\WebPConvert
Is there an extra step I am missing?
This is Cerad's answer which worked:
WebPConvert is not a Symfony bundle so it won't have any services defined. You will have to define them manually. Actually, from the readme file, it looks like WebPConvert::convert is a static method so there is nothing to inject. Just follow the example.

symfony 2.3 error: autoloader find file but not class

First, sorry if my english it's not so good.
I readed a lot of questions like the one i have, but any solution works.
The question is that I'm developing a porject in Symfony 2.3, yes, i'm beginner using it...
I've created a 'Userbundle', and i want to display the info profile of an user.
When I access to the correct URL I have the famous message error:
"The autoloader expected class "Mylife\UserBundle\Entity\UserRepository" to be defined in file "D:\www\Symfony/src\Mylife\UserBundle\Entity\UserRepository.php". The file was found but the class was not in it, the class name or namespace probably has a typo."
That's my default controller code:
namespace Mylife\UserBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Mylife\UserBundle\Entity\User;
use Mylife\UserBundle\Form\Frontend\RegisterType;
class DefaultController extends Controller
{
public function profileAction(){
$user_id=1;
$em=$this->getDoctrine()->getEntityManager();
$profile=$em->getRepository('UserBundle:User')
->findProfile($user_id);
return $this->render('UserBundle:Default:profile.html.twig', array( 'profile' => $profile));
}
And my UserRepository.php code:
// src/Mylife/UserBundle/Entity/UserRepository.php
namespace Mylife\UserBundle\Entity;
use Doctrine\ORM\EntityRepository;
class UserRepository extends EntityRepository
{
public function findProfile($user)
{
$em = $this->getEntityManager();
$consult= $em->createQuery('
SELECT u, nk, n
FROM UserBundle:User u
WHERE u.user= :id');
$consult->setParameter('id', $user);
return $consult->getResult();
}
}
I have the same problem when trying to use a form class in the same bundle, but i no see any error in namesapce or class name.
The project structure is:
-src
-Mylife
-UserBundle
....
-Entity
...
-User.php
-UserRepository.php
I'm going mad trying to solve the problem and reading a lot of forums and examples.
I've tryed to dissable APC, to restart Apache, erase the cache, and nothing of this worked.
Thanks a lot!!
Carlos
PD: I'm not sure why appears a piece of code at the top of the error page and why it begins in "getEntityMAnager();..." row... Why is not showing the text code before it?. Image:http://es.tinypic.com?ref=r0s8k5
IMPORTANT: When I generated the entity USer by console, I say "no" when asked to generate repository. May be this is the problem. Any suggestion now?
Thanks again
Try to add this comment in your User entity file:
/**
*
* #ORM\Entity(repositoryClass="YourProject\UserBundle\Entity\UserRepository")
*/
class User
{
...
}
or something like this:
custom repository class in symfony2
Found it!!
It's was a silly mistake. I've began the PHP repository file with
<?
...
and must be
<?php
...
Sorry at all!

Functional tests on Symfony 2 - issue with the default autoload.php.dist

I have been trying to run FunctionalTest which extends the Symfony\Bundle\FrameworkBundle\Tests\Functional\WebTestCase with not so much success.
The issue is that:
the code in FrameworkBundle\Tests\Functional\app\AppKernel.php attempts to load autoload.php.dist in the framework bundle (not the one in the app/).
And autoload.php.dist then tries to load vendor\autoload.php which does not exist within this path.
If I remove the autoload.php.dist in the FrameworkBundle then everything is fine, but I want to avoid doing that because each time I do composer update I will then have to remove that specific fine.
I wonder what I'm doing wrong.
The exact error from the console is posted below for your information:
Configuration read from D:\xampp\htdocs\demo\app\phpunit.xml.
dist
require_once() called at [D:\xampp\htdocs\demo\vendor\sym
fony\symfony\src\Symfony\Bundle\FrameworkBundle\Tests\Functional\app\AppKernel.p
hp:26]
require_once(D:\xampp\htdocs\demo\vendor\symfony\symfony\
src\Symfony\Bundle\FrameworkBundle\Tests\Functional\app\AppKernel.php)
called at
[D:\xampp\htdocs\demo\vendor\symfony\symfony\src\Symfony\Bun
dle\FrameworkBundle\Tests\Functional\WebTestCase.php:47]
Fatal error: main(): Failed opening required
'D:\xampp\htdocs\demo\vendor\symfony\symfony/vendor/autoload.php'
(include_path='.;D:\xampp\php\PEAR') in
D:\xampp\htdocs\demo\vendor\symfony\sym
fony\autoload.php.dist on line 9
The test class simply extends WebTestCase with the setUp looks like this:
static::$kernel = static::createKernel(array('test_case'));
static::$kernel->boot();
$this->containter = static::$kernel->getContainer();
Okay. I see the problem. You are extending from Tests\Functional\WebTestCase. That is actually a test for testing the WebTestCase. You want to extend from Test\WebTestCase.
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class PersonRepositoryTest extends WebTestCase
{
public function testProject()
{
$client = static::createClient();
$manager = $client->getContainer()->get('cerad_person.manager');
And you might want to get one or two working using the $client line shown above. All the setup stuff can be a bit tricky.

Symfony2 custom console command not working

I created a new Class in src/MaintenanceBundle/Command, named it GreetCommand.php and put the following code in it:
<?php
namespace SK2\MaintenanceBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class GreetCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('maintenance:greet')
->setDescription('Greet someone')
->addArgument('name', InputArgument::OPTIONAL, 'Who do you want to greet?')
->addOption('yell', null, InputOption::VALUE_NONE, 'If set, the task will yell in uppercase letters')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$name = $input->getArgument('name');
if ($name) {
$text = 'Hello '.$name;
} else {
$text = 'Hello';
}
if ($input->getOption('yell')) {
$text = strtoupper($text);
}
$output->writeln($text);
}
}
?>
And tried to call it via
app/console maintenance:greet Fabien
But i always get the following error:
[InvalidArgumentException]
There are no commands defined in the "maintenance" namespace.
Any ideas?
I had this problem, and it was because the name of my PHP class and file didn't end with Command.
Symfony will automatically register commands which end with Command and are in the Command directory of a bundle. If you'd like to manually register your command, this cookbook entry may help: http://symfony.com/doc/current/cookbook/console/commands_as_services.html
I had a similar problem and figured out another possible solution:
If you override the default __construct method the Command will not be auto-registered by Symfony, so you have to either take the service approach as mentioned earlier or remove the __construct override and make that init step in the execute method or in the configure method.
Does actually anyone know a good best practice how to do init "stuff" in Symfony commands?
It took me a moment to figure this out.
I figured out why it was not working: I simply forgot to register the Bundle in the AppKernel.php. However, the other proposed answers are relevant and might be helpful to resolve other situations!
By convention: the commands files need to reside in a bundle's command directory and have a name ending with Command.
in AppKernel.php
public function registerBundles()
{
$bundles = [
...
new MaintenanceBundle\MaintenanceBundle(),
];
return $bundles;
}
In addition to MonocroM's answer, I had the same issue with my command and was silently ignored by Symfony only because my command's constructor had 1 required argument.
I just removed it and call the parent __construct() method (Symfony 2.7) and it worked well ;)
If you are over-riding the command constructor and are using lazy-loading/autowiring, then your commands will not be automatically registered. To fix this you can add a $defaultName variable:
class SunshineCommand extends Command
{
protected static $defaultName = 'app:sunshine';
// ...
}
Link to the Symfony docs.
I think you have to call parent::configure() in your configure method
I had this same error when I tried to test my command execution with PHPUnit.
This was due to a wrong class import :
use Symfony\Component\Console\Application;
should be
use Symfony\Bundle\FrameworkBundle\Console\Application;
cf. Other stack thread
In my case it was complaining about the "workflow" namespace although the WorkflowDumpCommand was correctly provided by the framework.
However, it was not available to run because I have not defined any workflows so the isEnabled() method of the command returned false.
I tried to use a service passed via constructor inside the configure method:
class SomeCommand extends Command {
private $service;
public function __construct(SomeService $service) {
$this->service = $service;
}
protected function configure(): void {
$this->service->doSomething(); // DOES NOT WORK
}
}
Symfony uses Autoconfiguration that automatically inject dependencies into your services and register your services as Command, event,....
So first just make sure that you have services.yaml in your config folder. with autoconfigure:true.
this is the default setting
Then Make sure That All your files are exactly the same name as Your Class.
so if you have SimpleClass your file must be SimpleClass.php
If you have a problem because of a __constructor,
go to services.yml and add something like this:
app.email_handler_command:
class: AppBundle\Command\EmailHandlerCommand
arguments:
- '#doctrine.orm.entity_manager'
- '#app.email_handler_service'
tags:
- { name: console.command }
For newer Symfony-Version (5+) commands must be registered as services.
What I do frequently forget while setting it up, is to tag it properly:
<service id="someServiceCommand">
<tag name="console.command"/>
</service>
Without this litte adaptation, your command name will not be displayed and therefore not accessible.

Resources