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());
}
}
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 using Codeception for unit, functional, and acceptance tests of my Laravel 4 PHP application.
My unit tests look this:
use Codeception\Util\Stub;
class ExampleTest extends \Codeception\TestCase\Test
{
public function testExample()
{
$example = true;
$this->assertSame($example, true);
}
}
My functional tests look like this:
use \TestGuy;
class ExampleCest
{
public function example(TestGuy $I)
{
$I->amOnPage('/auth/login');
$I->see('Sign in');
}
}
But I also want to use PHPUnit assert methods in my functional tests. But when I try to, I get this error:
Call to undefined method ExampleCest::assertSame()
How do I use PHP assert methods in a Codeception functional test?
Since Codeception 2.1 (not 2.0) you can use it like the other asserts with:
$I->assertSame($expected, $actual, $message);
But don't forget to enable the Asserts module in your config - e.g.:
class_name: UnitTester
modules:
enabled: [ Asserts ]
Please note: You might need to change your configuration when upgrading to 2.1 - see upgrade instructions: http://codeception.com/06-19-2015/codeception-2.1-rc.html
\PHPUnit_Framework_Assert::assertSame()
In Codeception 4 just add the Asserts Module:
modules:
enabled:
- \Codeception\Module\Asserts
to your suite.yml config file and run codeception build
Another workaround can be to use Helper Methods in test suite.
For example for assertSame() method
class ExpectedHelper extends \Codeception\Module
{
protected $test;
function _before(\Codeception\TestCase $test) {
$this->test = $test;
}
function assertSame($expected, $actual, $message = '')
{
$this->test->assertSame($exception, $actual, $message);
}
}
where ExpectedHelper being the test suite Helper name (eg: UnitHelper, FunctionalHelper) which should be under _support folder
and you can use it in your test as $I->assertSame('12340','12340');
In a directory I have two files
oneTest.php
<?php
class oneTest extends PHPUnit_Framework_TestCase {
public function testSomethingOne()
{
echo 'ONE TEST';
$this->assertEquals(1, 1);
}
}
twoTest.php
<?php
class twoTest extends PHPUnit_Framework_TestCase {
public function testSomethingTwo()
{
echo 'TWO TEST';
$this->assertEquals(2, 2);
}
}
From within the directory I can run both tests fine
phpunit oneTest.php
phpunit twoTest.php
And I get the expected output on both.
If I try and run all tests with
phpunit *
It only runs the first test.
I'm running phpunit 3.6.12 on Ubuntu 12.04.
Any ideas why this is happening?
Thanks
This is simply a limitation of phpunit, it is not programmed to support multiple files on the command line. You can, however, pass a directory name to phpunit. If you want to run the tests in the current directory, use
phpunit .
Edit: alternatively, you can specify a testsuite in a XML configuration file.
I'm trying to implement the GeocodableBehavior on a Symfony 1.4 (with Propel 1.6) project i'm working on, but until now it's a complete failure. I've tried to search if other people but I didn't found anything, like if I was the only one having troubles with this.
So, maybe I'm missing something very very easy, but following the instructions given on the GeocodableBehavior leads to nothing but errors, and I can't figure out where's the problem.
I followed instructions for the GeocodableBehavior (here -> http://www.propelorm.org/cookbook/geocodable-behavior.html)
This seems to work as i'm getting the latitude/longitude columns created on my model. Until then, it works fine.
Where things get a little more complicated is when trying to save an object with the GeocodableBehavior, there's problems with the Geocoder class.
(Documentation here -> https://github.com/willdurand/Geocoder)
My class is Point, referring to a geolocated point, an address. When creating a Point using sf admin generator, the behavior which is supposed to use some fields (street, postal_code, country, etc) to query the GoogleMaps api, just fails to use the Geocoder class.
Fatal error: Class 'Geocoder\Geocoder' not found in /var/www/vhosts/www._________.local/lib/model/om/BasePoint.php on line 3717
I put the Geocoder class in a lib/vendor/geocoder folder, I tried to use the autoload.yml file to load it, but nothing changes...
autoload:
geocoder:
name: geocoder
path: %SF_LIB_DIR%/vendor/geocoder
recursive: on
There's something i'm missing in how to load those classes in my sf project, and i can't find what. Geocoder package has an autoload.php file but i didn't manage to "load" it successfully...
Thanks in advance.
I know it's kinda giving up on the autoloader, but you could establish a register function in /config/ProjectConfiguration.class.php. The only downside is that you will need to add a call to the function before any block that uses Geocoder.
class ProjectConfiguration extends sfProjectConfiguration
{
static protected $geocoderLoaded = false;
static public function registerGeocoder()
{
if (self::$geocoderLoaded) {
return;
}
require_once sfConfig::get('sf_lib_dir') . '/vendor/geocoder/autoload.php';
self::$geocoderLoaded = true;
}
...
}
Then just execute ProjectConfiguration::registerGeocoder(); anywhere you'd need the class. It's more annoying than getting the autoloader to work, but it's at least dependable.
Did you check your autoload cache to see it there is something related to Geocoder?
/cache/[apps_name]/dev/config/config_autoload.yml.php
/cache/project_autoload.cache
Maybe, manually add the autoload in the /config/ProjectConfiguration.class.php:
class ProjectConfiguration extends sfProjectConfiguration
{
public function setup()
{
require_once sfConfig::get('sf_lib_dir').'/vendor/geocoder/src/autoload.php';
Using the built-in autoloader should be a working option, but you can also combine symfony's autoloader with a "PSR-0 enabled" one. Basically, this boils down to the following implementation:
public function setup()
{
// plugin stuff here
// register the new autoloader
spl_autoload_register(array($this, 'autoloadNamespace'));
}
public function autoloadNamespace($className)
{
$className = ltrim($className, '\\');
$fileName = '';
$namespace = '';
if ($lastNsPos = strripos($className, '\\'))
{
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
// make sure that the path to Geocoder is correct
foreach(array(
sfConfig::get('sf_lib_dir').'/vendor/Geocoder/src' . DIRECTORY_SEPARATOR . $fileName . $className . '.php',
) as $fileName)
{
if (file_exists($fileName))
{
require $fileName;
return true;
}
}
return false;
}
With this additional autoloader, your application should be able to use Geocoder.
This one's got me stumped. I've been working with PHPUnit for a couple of months now, so I'm not that green...but I look forward to being pointed in the direction of the obvious mistake I'm making! The initialisation process outlined below works fine if I run the "app" from a browser - but PHPUnit is choking...can any one put me out of my misery?
I'm trying to test a homebrew MVC, for study purposes. It follows a typical ZF layout.
Here's the index page:
include './../library/SKL/Application.php';
$SKL_Application = new SKL_Application();
$SKL_Application->initialise('./../application/configs/config.ini');
Here's the application class (early days...)
include 'bootstrap.php';
class SKL_Application {
/**
* initialises the application
*/
public function initialise($file) {
$this->processBootstrap();
//purely to test PHPUnit is working as expected
return true;
}
/**
* iterates over bootstrap class and executes
* all methods prefixed with "_init"
*/
private function processBootstrap() {
$Bootstrap = new Bootstrap();
$bootstrap_methods = get_class_methods($Bootstrap);
foreach ($bootstrap_methods as $method) {
if(substr($method,0,5) == '_init'){
$bootstrap->$method();
}
}
return true;
}
}
Here's the test:
require_once dirname(__FILE__).'/../../../public/bootstrap.php';
require_once dirname(__FILE__).'/../../../library/SKL/Application.php';
class SKL_ApplicationTest extends PHPUnit_Framework_TestCase {
protected $object;
protected function setUp() {
$this->object = new SKL_Application();
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown() {
}
public function testInitialise() {
$this->assertType('boolean',$this->object->initialise());
}
}
But I keep stumbling at the first hurdle!!
PHP Warning: include(bootstrap.php): failed to open stream:
No such file or directory in path\to\files\SKL\Application.php on line 9
any ideas?
Use include_once or better yet require_once instead of include to include the bootstrap.php in the Application class file. Despite being already loaded include loads it again but since it's obviously not on the include path you get the error.
Thanks to Raoul Duke for giving me a push in the right direction, here's where I got to so far
1 - add the root of the application to the include path
2 - make all inclusion paths relative to the root of the application
3 - include a file in your unit tests that performs the same function, but compensates for the relative location when it is included. I just used realpath() on the directory location of the files.
The problem I have now is that the darn thing won't see any additional files I'm trying to pass it.
So, I'm trying to test a configuration class, that will parse a variety of filetypes dynamically. The directory structure is like this:
Application_ConfigTest.php
config.ini
The first test:
public function testParseFile() {
$this->assertType('array',$this->object->parseFile('config.ini'));
}
The error:
failed to open stream: No such file or directory
WTF? It's IN the same directory as the test class...
I solved this by providing an absolute (i.e. file structure) path to the configuration file.Can anyone explain to me how PHPUnit resolves it's paths, or is it because the test class itself is included elsewhere, rendering relative paths meaningless?