first time in phpunit, With some reference tutorial, i created below code. i don't know what actually below code going to do.
<?php
class MyGuestbookTest extends PHPUnit_Extensions_Database_TestCase
{
/**
* #return PHPUnit_Extensions_Database_DB_IDatabaseConnection
*/
protected function getConnection()
{
$db = new PDO("mysql:host=localhost;dbname=test","root", "");
return $this->createDefaultDBConnection($db, "test");
}
/**
* #return PHPUnit_Extensions_Database_DataSet_IDataSet
*/
public function getDataSet()
{
return $this->createXMLDataSet("seed.xml");
}
}
?>
But i notices when the run the " phpunit MyGuestbookTest " in the command line. i am getting following error " No tests found in class "MyGuestbookTest"."
D:\html\wamp\www\tdd>phpunit MyGuestbookTest PHPUnit 4.8.18 by
Sebastian Bergmann and contributors.
F
Time: 721 ms, Memory: 9.25Mb
There was 1 failure:
1) Warning No tests found in class "MyGuestbookTest".
FAILURES! Tests: 1, Assertions: 0, Failures: 1.
please advice me to fix this error, also i would like to know, How this program call the methods after we ran the command in the command line.
how really working ?
From phpunit documentation:
The tests are public methods that are named test*.
Your test class don't have that kind of method, and that's why there is a "no tests" warning.
Related
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.
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!
I recently created a new symfony project (3.1) with a dependency on graphaware/neo4j-php-ogm and neo4j/neo4j-bundle to manage my database.
Then I created a new Entity class named User with properties (login, password, ...) and I want to automatically set the current date before the flush event occurs (on preFlush).
I saw the PRE_FLUSH constant in neo4j-php-ogm/src/Events.php (https://github.com/graphaware/neo4j-php-ogm/blob/master/src/Events.php) but I haven't found any information about it in the documentation.
Well, my question is : Can we use this functionality in the actual version of the OGM ? If yes, do you have an example of the usage ?
Thank you for your help !
Yes you can, it is not documented you are right, I'll make sure it will be soon.
Integration test here : https://github.com/graphaware/neo4j-php-ogm/blob/master/tests/Integration/EventListenerIntegrationTest.php
First, You need create a class that will act as EventListener to the preFlush event of the EntityManager and a method reacting to the event :
<?php
namespace GraphAware\Neo4j\OGM\Tests\Integration\Listeners;
use GraphAware\Neo4j\OGM\Event\PreFlushEventArgs;
use GraphAware\Neo4j\OGM\Tests\Integration\Model\User;
class Timestamp
{
public function preFlush(PreFlushEventArgs $eventArgs)
{
$dt = new \DateTime("NOW", new \DateTimeZone("UTC"));
foreach ($eventArgs->getEntityManager()->getUnitOfWork()->getNodesScheduledForCreate() as $entity) {
if ($entity instanceof User) {
$entity->setUpdatedAt($dt);
}
}
}
}
Then you can register this event listener after having creating the entity manager :
/**
* #group pre-flush
*/
public function testPreFlushEvent()
{
$this->clearDb();
$this->em->getEventManager()->addEventListener(Events::PRE_FLUSH, new Timestamp());
$user = new User("ikwattro");
$this->em->persist($user);
$this->em->flush();
$this->assertNotNull($user->getUpdatedAt());
var_dump($user->getUpdatedAt());
}
Result of the test :
ikwattro#graphaware-team ~/d/g/p/ogm> ./vendor/bin/phpunit tests/ --group pre-flush
PHPUnit 5.6.2 by Sebastian Bergmann and contributors.
Runtime: PHP 5.6.27
Configuration: /Users/ikwattro/dev/graphaware/php/ogm/phpunit.xml.dist
. 1 / 1 (100%)int(1486763241)
Time: 378 ms, Memory: 5.00MB
OK (1 test, 1 assertion)
Result in the database :
Thank you a lot ! It's work perfectly. If anyone want to use it don't forget to type your property as "int". ;)
I'm working on a Symfony2 Command, and I have an error when I try to run the command in prod environment, but it work perfectly in dev :
Everything is fine if I run :
php app/console mycommand:start
When I run
php app/console mycommand:start --env=prod
I get this error :
[LogicException]
The command defined in "NameSpace\Of\MyClassCommand" cannot have an empty name.
Command definition :
services:
my_service.command:
class: NameSpace\Of\MyClassCommand
arguments:
name: mycommand:start
tags:
- { name: console.command }
Command :
class MyClassCommand extends Command
{
/**
* #param InputInterface $input
* #param OutputInterface $output
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
// logic
}
...
}
As you may notice, I don't overload the __construct or the configure methods. I tried but it didn't change anything. Same thing if I clean the cache.
Does anyone have an idea of what could be the difference between the prod and the dev environment, to cause this problem ?
Thank you
Clearing cache is not working for me. I need to declare base class as abstract class to hint Symfony not to register it as command.
abstract class BaseCommand {
...
}
I finally found the solution, Ruslan you were right : clear the cache.
But I had to manually nuke the cache folder, the cache:clear command was not enough.
Hello again SO!
I'm trying to get PHPunit to run on localhost, here are some of my specs
xDebugger : v 2.2 (enabled)
php : 5.4.3
PHPunit : tried with 3.7.31 && 4.0.17
Running tests works fine, However whenever I use the coverage-html the output is always 0% covered. I've tried this with both version of PHPunit.
Whenever i try the --coverage-text command I get the same result, the tests run fine(fail/success), however the coverage is 0%.
1 test - 1 assertion - 0
For simplicity, I created these two classes :
class my
{
function method()
{
$bool = true;
echo $bool;
}
}
and the test class :
require_once 'my.php';
class myTest extends PHPUnit_Framework_TestCase
{
function testequal()
{
$bool = true;
echo $bool;
$this->assertTrue($bool);
}
}
two different files, the file names are my.php and myTest.php.
If I can provide anymore information please let me know, Thanks in advance.
You're actually not testing the code of my. Isn't it? That's why the coverage is 0%.
Change the test code to this:
require_once 'my.php';
class myTest extends PHPUnit_Framework_TestCase
{
function testSomething()
{
$object = new my();
$this->assertEquals('1', $object->method());
}
}