PageRenderTime 50ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/java/net/collegeman/phpinjava/PHPInJavaTest.java

http://github.com/collegeman/php-in-java
Java | 90 lines | 66 code | 23 blank | 1 comment | 0 complexity | e40e1f3dae56d1aedbd3c6e24e8d29d0 MD5 | raw file
Possible License(s): GPL-2.0
  1. package net.collegeman.phpinjava;
  2. import junit.framework.*;
  3. import com.caucho.quercus.env.*;
  4. import java.util.*;
  5. import java.io.*;
  6. import net.collegeman.phpinjava.groovy.*;
  7. public class PHPInJavaTest extends TestCase {
  8. public void testExecuteFromClasspath() {
  9. assertEquals("Hello, world!", new PHP("classpath:/com/faux/php/HelloWorld.php").toString());
  10. assertEquals("Hello, world!", new GroovyPHP("classpath:/com/faux/php/HelloWorld.php").toString());
  11. }
  12. public void testExecuteSnippet() {
  13. assertEquals("Hello, world!", new PHP().snippet("<?php echo 'Hello, world!';").toString());
  14. assertEquals("Hello, world!", new GroovyPHP().snippet("<?php echo 'Hello, world!';").toString());
  15. }
  16. public void testExecuteSnippetDefiningFunction() {
  17. PHP php = new PHP().snippet("<?php function just_return_it($message) { return $message; }");
  18. assertEquals("Hello, world!", php.fx("just_return_it", "Hello, world!").toString());
  19. GroovyPHP gphp = new GroovyPHP().snippet("<?php function just_return_it($message) { return $message; }");
  20. assertEquals("Hello, world!", gphp.fx("just_return_it", "Hello, world!").toString());
  21. }
  22. public void testExecuteGlobalFunction() {
  23. PHP php = new PHP("classpath:/com/faux/php/HelloWorldFx.php");
  24. assertEquals("Hello, world!", php.fx("repeat", "Hello, world!").toString());
  25. GroovyPHP gphp = new GroovyPHP("classpath:/com/faux/php/HelloWorldFx.php");
  26. assertEquals("Hello, world!", gphp.fx("repeat", "Hello, world!").toString());
  27. }
  28. public void testExecuteInstanceMethod() {
  29. assertEquals("Hello, world!", new PHP("classpath:/com/faux/php/HelloWorldClass.php")
  30. .newInstance("Repeater", "Hello, world!")
  31. .invokeMethod("repeat").toString());
  32. assertEquals("Hello, world!", new GroovyPHP("classpath:/com/faux/php/HelloWorldClass.php")
  33. .newInstance("Repeater", "Hello, world!")
  34. .invokeMethod("repeat", null).toString());
  35. }
  36. public void testExecuteInstanceMutator() {
  37. PHPObject repeater = new PHP("classpath:/com/faux/php/HelloWorldClass.php").newInstance("Repeater");
  38. repeater.invokeMethod("setMessage", "Foo bar!");
  39. assertEquals("Foo bar!", repeater.invokeMethod("repeat").toString());
  40. GroovyPHPObject grepeater = new GroovyPHP("classpath:/com/faux/php/HelloWorldClass.php").newInstance("Repeater");
  41. grepeater.invokeMethod("setMessage", "Foo bar!");
  42. assertEquals("Foo bar!", grepeater.invokeMethod("repeat", null).toString());
  43. }
  44. public void testExecutePropertyAccess() {
  45. PHPObject repeater = new PHP("classpath:/com/faux/php/HelloWorldClass.php").newInstance("Repeater");
  46. repeater.setProperty("message", "Up, up, and away!");
  47. assertEquals("Up, up, and away!", repeater.getProperty("message").toString());
  48. GroovyPHPObject grepeater = new GroovyPHP("classpath:/com/faux/php/HelloWorldClass.php").newInstance("Repeater");
  49. grepeater.setProperty("message", "Up, up, and away!");
  50. assertEquals("Up, up, and away!", grepeater.getProperty("message").toString());
  51. }
  52. public void testGlobalReadAndWrite() {
  53. PHP php = new PHP();
  54. php.set("helloWorld", "Hello, world!");
  55. assertEquals("Hello, world!", php.get("helloWorld").toString());
  56. GroovyPHP gphp = new GroovyPHP();
  57. gphp.set("helloWorld", "Hello, world!");
  58. assertEquals("Hello, world!", gphp.get("helloWorld").toString());
  59. }
  60. public void testExecuteFile() {
  61. // hackish, but a file we know to exist...
  62. String path = getClass().getClassLoader().getResource("com/faux/php/HelloWorld.php").getPath();
  63. assertEquals("Hello, world!", new PHP(path).toString());
  64. assertEquals("Hello, world!", new PHP(new File(path)).toString());
  65. assertEquals("Hello, world!", new GroovyPHP(new File(path)).toString());
  66. }
  67. }