NetBeans's include path doesn't work - php

I have a NetBeansProject that works on ZendFramework (the library) and uses PHPUnit to do the tests but each time some function of the Zend Framework is called (the bootstrap file calls them for example) it gives a fatal error because it can't find those files:
Fatal error: require_once(): Failed opening required 'Zend/Db/Table/Abstract.php' (include_path='.;C:\xampp\htdocs\pear;C:\xampp\php\PEAR') in....
My bootstrap file looks like this:
<?php
ini_set('display_startup_errors', 1);
ini_set('display_errors', 2);
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'testing'));
defined('LIBRARY_PATH')
|| define('LIBRARY_PATH', realpath(dirname(__FILE__) . '/../library'));
defined('TESTS_PATH')
|| define('TESTS_PATH', realpath(dirname(__FILE__)));
//Define constant global variable
defined('_FOO_ROOT_DIR_') || define('_FOO_ROOT_DIR_', dirname(dirname(__FILE__)));
defined('_FOO_APP_DIR_') || define('_FOO_APP_DIR_', _FOO_ROOT_DIR_ . '/application');
defined('_FOO_LIB_DIR_') || define('_FOO_LIB_DIR_', _FOO_ROOT_DIR_ . '/library');
defined('_FOO_PUBLIC_DIR_') || define('_FOO_PUBLIC_DIR_', _FOO_ROOT_DIR_ . '/public');
defined('_FOO_TEST_DIR_') || define('_FOO_TEST_DIR_', _FOO_ROOT_DIR_ . '/tests');
defined('_FOO_ZF_DIR_') || define('_FOO_ZF_DIR_', 'C:\zend\ZendFramework-1.11.11\library');
require_once 'Zend/Db/Table/Abstract.php';
.
.
.
.
#End of bootstrap.php
and that require_once is what triggers the error. If for example I modify the bootstrap file and instead of doing the require from 'Zend/Db/Table/Abstract.php' I do it from _FOO_ZF_DIR_.'/Zend/Db/Table/Abstract.php' then the problem gets solved, but I have to rewrite every single include from every single file of the Zend library.
The path to the framework on the include is correct, and I have configured my PHPUnit with the bootstrap file I partially copied above and that phpunit.xml that was already given to me by my partner who has the project set and running.
The phpunit.xml looks like this (I don't know if it's relevant to the topic):
<phpunit bootstrap="./bootstrap.php" colors="true">
<testsuite name="ApplicationTestSuite">
<directory>./application/</directory>
<directory>./library/</directory>
</testsuite>
<filter>
<whitelist>
<directory suffix=".php">../application</directory>
<exclude>
<directory suffix=".php">../application/modules/pal</directory>
<directory suffix=".phtml">../application/views</directory>
<file>../application/Bootstrap.php</file>
</exclude>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="./log/coveragereport" charset="UTF-8"
yui="true" highlight="false" lowUpperBound="35" highLowerBound="70"/>
</logging>
</phpunit>
I have also configured the Zend tab on the options tools, registering the provider and such.
So how could I fix this so that the NetBeans can detect that the includes have to be found not only in relation to the project folder but also in relation to the included library path?
I've also tried to include the library through the global php includes and the project specific ones (through project properties) and neither of them works...
Thank you in advance

Solved!
I forgot to include the path to the ZendFramework library on the php.ini file.
This is what I had:
include_path=".;C:\xampp\htdocs\pear;C:\xampp\php\PEAR"
And this is what is required for it to work:
include_path=".;C:\xampp\htdocs\pear;C:\xampp\php\PEAR;C:\zend\ZendFramework-1.11.11\library"
Regards!

Related

How to compare 2 URL adres with PHPUnit?

