Skip to content

Instantly share code, notes, and snippets.

@freddiefujiwara
Created February 25, 2012 17:17
Show Gist options
  • Select an option

  • Save freddiefujiwara/1909586 to your computer and use it in GitHub Desktop.

Select an option

Save freddiefujiwara/1909586 to your computer and use it in GitHub Desktop.
JavaScript Test using PHPUnit
$ cat Cat.js
var Cat;
Cat = (function() {
function Cat(name) {
this.name = name;
}
Cat.prototype.mew = function() {
return "I am " + this.name + " mew mew..";
};
Cat.prototype.changeName = function(name) {
this.name = name;
};
return Cat;
})();
$ cat CatTest.php
<?php
/**
* Test class for Cat.
*/
class CatTest extends PHPUnit_Framework_TestCase
{
/**
* @var v8(JS Engine)
*/
protected $v8;
protected function setUp(){
$this -> v8 = new V8Js();
$this -> v8 -> executeString(file_get_contents('Cat.js'));
}
public function testMew(){
$this -> v8 -> executeString("var cat = new Cat('tama');");
$this -> assertEquals("I am tama mew mew..",$this -> v8 -> executeString("cat.mew()"));
}
public function testChangeName(){
$this -> v8 -> executeString("var cat = new Cat('tama');");
$this -> v8 -> executeString("cat.changeName('pochi');");
$this -> assertEquals("I am pochi mew mew..",$this -> v8 -> executeString("cat.mew()"));
}
}
$ phpunit CatTest.php
PHPUnit 3.6.10 by Sebastian Bergmann.
..
Time: 0 seconds, Memory: 3.25Mb
OK (2 tests, 2 assertions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment