ZF, ZFDoctrine and PHPUnit setup - unit-testing

Does anyone here use Zend Framework, ZFDoctrine and PHPUnit together?
How to rebuild the database on each test run?
How to separate local/production/testing environments?
Would you share your unit testing setup?
I have been trying something like that:
// /tests/bootstrap.php
// ... setup paths and constants here
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap('doctrine');
$provider = new ZFDoctrine_Tool_DoctrineProvider;
$provider->generateModelsFromYaml();
//$provider->buildProject(true);
But this ends in:
Notice: Constant APPLICATION_PATH already defined in /home/user/www/library/ZendFramework/1.10.7/library/Zend/Tool/Project/Context/Zf/BootstrapFile.php on line 106
Fatal error: Call to a member function getResponse() on a non-object in /home/user/www/library/zf-doctrine/library/ZFDoctrine/Tool/DoctrineProvider.php on line 271
Models are not generated.
I get similar errors running:
$provider->createDatabase();
But in this case database is created.
The other provider commands do not work.
The solution:
$provider = new ZFDoctrine_Tool_DoctrineProvider;
$registry = new Zend_Tool_Framework_Registry;
$provider->setRegistry($registry);
#$provider->buildProject(true);
If anybody knows a better approach, please correct me.

I haven't used ZFDoctrine, but just plain Doctrine 1.2. I don't know if my solution is better but I figured I post if any1 is interested, here's the bootstrap.php in my tests folder:
<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../application'));
// Define application environment
/**
* In the application.ini:
[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
doctrine.dsn = "mysql://my_user:passwd#localhost/my_phpunit_test_db"
*/
define('APPLICATION_ENV', 'testing');
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path()
)));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/../configs/application.ini'
);
$application->getBootstrap()->bootstrap();
// Can run out if too small
ini_set('memory_limit', '512M');
// Get the doctrine settings
$config = $application->getOption('doctrine');
$cli = new Doctrine_Cli($config);
$cli->run(array("doctrine", "build-all-reload","force"));
The key here is actually the last line that rebuilds all databases creating a clean environment for each testing.

Related

SoapClient does not recognize a function I am seeing in WSDL

I have a simple webservice in symfony2 that is working perfectly. I have added a new method, however, strangely, that method is not recognized, even when I see it in the WSDL definition.
Please load: WSDL definition
Method is called GetHoliday
The controller that executes that method is the following:
public function getHolidayAction() {
date_default_timezone_set('America/Santiago');
$request = $this->getRequest();
$client = new \SoapClient('http://' . $request->getHttpHost() . $request->getScriptName() . '/feriados?wsdl');
$year = $request->get('year');
$month = $request->get('month');
$day = $request->get('day');
$types = $client->__getFunctions();
var_dump($types);
die();
$result = $client->GetHoliday('8cd4c502f69b5606a8bef291deaac1ba83bb7727', 'cl', $year, $month, $day);
echo $result;
die();
}
After the call to __getFunctions call, GetHoliday method is missing.
If you want to see the __getFunctions response, please load online site
Enter any date in the input field. The response will appear in red.
The most curious thing, is that this works in my development machine which also has RedHat operating system (my hosting is HostGator).
Any help will be appreciated,
Finally, the problem was that the WSDL was being cached.
To make the first test, I used
$client = new \SoapClient('http://' . $request->getHttpHost() . $request->getScriptName() . '/feriados?wsdl', array('cache_wsdl' => WSDL_CACHE_NONE) );
To instantiate SoapClient. That way, it worked. so to get rid of WSDL_CACHE_NONE parameter, I deleted all files that start with wsdl in /tmp folder.
Regards,
Jaime

PHPUnit with Selenium2 - share session not working

