PHPUnit and different class naming convention - php

I have following problem with setting up my phpunit tests. The project I'm working on has different class naming convetion, for example - class name is cMailing and the name of that file is class_mailing.php. How to automate phpunit lookup for that class in my tests?

Use bootstrap file for autoloding your classes.
http://phpunit.de/manual/3.7/en/textui.html
http://jes.st/2011/phpunit-bootstrap-and-autoloading-classes/

Related

Codeception unit tests - error Class 'Dog' not found

I'm using Codeception to TDD the development of a simple application in PHP. I've created my first unit test called DogTest.php with a simple assertion, but it's complaining about not being able to find the Dog class.
I have created a Dog.php file in the root directory, and also placed it under /src, but neither is working. I think this is either a namespace issue or an autoloader issue, but the Codeception documents (and the various TDD guides I've looked at) have this important detail missing.
Can someone please advise on how to get my DogTest to detect the Dog class?
In your composer.json, make sure you have added autoloading configuration. As an example:
"autoload":{
"psr-4":{
"Del\\":"src/"
}
}
Every file in src should have namespace Del. For instance, src/Blank.php would look like:
<?php
namespace Del;
class Blank
{
}
Whereas src/Http/Client.php would have namespace Del\Http.
Once added, run composer dumpautoload to generate the class maps. Your classes should now autoload without problems.
See my Blank starter project with codeception test for more info.
https://github.com/delboy1978uk/blank

autoloading nested classes with composer

I have created a very basic validator class.
My base code is in a my src/ folder, which gets autoloader with
"kevdotbadger\\Validator\\": "src/"
this works fine, so that when I instantiate a new "kevdotbadger\Validator\ Validator is gives me src/Validator.php
My Validator.php class then loads a bunch of sub-classes in my src/Rules directory. These are magically loaded by using the __call, so ->between() should look for src/Rules/between.php. However, for some reason it won't usual load despite it being setup in my composer.json file.
My whole codebase is available at https://github.com/kevdotbadger/validator/
Have I setup my namespace correctly? I think the problem might be with php version 5.3, however I need to use version 5.3.
Thanks.
Well you need to keep the guidelines of psr-4 as you are using it to autoload.
change the folder name "rules" to "Rules"
Uppercase all your file names of classes like:
between.php --> Between.php
that should do the job

PHPUnit and auto require of testing class

Isn't PHPUnit supposed to require the class itself using the naming conventiong "ClassTest"?
I have a php file "Router.php" with a class Router
My PHPUnit testing class is defined as following:
class RouterTest extends PHPUnit_Framework_TestCase
However it raises a fatal error for file not found "Router.php".
If I require it manually everything is ok.
What's wrong with it?
There is nothing wrong with it. How would PHPUnit know where your classes are in relation from the tests?
For your tests, you need to either require the class under test yourself or create an autoloader that will require the class for you. PHPUnit has no idea where it should be getting the files from. It has an autoloader for its internal files but that is it.

php zf project: db classes in models directory do not get auto-included

I'm trying to create a Zend Framework project using PHP 5.3.2 and Zend Framework 1.10.3.
i created the source files of the project using the zend framework tool and the db related classes i created with zend-db-model-generator.
example:
in models directory i have the following:
FbUser.php - class Default_Model_FbUser
FbUserMapper.php - class Default_Model_FbUserMapper
DbTable/FbUser.php - class Default_Model_DbTable_FbUser
The models in the models directory should be included automatically when I use them in one of the controllers, but it seems that it doesn't.
what should i configure in order for the db class models will be auto-included when used ?
update
I tried adding the following code to the bootstrap file:
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('Default_');
the autoloader still looks for Default/Model/FbUser.php in include path instead of Model/FbUser.php in the zf project.
I did not need to use Zend_Loader_Autoloader at all, although it's a cool component to auto-load classes in your include path.
i need to add to application.ini which is the main config of the application, the following line:
appnamespace = "Default_"
The program understands the application name space and then loads the db model classes properly.

PHPUnit filenaming conventions

I'm just starting to try out phpunit on some existing code. The naming convention we use is that the MyClass class should be in MyClass.class.php. PHPUnit seems to require that the file should be called MyClass.php.
Is there any way around this?
I noticed it while trying to generate a skeleton test class:
phpunit --skeleton-test MyClass.class
PHPUnit 3.3.4 by Sebastian Bergmann.
Could not find class "MyClass.class" in "/home/jd/skeleton/classes/MyClass.class.php".
Fatal error: Call to a member function getOutClassName() on a non-object in /usr/share/php/PHPUnit/TextUI/Command.php on line 470
Its not a requirement, its just assumptive. You can write your own test-cases.
Skeleton just makes a mock-up one "the easy way" that makes dummy functions for all your classes dummy functions.
Also,
phpunit --skeleton-test MyClass MyClass.class.php
Might do what you want.
Usage: phpunit [switches] UnitTest [UnitTest.php]
phpunit [switches] <directory>
--skeleton-class Generate Unit class for UnitTest in UnitTest.php.
--skeleton-test Generate UnitTest class for Unit in Unit.php.
so by that reasoning, if you don't tell it what file the class is in it will try guess, if you do tell it what file the class is in, it wont guess.
I ran up against a similar problem when the class a skeleton is being generated for is namespaced. I'm using phpunit 3.5.6 from the command-line. The phpUnit documentation example didn't work for me and I couldn't find the answer online so I figured I share it here. Suppose you have class Foo inside directory /my/dir and it's declared in a single file Foo.php so the full path to the class file is /my/dir/Foo.php
Also suppose that Foo is declared as follows:
<?php
namespace my\space;
class Foo
{
...
}
In order to generate a test skeleton I made sure I was in the directory where Foo.php resides and then I ran the following command:
user[/my/dir]$ phpunit --skeleton-test my\\dir\\Foo Foo.php
That got my skeleton to generate. The key was escaping the backslash as the documentation of phpUnit mentions namespaces but only shows one backslash. Hope this helps someone.

Categories