notboring dev blog

flex, flash, javascript, videos, games, iphone


Automated unit testing for code igniter 2.x/3.x

Here’s a quick way to setup unit testing for CI 2.x/3.x:

Step 1: Add a class for testing into your application/controllers folder:

< ?php
class Frontend_Tests_Controller extends Frontend_Controller{
    function __construct(){
        parent::__construct();
    }
 
    public function index(){
        $this->load->library('unit_test');
 
        $this->unit->run( $this->test_reflection(5), 5, "Testing testing" );
 
        echo $this->unit->report();
    }
 
    protected function test_reflection($value){
        return $value;
    }
}

In this file you can add all your tests and then call them from the index function.

Step 2: Add auto loading code for your new testing class to application/config/config.php:

function __autoload($class){
    if (file_exists(APPPATH."controllers/".strtolower($class).".php")){
        require_once(APPPATH.'controllers/'.strtolower($class).".php");
    }
}

Step 3: Add the route to application/config/routes.php

$route['tests'] = "Frontend_Tests_Controller/index";

Now you can call /tests to run the tests.

No Comments

xampp mailtodisk application problem: no emails are being saved, the tool seems broken.

To “fix” the issue, please check the content of your xampp/apache folder. mailtodisk ingores the path settings and write the logs in to a subfolder of the webserver. That means you gonna find your emails in xampp/apache/mailoutput.

No Comments

Deleting all files from a folder that reached a certain age (x hours/days/weeks/months/years) using PHP

Here a quick example of how to delete all files of a folder that are olden the x days/minutes/hours/months. This method works under both windows and unix. The custom getCorrectMTime() function was posted on the PHP documentation and takes into account that time measurement under windows and unix is slightly different. Of course I also have a small ignore list where files (eg .htaccess, index.html etc) can be blocked from getting deleted.

/**
     * @param $folder the folder in which the old files are supposed to get purged
     * @param $maximumAge maximum file age in seconds
     * @param $ignoreList array() of filenames that should get ignored
     */
    public function deleteOldFiles($folder, $maximumAge,$ignoreList){
        $dir=scandir($folder);
        foreach($dir as $file){
            $secretFileTime=$this->getCorrectMTime($folder.$file);
            $fileAge=time()-$secretFileTime;
            if(($file!='..')&&($file!='.')&&($file!='index.html')&&(!in_array($file,$ignoreList))){
                if($fileAge>$maximumAge){
                    unlink($folder.'/'.$file);
                    //echo "<br />deleting ".$folder.$file." because it has the age: ".$fileAge;
                }else{
                    //echo "<br />keeping ".$folder.$file." because it has the age: ".$fileAge;
                }
            }else{
                //echo "<br />ignored ".$file;
            }
        }
    }
 
public function getCorrectMTime($filePath){
 
        $time = filemtime($filePath);
 
        $isDST = (date('I', $time) == 1);
        $systemDST = (date('I') == 1);
 
        $adjustment = 0;
 
        if($isDST == false && $systemDST == true)
            $adjustment = 3600;
 
        else if($isDST == true && $systemDST == false)
            $adjustment = -3600;
 
        else
            $adjustment = 0;
 
        return ($time + $adjustment);
    }
No Comments

Fixing the Mochi “Download the latest flash player” bug

I decided to update my Mochi titles to the latest version of the framework: 3.9.5. The compiled files looked nice and worked without flaws. But after uploading them to Mochi, enabling the live updater, things broke.

All I could see was a “Download the latest flash player” message right after the Ad. The game was not accessible anymore.

When checking the Flash export settings in noticed that Flash CS6 seem to have set the plugin version number for the project to the latest version istead of the previous version 9.

Badly enough switching back to version 9 didn’t help, but after a few tries I found out that version 10.3. works perfectly.

No Comments

Simple to use Windows SMTP test server

If you are developing on a windows server using xampp or a similar webserver package you might run into issues with testing functions that reply on emails. That’s because windows got no built in SMTP server.

But there are simple SMTP servers aiming at developers only. and one of them is the antix smtp server:

Since the official antix smtp server is offline and alternative downloads that I checked contained a trojan, here a list of alternatives:

http://smtp4dev.codeplex.com/
https://github.com/yankee42/developmentSMTP

No Comments

xampp: Making CURL available for direct (php.exe/php cli) execution

Open your xampp folder (eg. c:\xampp) and go into the php subfolder (eg. c:\xampp\php). Open the file “php.ini” in a text editor and enable CURL line by replacing the outcommented line

;extension=php_curl.dll

by

extension=php_curl.dll

This enables the CURL extension for both HTTP and the php command line executable (php.exe/php).

No Comments

How to access wordpress functionality using the command line (CLI)

WordPress does not offer any type of command line (cli) support to autmate tasks without using the HTTP protocol. You can easily add it though.

Full fledged wordpress administration using the command line (cli)

wp-cli, a set of tools to manage wordpress from the CLI. It adds access to some of the word press functionality and can be easily extended to run your own code from the command line.

Find more informations about this usefull word press extension at:

http://wp-cli.org/

Directly calling WordPress PHP functions from the command line (cli)

If you just need to run a php script to add own functionality, you can use the following template to initalize wordpress correctly:

set_time_limit(0);
ini_set( "memory_limit", "64M" );
// set this to the path of your file. In my example the php 
// file is in a subfolder, thus there's a ".." to tell PHP it 
// needs to access the parent directory
require_once(dirname(__FILE__) . '/../wp-load.php');

That’s it. Now you can use wordpres functionality like you would do it in plugins. Here a bigger example:

set_time_limit(0);
ini_set( "memory_limit", "64M" );
// set this to the path of your file. In my example the php 
// file is in a subfolder, thus there's a ".." to tell PHP it 
// needs to access the parent directory
require_once(dirname(__FILE__) . '/../wp-load.php');

// now let's access the wpdb database
global $wpdb;
$q = $wpdb->prepare ("SELECT * FROM nb_cntv_video_files WHERE filename_base=%s",$filenameBase);
$results = $wpdb->get_results ($q);
echo "found " . count ($results) . "\n";
No Comments

GIT: Commit fail with exit code 128 (unable to auto-detect email address)

A typical error happening on windows system. To fix it, open the GIT shell and enter the following two lines:

git config --global user.email "you@domain.com"
git config --global user.name "Your Name"

Source:
http://code.google.com/p/tortoisegit/issues/detail?id=1258

No Comments

Apache/XAMPP http.exe using 100% of the cpu power of a PC

This can be caused by a too big log file. Go to

[your xampp folder]/apache/logs

Delete all files bigger then a few megabytes.

No Comments

No such posts after moving from qTranslate to WPML using the WPML importer

A quick check of the database showed that the WPML importer didn’t do a good job. But the WPML devs seems to have known that, so offer a function to clean up the mess.

Open the wordpress admin menu and go to “WPML -> Troubleshooting”. There you find the “Clean up” section. Click on general clean up and the posts should be back to being visible.

No Comments
Rss Feeds