Testing a Laravel package - php

So, I'm writing a basic Laravel package and I seem to have stumbled upon yet another problem, this time with testing.
The package in development is currently in a packages folder in the root of the project. I have modified the composer.json file of the package to include the dependencies I need
"require-dev": {
"phpunit/phpunit": "~4.0",
"laravel/laravel": "dev-develop"
}
However , whenever I try running phpunit tests in the package folder (which contains a folder named tests along with a sample test), I get the following error:
PHP Fatal error: Class 'Illuminate\Foundation\Testing\TestCase' not found in /workspace/laravel/packages/sample/http-request/tests/HttpRequestTest.php on line 8
The test file is just the auto-generated stub:
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class HttpRequestTest extends Illuminate\Foundation\Testing\TestCase
{
/**
* A basic test example.
*
* #return void
*/
public function testExample()
{
$this->assertTrue(true);
}
}
Any idea why this isn't working? The app tests run without a hitch, but the app itself doesn't have dependencies other than what's in the box.
SOLUTION
Managed to make it work independently by extending the PHPUnit_Framework_TestCase:
class HttpRequestTest extends PHPUnit_Framework_TestCase
However , running it like:
vendor/bin/phpunit packages/yourname/package-name/
Works as well, so I picked it as an answer.

This works for me:
class HttpRequestTest extends TestCase
And running test with:
vendor/bin/phpunit packages/yourname/package-name/

(Posted on behalf of the OP as an answer).
Managed to make it work independently by extending the PHPUnit_Framework_TestCase:
class HttpRequestTest extends PHPUnit_Framework_TestCase
However , running it like:
vendor/bin/phpunit packages/yourname/package-name/
Works as well, so I picked it as an answer.

For Windows environments you need to use backslashes!
vendor\bin\phpunit packages\yourname\package-name

Create a directory named tests in your root of the package and write various test classes inside it. You can have all functioning of your package inside various methods in a single class or you can use multiple classes.
Move to your project's root and run following commands,
vendor/bin/phpunit packages/YOUR_DIRECTORY_NAME/PACKAGE_NAME
Wait a little and you will get the response for your test cases. No. of positive and negative tests will be shown.

Related

Call to a member function connection() on null during tests

I want to do performant testing, so I thought about using PHPUnit_Framework_TestCase instead of TestCase in my test files. I'm sacrificing http requests testing, but I want at least to use some of the components of Laravel (like its providers and such).
Here's what it looks like :
<?php
class ChatTest extends PHPUnit_Framework_TestCase
{
/** #test */
public function chat_can_be_accepted()
{
$this->chat->setStatus(Chat::CHAT_STATUS_WAIT_MASTER);
$this->chat->accept();
$this->assertEquals(Chat::CHAT_STATUS_WAIT_CONFIRM, $this->chat->getStatus());
}
}
I have an error saying Call to a member function connection() on null. setStatus actually uses Laravel internals in this example (something as simple as a relationship that needs the app).
I already tried switching every providers, but it seems code is not even going through providers. Also tried changing PHP versions, or extensions.
I'm answering my own question for future reminders, and also because I never found this answer anywhere else.
At this point I was using PHPStorm and it doesn't auto load the phpunit.xml file when launching tests by default.
That line in the file launches Laravel (works with Lumen too) along with the tests : bootstrap="bootstrap/app.php".
This is needed to use Laravel internals even without using their test frameworks.
To use this in PHPStorm :
To use this in CLI :
phpunit --configuration phpunit.xml

Fatal error: Class 'PHPUnit_Framework_TestCase' not found in dbunit\PHPUnit\Extensions\Database\TestCase.php on line 24

I want to write tests for MySQL Database. When I try to extend the database extension, it gives me the above error in title:
class FixtureTestCase extends PHPUnit_Extensions_Database_TestCase
Note: I have installed PHPUnit globally and I can access it through phpunit in cmd. I have latest PHPUnit 4.5.1.
I also installed Dbunit globally through composer:
composer global require phpunit/dbunit
Any help will be much appreciated. I've used PHP 5.4.
For referring to classes that are located within the global namespace, you can simply prefix them with a backward ( \ ) slash.
Try with this:
class FixtureTestCase extends \PHPUnit_Extensions_Database_TestCase
With the leading backward ( \ ) slash, PHP knows that we are referring to the PHPUnit_Extensions_Database_TestCase class in the global namespace, and located that one.
More info here
Hope this help.
It just might be the version of DBUnit. For example I'm using PHPUnit 4.8 and it works with DBUnit 1.4.
I had the above error when I was using composer with "phpunit/dbunit": ">=1.2" and I ended up with DBUnit 2.0.
Test your DB how? Through an API you created or direct accesses to DB? i would recommend you to write an API to access your DB and then write the tests to the API.
for exemple if you want to insert a user you call your web-server address (localhost:3000) or something you have and then call /users through POST to insert and make the routes to that.
I would suggest using LARAVEL for that.

Adding a custom Artisan command with Behat

