Τετάρτη 31 Οκτωβρίου 2012

Unit Testing - Installing PHPUnit

The primary goal of unit testing is to take the smallest piece of testable software in the application, isolate it from the remainder of the code, and determine whether it behaves exactly as you expect. Each unit is tested separately before integrating them into modules to test the interfaces between modules. Unit testing has proven its value in that a large percentage of defects are identified during its use.

PHPUnit is a unit testing software framework for PHP, as you may guess.

At first I thought installation was something straight forward, but it was not. I had to install PEAR first, and problems appeared from the beginning.I don't want to discourage you, instead I am telling you this just to encourage you not to give up.

During the installation process, I was wondering, what I was doing wrong, so I kept googling and here I will present you the steps needed to install PEAR and PHPUnit error free on WAMP, according to the problems I have faced and the research I have done.

1) Install PEAR

1st step
  • First open php.ini file located at C:\wamp\bin\php\php5.3.0\php.ini 
  • Find the following line ;phar.require_hash = On
  • Uncomment by removing the semi-colon
  • Change 'On' to 'Off'
  • Save the file
 
2nd step 
  • Locate your php folder by writing cd C:\wamp\bin\php\php5.3.0 *** For those who don't know this, by typing cd .. in a command line,  you are being moved one level up in the folder structure
  • On command prompt type go-pear.bat
  • Confirm "system" and hit enter a few times
  • double click PEAR_ENV.reg found at your php folder
  • edit php.ini again to add pear include path.  Search for go-pear and you will see
  ;***** Added by go-pear include_path=".;C:\wamp\bin\php\php5.3.0\pear" ;*****
  • Copy these lines and paste them to "php.ini" file located at C:\wamp\bin\apache\apache2.2.11\bin 
  • Restart the apache
  • Type pear upgrade pear and you have PEAR installed on you machine

3rd step

We are almost done here with PEAR installation. In order to use ‘pear’ command in your command prompt from anywhere, you will need to modify the windows environment variables.

  • Right click on "My computer" choose Properties -> Advanced > Environmental variables
  • Edit "Path" variable and add  C:\wamp\bin\php\php5.3.0
      ***Be careful here, by mistake I have left a space character at the end, and coudn't find out for half an hour, what was I was doing wrong
  • You also need to add a new variable ‘PHP_PEAR_PHP_BIN’ and set the value to
    C:\wamp\bin\php\php5.3.0\php.exe  
  • Close the command prompt, open it again and type pear
  • You will see pear command help printed for you




2) Upgrade PEAR

In order to continue to the PHPUnit installation, you first have to upgrade PEAR

  • Just type pear upgrade pear 
  • Type pear -V, to see the version installed



3) Install PHPUnit

Now we can continue with the PHPUnit installation
  • On ‘C’ drive create a folder named ‘php5’ (I don't know why and I don't know if this is necessasy, but I did it anyway
  • On command prompt type following, but first be sure that you have upgraded PEAR
pear channel-discover components.ez.no
pear channel-discover pear.phpunit.de
pear channel-discover pear.symfony-project.com
pear install --alldeps phpunit/PHPUnit
  • We’re done! type phpunit –version to confirm installation.
 
4) Solutions to problems you may face
 
If you face a problem here, you can type  

  • pear update-channels 
    
    
You can also clear cache by typing
  • pear clear-cache  
      
If you want to unistall PEAR and do the above steps again type
  • pear uninstall pear

If you want to unistall PHPUnit type
  • pear uninstall phpunit/PHPUnit
      
If you have a problem like this

Duplicate package channel://components.ez.no/ConsoleTools-1.5.1 found
Duplicate package channel://components.ez.no/ConsoleTools-1.5.2 found


or a duplicate package to another channel like
Duplicate channel://pear.phpunit.de/Whatever1.1
Duplicate channel://pear.phpunit.de/Whatever1.2 

you can type

pear uninstall pear.phpunit.de/Whatever1.2
pear install pear.phpunit.de/Whatever1.1

That last thing is what made the trick for me! I don't remember exactly which version I have installed and which I have uninstalled for the duplicate packages, but the point is that I read error message and figured out what I have to install or uninstall manually!


Useful links 
 
http://docs.moodle.org/dev/PHPUnit_installation_in_Windows
http://amiworks.co.in/talk/installing-pear-and-phpunit-on-wamp-and-windows-7/ 
http://www.karakas-online.de/forum/viewtopic.php?t=10459
http://stackoverflow.com/questions/2006626/installing-phpunit-via-pear
http://share.ez.no/forums/install-configuration/noob-having-trouble-installing-pear-and-ezcomponents 
 
 

Τρίτη 23 Οκτωβρίου 2012

Fatal error: Call to undefined function pg_connect()

Problem is that when you´re using a PHP version above 5.2.5 in windows, you just can´t load the pgsql extension.

You have to install php 5.2.5 or an older version, or just install a newer one and overwrite php_pgsql.dll with the one from PHP 5.2.5.

Κυριακή 7 Οκτωβρίου 2012

Localhost redirects to online wordpress site and vice versa - Multisite

If you have installed multisite for wordpress and your localhost installation redirects you to your online version, then you have to check following tables using phpMyadmin:
  1. wp_options
    • siteurl
    • home
  2. wp_2_options
    • siteurl
    • home
  3. wp_site -> change domain to localhost and path to your path

Τρίτη 4 Σεπτεμβρίου 2012

Animate to a specific div using jquery

I am really impressed about sites tha have a fixed menu and when user clicks on it, whole page animates to a specific div.

Have a look at this wonderful site, to see what I mean (click onto the image)
















Really impressive, ha! Are you still here? Well for those you came back to learn this technique, it's time to realize how simple it is.

We just use jquery's animate method, and all magic happens to the site. Really cool! But to be more specific, you can read the following function:

Let me just explain it a little bit.

This function takes the id of the targeting div as parameter  (we will explain how we call it later).

First of all we define the animation speed and the easing type. So, you have to dowload Jquey Easing Plugin first.

After defining the variables, we just check if it's a webkit browser (Google Chrome), because webkit browsers do not support animate-html.

We then call the animate method (be careful!! before calling animate(), we call stop() ), and that's all.


The HTML
Just put an anchor tag on your page and on click, call the Animate2id('#news') function, which animates to a div with id=news.

Πέμπτη 30 Αυγούστου 2012

How to center a div inside a div


If you want to center a div inside another width and the content of the inner div is dynamic (so you don't know the exact width), you can do something like this:

Τετάρτη 29 Αυγούστου 2012

Put an image on the Canvas

To draw an image on a canvas, we use the drawImage(image,x,y) method

HTML5 Canvas for beginners

The canvas element is used to draw graphics, on the fly, on a web page. It is only a container for graphics. We must use a script to actually draw the graphics.

***Internet Explorer 8 and earlier versions do not support the canvas element.

How can we create the canvas element

In order to create a canvas element, use the following markup:


As you can see above, I used an id attribute in order to referrence it later using javascript.If you want to style your canvas you can use the style attribute.

 
How can we draw onto the canvas using javascript 

As I mentioned earlier, after we create the canvas, we use javascript in order to draw something onto it. For example if we want to draw a red rectangle we use following markup, inside a script javascript tag.

I will briefly explain the code. First we find the canvas element with the id attribute, and then we call its getContext() method, passing the string "2d" as an arguement.

***The getContext("2d") object is a built-in HTML5 object, with many properties and methods for drawing paths, boxes, circles, text, images, and more. 

At last we set the red color and draw the rectangle.














Read more examples

*** For more examples visit http://www.w3schools.com/html5/html5_canvas.asp