I'm doing some tests with PHPUnit and Selenium and i would like all of them to run in the same browser window.
I've tried starting the Selenium Server with
java -jar c:\php\selenium-server-standalone-2.33.0.jar -browserSessionReuse
but with no visible change.
I've also tried with shareSession() in the setup
public function setUp()
{
$this->setHost('localhost');
$this->setPort(4444);
$this->setBrowser('firefox');
$this->shareSession(true);
$this->setBrowserUrl('http://localhost/project');
}
but the only change is that it opens a window for every test, and not really sharing the session. I'm out of ideas at this point.
My tests look like this:
public function testHasLoginForm()
{
$this->url('');
$email = $this->byName('email');
$password = $this->byName('password');
$this->assertEquals('', $email->value());
$this->assertEquals('', $password->value());
}
Here's the elegant solution. To share browser sessions in Selenium2TestCase, you must set sessionStrategy => 'shared' in your initial browser setup:
public static $browsers = array(
array(
'...
'browserName' => 'iexplorer',
'sessionStrategy' => 'shared',
...
)
);
The alternative (default) is 'isolated'.
You do not need to use the flag -browserSessionReuse
In your case The set up function running before every test and starting new instance.
This is what i did to prevent this to happen (Its little bit ugly but work for me both in Windows and Ubuntu):
I created helper class with static ver: $first and initialized it.
helper.php:
<?php
class helper
{
public static $first;
}
helper::$first = 0;
?>
Edit main test file setUp() function(and add require_once to helper.php):
require_once "helper.php";
class mySeleniumTest extends PHPUnit_Extensions_SeleniumTestCase
{
public function setUp()
{
$this->setHost('localhost');
$this->setPort(4444);
if (helper::$first == 0 )
{
$this->shareSession(TRUE);
$this->setBrowser('firefox');
$this->setBrowserUrl('http://localhost/project');
helper::$first = 1 ;
}
}
....
setHost and setPort outside the if because the values restarted after each test(For me...) and need to set up every time (if the selenium server is not localhost:4444)
Just found an (much) faster way to proceed : If you perform several test in one function, all test are performed in the same window. The setback is that the tests and reporting won't be nicely presented by tests, but the speed is way up!
In the same function for each test just use:
$this->url('...');
Or
$this->back();

ZF2 model unit test, class not found

Eh, this one makes my hair fall out...
I did some usefull stuff in zf1 and now I'm struggling to switch to zf2, and to do the thing right, I want to get stuff done TDD-style.
I've set up the Skeleton application, then made two additional modules, called "Weather" and "Airport". I than made a test case for WeatherController which works fine. Than I made a test case for models within Airport module and it fails with :
Fatal error: Class 'Airport\Model\Airport' not found in C:\xampp\htdocs...
, and the error is triggered here (AirportTableTest.php) :
<?php
namespace AirportTest\Model;
use Airport\Model\Airport;
use Airport\Model\AirportTable;
use PHPUnit_Framework_TestCase;
class AirportTableTest extends PHPUnit_Framework_TestCase {
public function testExample() {
$airport = new Airport(); // - this is not getting loaded and throws the fatal error :(
}
}
The code is based on the Album module example in ZF2 tutorial. The AirportTable model is supposed to interface a SQL table in the DB and the Airport model is written just like the Album model was written in the tutorial. The directory structure is (abbrevated) :
/module
/Airport
/src
/Airport
/Controller
/Model
AirportTable.php
Airport.php
/Application
/Weather
/public
/tests
/module
/Airport
/src
/Airport
/Controller
/Model
AirportTableTest.php
AirportTest.php
/Application
/Weather
bootstrap.php
phpunit.xml
/vendor
bootstrap.php from tests directory :
<?php
chdir(dirname(__DIR__));
error_reporting(E_ALL | E_STRICT);
include __DIR__.'/../init_autoloader.php';
The Airport.php with the class that is not being loaded :
<?php
namespace Airport\Model;
class Airport
{
public $icao;
public $lat;
public $lng;
public $metar;
public function exchangeArray($data){
$this->icao = (isset($data['id'])) ? $data['icao'] : null;
$this->lat = (isset($data['lat'])) ? $data['lat'] : null;
$this->lng = (isset($data['lng'])) ? $data['lng'] : null;
$this->metar = (isset($data['metar'])) ? $data['metar'] : null;
}
}
?>
The Module.php for Airport module :
<?php
namespace Airport;
use Airport\Model\Airport;
use Airport\Model\AirportTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
class Module
{
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
public function getServiceConfig()
{
return array(
'factories' => array(
'Airport\Model\AirportTable' => function($sm) {
$tableGateway = $sm->get('AirportTableGateway');
$table = new AirportTable($tableGateway);
return $table;
},
'AirportTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Airport());
return new TableGateway('airport', $dbAdapter, null, $resultSetPrototype);
},
),
);
}
}
So I'm probably missing something pretty obvious, like autoloader things related perhaps ? So, uhm... help maybe (pretty please) ?
Sooo, I came up with a working solution, although I'm not quite sure whether its smart or completly retarded.
Based on PHPUnit with a Zend Framework 2 module I added the line
Zend\Mvc\Application::init(include '/../config/application.config.php');
to the bootstrap.php of the test suite, and now everything works as expected, however I have no idea whatsoever why it would work without this line for the "Weather" module and not for "Airport" module...
You may want to have a look at the way the ZF2 getting started tutorial lays out tests. I have completed the tutorial and committed the changes to my own fork of the ZF2 Skeleton Application source.
Basically, each module has it's own test suite, with a dedicated Bootstrap file with configuration and a phpunit.xml that will tell PHPUnit to load all this when you run your test (so long as you're in the tests directory when running phpunit). This helps keep the tests modular.

How to enable Doctrine query logging in Symfony 1.4 unit tests

I am unit testing a model class and I would like all Doctrine queries to be logged.
My settings.yml for the test environment contains
logging_enabled: true
and my script
$configuration = ProjectConfiguration::getApplicationConfiguration( 'frontend', 'test', true);
new sfDatabaseManager( $configuration ) ;
Still, I don't see any log in any log file.
So, I found a workaround by using an event listener on the Doctrine profiler.
$profiler = new Doctrine_Connection_Profiler();
$conn = Doctrine_Manager::connection();
$conn->setListener($profiler);
/* tests go here */
foreach ($profiler as $event) {
echo $event->getQuery() . "\n";
}
But the same query is printed out several times for some reason (I am sure it is executed only once). Plus it is not so convenient to have the query logs dissociated from the rest of the log messages.

add dropbox option to joomla on new user creation

I'm using this plugin to permit the integration of dropbox into joomla.
When an user login te system create a folder in your dropbox account and everything works fine.
What I need is to add this option to the user creation page. I've tried to edit the save function in the controller com_users/controllers/user.php
JModel::addIncludePath (JPATH_ROOT . DS . 'components' . DS . 'com_dropbox' . DS . 'models');
$dropbox =& JModel::getInstance('dropbox', 'dropboxModel');
I need to pass values to the model here:
/**
* Creates a new folder
*
* This method returns the information from the newly created directory
*
* #param string $path
* #return stdclass
*/
public function createFolder($path="") {
$path= &Jfolder::makeSafe($path);
if (trim($path)=="")
{
//OK lets try to create the chroot
// $path=$this->dropbox->chroot;
}
$result = $this->auth->fetch('fileops/create_folder', array('path' => $this->dropbox->folder . '/' . $path, 'root' => $this->root),'POST');
return json_decode($result);
}
No results after various tries....
Please help me, thanks...!
You have to include the model file and call the function using the classname
Add the following line in the plugin:
jimport( 'joomla.filesystem.folder' );
require_once JPATH_ROOT . '/components/com_dropbox/models/filename.php';
dropboxModelfilename::functionname();