I am working on a task that is expose a xml file with given url. I want to test my code and I do this with PHPUnit. I used composer to install PHPUnit.
The example test that I want to run is this :
<?php
use PHPUnit\Framework\TestCase;
class IndexTest extends TestCase{
public function testGetXmlWithUrl(){
require 'index.php';
$XmlClass = new XmlReaderClass("localhost", "8000", "status.xml", "");
$url= $XmlClass->$url;
$myUrl = "http://localhost:8000/status.xml?password=";
$this->assertEquals($myUrl, $url);
}} ?>
TestCase class cannot be added. I mean even PhpUnit name is not colored in blue in the code. So I guess I could not include it in my project actually. Error is like:
Fatal error: Uncaught Error: Class 'PHPUnit\Framework\TestCase' not found in /Users/demetsen/Desktop/tests/IndexTest.php:4
My second problem is when I try to run testGetXmlWithUrl() method the result is :
Failed asserting that SimpleXMLElement Object (...) matches expected '\n ....
Why am I getting this result and how can I solve it?
A default phpunit.xml (or better, use phpunit.xml.dist, so you can copy/edit it locally if needed), can be created with phpunit --generate-configuration will include a line for bootstrap:
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.3/phpunit.xsd"
bootstrap="path/to/bootstrap.php"
<!-- more lines -->
</phpunit>
Here, the bootstrap="...." line can, at its simplest, point to the composer autoloader. PHPunit can also create a suitable file for you.
$ php ./phpunit-9.2.phar --generate-configuration # or vendor/bin/phpunit, via composer
PHPUnit 9.2.6 by Sebastian Bergmann and contributors.
Generating phpunit.xml in /home/username/code/test
Bootstrap script (relative to path shown above; default: vendor/autoload.php):
Tests directory (relative to path shown above; default: tests):
Source directory (relative to path shown above; default: src):
Generated phpunit.xml in /home/username/code/test
# optional, but useful:
# mv phpunit.xml phpunit.xml.dist # .dist will be read if .xml does not exist
The output file:
$ cat phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.2/phpunit.xsd"
bootstrap="vendor/autoload.php"
executionOrder="depends,defects"
forceCoversAnnotation="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
verbose="true">
<testsuites>
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
If you had something you needed to add to the bootstrap process, create a file with your needs, and you'd probably also add something like require __DIR__.'/vendor/autoload.php'; to setup the autoloading.

Entity Class not found PHPUnit Test

Below are the paths where files are located,
src\TW\Talk\Entity\Talk.php
src\Tests\Talk\Entity\TalkTest.php
src\phpunit.xml.dist
In TalkTest.php, I have included PHPUnit and the entity Talk.
require_once 'TW/Talk/Entity/Talk.php';
require('PHPUnit/Autoload.php');
Class TalkTest extends PHPUnit_Framework_TestCase
{
...
}
In phpunit.xml.dist file, I have,
<phpunit>
<testsuites>
<testsuite name="TW">
<file>Tests/Talk/Entity/TalkTest.php</file>
</testsuite>
</testsuites>
</phpunit>
I am running phpunit command from src directory, I am getting error that Fatel Error: Class 'Tests\TW\Talk\Enity\Talk' not found.
For reference, I am referring to php-object-freezer-master which has similar structure.
Any idea why the TalkTest is not able to find Talk class ?
phpunit command is trying to find Talk entity in Tests folder.
Changing phpunit.xml.dist to
<phpunit bootstrap="loader.php">
<testsuites>
<testsuite name="TW_Talk">
<directory>Tests</directory>
</testsuite>
</testsuites>
</phpunit>
and loader file as,
<?php
function tw_test_autoloader($class) {
if(file_exists(__DIR__."\\" . $class . ".php"))
require_once(__DIR__."\\" . $class . ".php");
}
spl_autoload_register('tw_test_autoloader');
Worked for me.
But still if I replace directory tag to file
<file>Tests\TW\Talk\Entity\TalkTest.php</file>
It does not work.
Check your include_path:
echo get_include_path();
It should contain the directory to which your TW/Talk/Entity/Talk.php is relative. If it is not there, then you must add it either to php.ini or to PHPUnit's bootstrap.
You can easily test if PHP can find your file using your include path with this:
var_dump( stream_resolve_include_path('TW/Talk/Entity/Talk.php') );

'Class not found' when using namespaces in PHPUnit

