I should really read the PHPUnit Manual…
Tag Archives: php
PHP Gettext
TODO: learn how to integrate Gettext in my PHP apps.
PHP spl_autoload_register
Today I used spl_autoload_register again.
This time to load default concrete classes. For example in my framework I have a class called SlibUser:
class SlibUser extends SlibRecord { public static function Load( $id ) { $args = array( 'id' => $id ); $data = db()->get_row( 'select * from /*prefix*/user where id = :id', $args ); if ( $data ) { return new User( $data ); } return null; } }
As you can see SlibUser creates a new User. That User can be provisioned by a user of my framework, but if they don’t specify a particular User definition I give them a default implementation with spl_autoload_register, e.g.:
<?php function load_slib_mock( $class = false ) { //var_dump( $class ); static $classes; static $files; if ( ! $files ) { $files = array(); } if ( ! $classes ) { $classes = array( // URL 'Uri', 'UriPath', 'UriPathBuilder', 'UriQuery', 'UriQueryBuilder', 'UriQueryNode', // framework: 'Auth', 'Column', 'FrontController', 'Option', 'OptionGroup', 'ResourceManager', 'Schema', 'Settings', 'Table', 'Variable', // database: 'Database', 'DatabaseUpgrader', // ORM: 'Browser', 'GeoipLocation', 'HttpAccept', 'HttpAcceptEncoding', 'HttpAcceptLanguage', 'HttpMethod', 'HttpUserAgent', 'Partition', 'Request', 'Role', 'Session', 'SessionSettings', 'User', 'UserPartition', 'UserSettings' ); } if ( ! in_array( $class, $classes ) ) { return false; } $path = "/var/tmp/slib-$class.php"; if ( ! file_exists( $path ) ) { $content = "<?php class $class extends Slib$class {}"; $file = fopen( $path, 'w' ); if ( ! $file ) { throw new IOException( "Cannot write '$file'." ); } fwrite( $file, $content ); fclose( $file ); } require_once( $path ); return true; } // autoload handler: spl_autoload_register( 'load_slib_mock', true, true );
Debugging PHPUnit Tests in NetBeans with XDebug
Today I read Debugging PHPUnit Tests in NetBeans with XDebug and got PHPUnit tests running in the NetBeans debugger.
AngularJS and CURL and pcntl_fork, oh my!
Reading about angularjs. And the curl library. And pcntl_fork.
PHPUnit Skeleton Generator
Today I installed PHPUnit Skeleton Generator like this:
$ vim ~/.composer/composer.json Added: "minimum-stability": "dev" $ composer global require 'phpunit/phpunit-skeleton-generator=*'
But… then it turned out that the interface was broken when invoking from NetBeans, so over here:
$ pear channel-discover components.ez.no $ pear install phpunit/PHPUnit_SkeletonGenerator
Then I created a phpunit-skelgen.php file in my project and added:
#!/usr/bin/php <?php require_once '/usr/share/php/SebastianBergmann/PHPUnit/SkeletonGenerator/autoload.php'; require_once '/usr/share/php/SebastianBergmann/PHPUnit/SkeletonGenerator/Command.php'; SebastianBergmann\PHPUnit\SkeletonGenerator\Command::main();
Simple! :)
PDO Persistent Connection in PHP left broken if connected to dropped database
So you use PDO and specify PDO::ATTR_PERSISTENT => true. Then you drop your database. Then you open a new persistent connection and bang! Not working. The trick is to not use persistent connections to databases that may be dropped. And probably an ‘apache2ctl graceful’ after you drop a database being used by Apache…
Debugging PHPUnit tests in Eclipse PDT with XDebug on Debian GNU/Linux
Wow, this was complicated!
Make sure PHP, Xdebug, etc. are installed:
# apt-get install php php5-xdebug php5-curl php5-mysql php5-gd
Install Eclipse:
# apt-get install eclipse
To install PHP Developer Tools in Eclipse: Open Eclipse, click Help -> Install New Software…
Work with: http://download.eclipse.org/tools/pdt/updates/release
Select PHP Development Tools / PHP Development Tools (PDT) and install.
Create a new PHP project in Eclipse.
Download PHPUnit into your project folder, e.g.:
$ cd ~/workspace/new-project $ wget https://phar.phpunit.de/phpunit.phar $ chmod +x phpunit.phar $ cp phpunit.phar /usr/local/bin/phpunit
In your Eclipse PHP project right-click on your project and select “Include Path” -> “Configure Include Path”.
Click “Libraries” -> “Add External PHARs” then add “phpunit.phar” (in your project’s workspace).
In Eclipse click “Run” -> “Debug Configurations”. Click “PHP CLI Application” and then “New”. Enter the PHP Script name as “PHPUnit” with Project default PHP: PHP CLI (Xdebug 5.4.4 CLI). Set the PHP file as “/project-name/phpunit.phar”.
Edit your php.ini file, e.g.:
# vim /etc/php5/cli/php.ini
And make sure to specify an xdebug configuration (append to end of file is OK):
[xdebug] zend_extension=/usr/lib/php5/20100525/xdebug.so xdebug.remote_enable=On ;xdebug.remote_host="localhost" xdebug.remote_host=localhost ;xdebug.remote_port=9999 ;xdebug.remote_port=9000 xdebug.remote_port=9999 xdebug.remote_handler="dbgp" xdebug.profiler_enable=1 xdebug.profiler_output_dir="/var/tmp" xdebug.default_enable = on xdebug.remote_autostart=on xdebug.remote_mode = "req" xdebug.remote_connect_back = on xdebug.remove_log = /tmp/xdebug.log
Make sure you have a phpunit.xml file next to phpunit.phar in your workspace, e.g.:
<?xml version="1.0" encoding="UTF-8"?> <phpunit backupGlobals="false" backupStaticAttributes="false" syntaxCheck="false"> <testsuites> <testsuite name="Tests"> <directory suffix="Test.php">test</directory> </testsuite> </testsuites> </phpunit>
Make sure there is one test in your test directory, e.g. /project-name/test/MyTest.php
<?php class MyTest extends PHPUnit_Framework_TestCase { public function setUp() { //require_once( __DIR__ . '/../src/example.php' ); } public function testExample() { $this->assertSame( '1', '1' ); } }
Double-click in the sidebar next to $this->assertSame to put a breakpoint there.
Then in Eclipse on the toolbar at the top is a little ‘bug’ icon, click ‘down’ next to that and debug ‘PHPUnit’.
Your unit tests should run in PHPUnit and break into the PHP debugger in Eclipse.
Did that work for you? Let me know!
OWASP ESAPI PHP tests: Security configuration file does not exist
To get the source for OWASP ESAPI PHP:
svn checkout http://owasp-esapi-php.googlecode.com/svn/trunk/ owasp-esapi-php-read-only
Make sure phpunit is installed with PEAR. To run the unit tests:
root@mercy:/home/jj5/Desktop/owasp-esapi-php-read-only# phpunit test Security configuration file does not exist.PHP Fatal error: Call to a member function xpath() on a non-object in /home/jj5/Desktop/owasp-esapi-php-read-only/src/reference/DefaultSecurityConfiguration.php on line 226
To get a better error message:
root@mercy:/home/jj5/Desktop/owasp-esapi-php-read-only# grep -R 'Security conf' . ./src/reference/DefaultSecurityConfiguration.php: throw new Exception("Security configuration file does not exist.");
Edit ‘src/refererence/DefaultSecurityConfiguration.php’ and replace “Security configuration file does not exist.” with “Security configuration file ‘$path’ does not exist.”
Try again:
root@mercy:/home/jj5/Desktop/owasp-esapi-php-read-only# phpunit test Security configuration file '/home/jj5/Desktop/owasp-esapi-php-read-only/test/filters/../../testresources/ESAPI.xml' does not exist.PHP Fatal error: Call to a member function xpath() on a non-object in /home/jj5/Desktop/owasp-esapi-php-read-only/src/reference/DefaultSecurityConfiguration.php on line 226
So the problem is a misconfigured path to the ESAPI.xml file,
root@mercy:/home/jj5/Desktop/owasp-esapi-php-read-only# grep -R \\.\\.\\/testres . ./test/filters/SafeRequestTest.php: $ESAPI = new ESAPI(dirname(__FILE__) . '/../../testresources/ESAPI.xml'); ...
Edit the SafeRequestTest.php file:
root@mercy:/home/jj5/Desktop/owasp-esapi-php-read-only# vim test/filters/SafeRequestTest.php
On line 58 change “/../../testresources” to “/../testresources”.
Now our tests will run:
root@mercy:/home/jj5/Desktop/owasp-esapi-php-read-only# phpunit test
Salted Password Hashing – Doing it Right
Today I read Salted Password Hashing – Doing it Right which was a good run-down on how to do password hashing. The article linked to phpass: Portable PHP password hashing framework which is a library for hashing passwords.