I've registered a custom Artisan command:
Artisan::add(new MigrateAll);
The class resides in app/commands (default location)
However when I run Behat I get the error:
Class 'MigrateAll' not found
Artisan is called in Behat for setting up the DB:
/**
* #static
* #beforeSuite
*/
public static function setUpDb()
{
Artisan::call('migrate:install');
//...
}
Do I need to give it a namespace? (I could not find the correct way to call the Artisan::add command with a namespaced class)
This is somewhat related to your earlier question. Your Behat test suite runs in a separate process independently of your app and knows nothing about the configuration. This also applies to the autoloading in your bootstrap and the autoloading would be the most likely reason why classes don't get found. This should be easily fixed by using Composer to autoload your own sources and vendor packages (both in your app and in your test suite).
# composer.json
{
"require": {
"…": "…"
},
"autoload": {
"psr-0": {
"": "../src"
}
}
}
// Include composer's autoloader in your `setUp()` / bootstrap / index.php.
include __DIR__ . '../vendor/autoload.php';
Take that process separation as a rule and keep in mind that Laravel like any other framework requires a whole bunch of other configuration. Since you are trying to use the database component, your next issue will be with that, because it won't be configured in your test suite.
The best approach is to create separate bootstrap file for Behat, which would inherit most lines from your normal bootstrap, where you need to pass the necessary configuration and do this:
/**
* #static
* #beforeSuite
*/
public static function setUp()
{
include_once('bootstrap.php');
}
If you configured your behat environment with this tut (Laravel, BDD And You: Let’s Get Started), after you added a new command, you need to $ composer dump-autoload to make behat to know the command.

PHPUnit CodeIgniter Generate Fixtures PHP Fatal error: Class 'PHPUnit_Framework_TestCase' not found

So have PHPUnit and CodeIgniter installed:
http://d.hatena.ne.jp/Kenji_s/20120117/1326763908
Couldn't download the PEAR as its been deprecated. So had to download the phpunit phar file:
http://phpunit.de/manual/4.0/en/installation.html#installation.phar
So was able to get some tests to run properly. Moved my phpunit.phar to /usr/local/bin and ran on the tests dir:
php /usr/local/bin/phpunit.phar
And all the tests ran correctly. But when i tried to run the php generate fixtures and php generate.php fixtures:
PHP Fatal error: Class 'PHPUnit_Framework_TestCase' not found in /www/test/application/third_party/CIUnit/libraries/CIUnitTestCase.php on line 15
Fatal error: Class 'PHPUnit_Framework_TestCase' not found in /www/test/application/third_party/CIUnit/libraries/CIUnitTestCase.php on line 15
Seems like its not finding the classes inside the phar file or at least they are not in the correct order? What is funny is that it runs the tests fine but not the generate fixtures.
Additionally i also installed using composer the phpunit so i have a /www/test/vendor/bin/phpunit installed as well.
Any help would be appreciated.
I had the same problem in my code, although I do not use the CodeIgniter. Trying to run tests would result in the error message:
Class 'PHPUnit_Framework_TestCase' not found
For what it's worth I had this fixed by adding a backslash to my test class declaration.
// Before
namespace IMAVendor\Super\Duper;
class MyClassTest extends PHPUnit_Framework_TestCase
// After
namespace IMAVendor\Super\Duper;
class MyClassTest extends \PHPUnit_Framework_TestCase
^
Added this backslash here
This seems to have something to do with namespaces and the autoloader that's built in phpunit. I have my own autoloader for the project code and it seems that it was trying to load the phpunit's classes from my code. I'm not really sure why it didn't try to load it from the 'base' when it wasn't able to find it in the projects namespace (This may very well be due to my own autoloader being faulty).
I know this is an old question, but I'll just leave this here in case it may help somebody somewhere.

Phalcon UnitTesting

I am running the example from the documentation: http://docs.phalconphp.com/en/latest/reference/unit-testing.html#sample-unit-test
I want to create an abstract unit test from Phalcon\Test\UnitTestCase as in the documentation. However when I run my test I become:
PHP Fatal error: Class 'Phalcon\Test\UnitTestCase' not found
I have followed the exact documentation steps. Did anyone have the same problem and solved it?
This class is part of the incubator: https://github.com/phalcon/incubator
$loader = new Phalcon\Loader();
$loader->registerNamespaces(array(
'Phalcon' => '/path/to/incubator/Library/Phalcon/'
));
$loader->register();
I figure it out.
Basically we have to do 2 things.
is whats in the #twistedxtra's answer. (setting up the path to where the incubator is)
in the testsTestUnitTest.php we created, it has the following line
class UnitTest extends \UnitTestCase {
we have to change that line to
class UnitTest extends \Phalcon\Test\UnitTestCase {
What we did was set the proper namespace so that the code knows where the UnitTestCase class is.
Thats it. Cheers...!!!
Make sure you run phpunit command in tests folder. It's very important.
Don't run something like phpunit tests/

Categories