I'm new to PHPUnit and am having some trouble setting it up to access my PHP files. The directory structure I'm using for my app is this:
./phpunit.xml
./lib/Application/
-> Dir1/File1.php (namespace = Application\Dir1)
-> Dir1/File2.php
-> Dir2/File1.php (namespace = Application\Dir2)
./tests/Application/Tests
-> Test1.php (namespace = Application\Tests)
-> Test2.php
In my PhpUnit.xml, I have:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit verbose="false">
<testsuites>
<testsuite name="Application">
<directory>./tests/Application/Tests</directory>
</testsuite>
</testsuites>
<logging>
<log type="coverage-text" target="php://stdout" showUncoveredFiles="false"/>
<log type="json" target="/tmp/phpunit-logfile.json"/>
</logging>
<filter>
<whitelist>
<directory suffix=".php">./lib</directory>
</whitelist>
</filter>
</phpunit>
And in one of my test files, I open with:
namespace Application\Tests;
use Application\Dir1\File1;
class MyTest extends File1 {}
But it keeps on saying:
Class 'Application\Dir1\File1' not found
Where am I going wrong?
If you installed PHPUnit using Composer then you can use Composers autoloader. The easiest way to do so would be to add:
"autoload":{
"psr-0":{
"your-app-directory":""
}
}
to composer.json
Even if you use use, you still have to include the file, either by using include, require, include_once, or require_once, or by using spl_autoload_register to include the file, like so:
spl_autoload_register(function ($class)
{
include '\lib\\' . $class . 'php';
});
When you then try to use Application\Dir1\File1 the script will automatically run include '\lib\Application\Dir1\File1.php'
I had the same issue.
I'm using composer as well and the only thing that solved it for me was the following:
add to your composer.json file in the autoload section a class map section with your root namespace
"autoload": {
"classmap": ["namespaceRoot/"]
}
execute composer dump-autoload command in order to recreate your autoload files (with all the class mappings!)
I found this really useful class autoloader by Jonathan Wage which allows PHPUnit tests to access namespaces from different directories. In my bootstrap.php, I just specified the location and associated module namespace:
require_once 'SplClassLoader.php';
$classLoader = new SplClassLoader('Application', dirname(__FILE__) . '/../lib');
$classLoader->register();

Setup PHPUnit with Zend Test

I'm trying to start using PHPUnit with Zend Test for my Zend Framework application. I'm able to run the PHPUnit command from command line phpunit --configuration phpunit.xml. I've tried following this tutorial which is based off of Matthew Weier O'Phinney's blog post. I'm getting an error when PHPUnit tries to write the log file. Here's my phpunit.xml
<phpunit bootstrap="./Bootstrap.php" colors="true">
<testsuite name="Zend Framework Tests">
<directory>./</directory>
</testsuite>
<!-- Optional filtering and logging settings -->
<filter>
<whitelist>
<directory suffix=".php">../library/</directory>
<directory suffix=".php">../application/</directory>
<exclude>
<directory suffix=".phtml">../application/</directory>
</exclude>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="./log/report" charset="UTF-8" yui="true" highlight="true" lowUpperBound="50" highLowerBound="80"/>
<log type="testdox-html" target="./log/testdox.html"/>
</logging>
</phpunit>
My testing bootstrap:
<?php
//Set app paths and environment
define('BASE_PATH', realpath(dirname(__FILE__) . '/../'));
define('APPLICATION_PATH', BASE_PATH . '/application');
define('TEST_PATH', BASE_PATH . '/tests');
define('APPLICATION_ENV', 'testing');
//Set include path
set_include_path('.' . PATH_SEPARATOR . BASE_PATH . '/library' . PATH_SEPARATOR . get_include_path());
//Set the default timezone
date_default_timezone_set('America/Chicago');
?>
And my ControllerTestCase that I would like my testing controllers to extend:
<?php
require_once 'Zend/Application.php';
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
abstract class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
public $_application;
public function setUp()
{
//Override the parent to solve an issue with not finding the correct module
$this->bootstrap = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
parent::setUp();
}
}
?>
The error I get when PHPUnit tries to write the log file is:
Fatal error: Class 'Symfony\Component\Console\Command\Command' not found in C:\repositories\myfirstzend.com\includes\library\Doctrine\DBAL\Tools\Console\Command\ImportCommand.php on line 38
Any clues on what I'm doing wrong? I'm on PHP 5.4, Windows 7, XAMPP 8.0, Pear is up to date, and I have the latest PHPUnit.
Update If I change my Bootstrap.php to the following from Matthew Weier O'Phinney's blog:
<?php
/*
* Start output buffering
*/
ob_start();
/*
* Set error reporting to the level to which code must comply.
*/
error_reporting( E_ALL | E_STRICT );
/*
* Set default timezone
*/
date_default_timezone_set('GMT');
/*
* Testing environment
*/
define('APPLICATION_ENV', 'testing');
/*
* Determine the root, library, tests, and models directories
*/
$root = realpath(dirname(__FILE__) . '/../');
$library = $root . '/library';
$tests = $root . '/tests';
$models = $root . '/application/models';
$controllers = $root . '/application/controllers';
/*
* Prepend the library/, tests/, and models/ directories to the
* include_path. This allows the tests to run out of the box.
*/
$path = array(
$models,
$library,
$tests,
get_include_path()
);
set_include_path(implode(PATH_SEPARATOR, $path));
/**
* Register autoloader
*/
require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();
/**
* Store application root in registry
*/
Zend_Registry::set('testRoot', $root);
Zend_Registry::set('testBootstrap', $root . '/application/bootstrap.php');
/*
* Unset global variables that are no longer needed.
*/
unset($root, $library, $models, $controllers, $tests, $path);
I continue to get the error about Symfony from Doctrine. I ensured that I installed pear.symfony.com/Yaml as well. So still broken.
If I remove the Doctrine reference from my app.ini for the application I'm testing (which means it doesn't get loaded), I still get the error. It almost feels like the loaders for each of the three parts (PHPUnit, ZF, Doctrine) are fighting each other. Is there a way around this?
Second update: I downgraded PHPUnit to 3.4.15 and I'm still having this issue. My next step is to go from PHP 5.4 to 5.3.x.
Third update: I am now on PHP 5.3.10 and am seeing the same error.
If there's more information you need, please let me know.
I'm missing the loading of the application in your bootstrap.
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();
To give you an idea about what I have in my tests/bootstrap.php (this is auto-generated by zend tool since release-1.11.4)
<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'testing'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();
As mentioned by the Zend Framework manual, best is to use PHPUnit 3.4.15 although I could run my tests using PHPUnit 3.6.12 as I'm isolating my tests to focus on my business logic and not the logic of Zend Framework.
I also modified my phpunit.xml as well into the following:
<phpunit bootstrap="./bootstrap.php" colors="true">
<testsuite name="Application Test Suite">
<directory>./application</directory>
</testsuite>
<testsuite name="Library Test Suite">
<directory>./library</directory>
</testsuite>
<filter>
<whitelist>
<directory suffix=".php">../../library</directory>
<directory suffix=".php">../../application</directory>
<exclude>
<directory suffix=".php">../../library/Zend</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
I hope this solves many of your issues you're facing now.
Best regards,
Michelangelo

Zend Framework UnitTest

I tried to use Zend_Test_PHPUnit to write unit tests for my application, but I always just get
1) IndexControllerTest::testValidation
Failed asserting last controller used <"error"> was "test"
I have created a test controller but even there I cannot get it to work.
Can anyone help?
Thanks!
class TestController extends Zend_Controller_Action
{
public function indexAction()
{
print 'test';
}
}
Bootstrap is:
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'testing'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();
phpunit.xml is:
<phpunit bootstrap="./bootstrap.php">
<testsuite name="Application Test Suite">
<directory>./application</directory>
</testsuite>
<testsuite name="Library Test Suite">
<directory>./library</directory>
</testsuite>
<filter>
<whitelist>
<directory suffix=".php">../library/</directory>
<directory suffix=".php">../application/</directory>
</whitelist>
</filter>
</phpunit>
Test controller is
class IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{
public function setUp()
{
$this->bootstrap = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
parent::setUp();
}
public function testValidation()
{
$this->dispatch('/test/');
$this->assertController("test");
}
}
Looks like an error in your TestController.
Is your View available (/test/index.phtml)?
Good solution would be to check the thrown Exception (wrap the unit test in an Try/Catch Block and print to error log).
I was getting the exact same error, but due to a completely different problem.
A more general solution might be to add this into the test method
print_r(strip_tags(
$this->bootstrap
->getBootstrap()
->getResource('layout')
->content
)); die;
This is assuming that Zend_Layout is being used... It prints out the contents of the error.phtml file, so at least you can see exactly what is going on.
You should see something like:
An error occurred
Application error
Exception information:
Message: {Your Error Message will appear here}
Stack trace:
{A stack trace will follow...}
With your specific error message appearing after "Message:" and then a full stack trace after "Stack Trace:".
Hopefully this will at least help to debug the underlying issue

Categories