PageRenderTime 70ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/jelix-tests/classes/junittestcase.class.php

https://github.com/gmarrot/jelix
PHP | 236 lines | 147 code | 17 blank | 72 comment | 19 complexity | 3be6e419838cc2983cdc86e495d6bba1 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * @package jelix
  4. * @subpackage jelix-tests
  5. * @author Laurent Jouanneau
  6. * @contributor Christophe Thiriot
  7. * @copyright 2006-2012 Laurent Jouanneau
  8. * @link http://www.jelix.org
  9. * @licence GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
  10. */
  11. require (JELIX_LIB_CORE_PATH.'request/jClassicRequest.class.php');
  12. class jCoordinatorForTest extends jCoordinator {
  13. function testSetRequest($request) {
  14. $this->setRequest($request);
  15. }
  16. }
  17. class jUnitTestCase extends PHPUnit_Framework_TestCase {
  18. /**
  19. * indicates if PDO is needed. If yes, PDO will be checked
  20. * and if not present, tests will be skipped
  21. * @var boolean
  22. */
  23. protected $needPDO = false;
  24. /**
  25. * profile name for jDb
  26. * @var string
  27. */
  28. protected $dbProfile ='';
  29. protected function setUp() {
  30. parent::setUp();
  31. if($this->needPDO && false === class_exists('PDO',false)){
  32. $this->markTestSkipped('PDO does not exists ! You should install PDO because tests need it.');
  33. }
  34. }
  35. /**
  36. * init jelix configuration.
  37. *
  38. * If you need to setup a full jelix environment with a coordinator,
  39. * prefer to call initClassicRequest
  40. * @param string $config the configuration file to use, as if you were inside an entry point
  41. * @param string $entryPoint the entrypoint name as indicated into project.xml
  42. */
  43. protected static function initJelixConfig($config = 'index/config.ini.php', $entryPoint = 'index.php') {
  44. $config = jConfigCompiler::read($config, true, true, $entryPoint);
  45. jApp::setConfig($config);
  46. jApp::setCoord(null);
  47. }
  48. /**
  49. * @var \jelix\FakeServerConf\ApacheMod
  50. */
  51. protected static $fakeServer = null;
  52. /**
  53. * initialize a full jelix environment with a coordinator, a request object etc.
  54. *
  55. * it initializes a coordinator, a classic request object. It sets jApp::coord(),
  56. * @param string $url the full requested URL (with http://, the domaine name etc.)
  57. * @param string $config the configuration file to use, as if you were inside an entry point
  58. * @param string $entryPoint the entrypoint name as indicated into project.xml
  59. */
  60. protected static function initClassicRequest($url, $config = 'index/config.ini.php', $entryPoint = 'index.php') {
  61. self::$fakeServer = new jelix\FakeServerConf\ApacheMod(jApp::wwwPath(), '/'.$entryPoint);
  62. self::$fakeServer->setHttpRequest($url);
  63. $config = jConfigCompiler::read($config, true, false, $entryPoint);
  64. $coord = new jCoordinatorForTest($config, false);
  65. jApp::setCoord($coord);
  66. $request = new jClassicRequest();
  67. $coord->testSetRequest($request);
  68. }
  69. /**
  70. * compatibility with simpletests
  71. */
  72. public function assertEqualOrDiff($first, $second, $message = "%s"){
  73. return $this->assertEquals($first, $second, $message);
  74. }
  75. // complex equality
  76. public function assertComplexIdentical($value, $file, $errormessage=''){
  77. $xml = simplexml_load_file($file);
  78. if(!$xml){
  79. trigger_error('Unable to load file '.$file,E_USER_ERROR);
  80. return false;
  81. }
  82. return $this->_checkIdentical($xml, $value, '$value', $errormessage);
  83. }
  84. public function assertComplexIdenticalStr($value, $string, $errormessage=''){
  85. $xml = simplexml_load_string($string);
  86. if(!$xml){
  87. trigger_error('Wrong xml content '.$string,E_USER_ERROR);
  88. return false;
  89. }
  90. if($errormessage != '')
  91. $errormessage = ' ('.$errormessage.')';
  92. return $this->_checkIdentical($xml, $value, '$value', $errormessage);
  93. }
  94. /*
  95. <object class="jDaoMethod">
  96. <string property="name" value="" />
  97. <string property="type" value="" />
  98. <string property="distinct" value="" />
  99. <object method="getConditions()" class="jDaoConditions">
  100. <array property="order">array()</array>
  101. <array property="fields">array()</array>
  102. <object property="condition" class="jDaoCondition">
  103. <null property="parent"/>
  104. <array property="conditions"> array(...)</array>
  105. <array property="group">
  106. <object key="" class="jDaoConditions" test="#foo" />
  107. </array>
  108. </object>
  109. </object>
  110. </object>
  111. <ressource />
  112. <string value="" />
  113. <integer value="" />
  114. <float value=""/>
  115. <null />
  116. <boolean value="" />
  117. <array>
  118. <object class="">
  119. </object>*/
  120. function _checkIdentical($xml, $value, $name, $errormessage){
  121. $nodename = dom_import_simplexml($xml)->nodeName;
  122. switch($nodename){
  123. case 'object':
  124. if (isset($xml['class'])) {
  125. $this->assertInstanceOf((string)$xml['class'], $value, $name.': not a '.(string)$xml['class'].' object'.$errormessage);
  126. } else {
  127. $this->assertTrue(is_object($value), $name.': not an object'.$errormessage);
  128. }
  129. foreach ($xml->children() as $child) {
  130. if(isset($child['property'])){
  131. $n = (string)$child['property'];
  132. $v = $value->$n;
  133. }elseif(isset($child['p'])){
  134. $n = (string)$child['p'];
  135. $v = $value->$n;
  136. }elseif(isset($child['method'])){
  137. $n = (string)$child['method'];
  138. eval('$v=$value->'.$n.';');
  139. }elseif(isset($child['m'])){
  140. $n = (string)$child['m'];
  141. eval('$v=$value->'.$n.';');
  142. }else{
  143. trigger_error('no method or attribute on '.(dom_import_simplexml($child)->nodeName), E_USER_WARNING);
  144. continue;
  145. }
  146. $this->_checkIdentical($child, $v, $name.'->'.$n,$errormessage);
  147. }
  148. return true;
  149. case 'array':
  150. $this->assertInternalType('array', $value, $name.': not an array'.$errormessage);
  151. if(trim((string)$xml) != ''){
  152. if( false === eval('$v='.(string)$xml.';')){
  153. $this->fail("invalid php array syntax");
  154. return false;
  155. }
  156. $this->assertEquals($v,$value,'negative test on '.$name.': '.$errormessage);
  157. }else{
  158. $key=0;
  159. foreach ($xml->children() as $child) {
  160. if(isset($child['key'])){
  161. $n = (string)$child['key'];
  162. if(is_numeric($n))
  163. $key = intval($n);
  164. }else{
  165. $n = $key ++;
  166. }
  167. $this->assertTrue(array_key_exists($n,$value),$name.'['.$n.'] doesn\'t exist arrrg'.$errormessage);
  168. $v = $value[$n];
  169. $this->_checkIdentical($child, $v, $name.'['.$n.']',$errormessage);
  170. }
  171. }
  172. return true;
  173. case 'string':
  174. $this->assertInternalType('string', $value, $name.': not a string'.$errormessage);
  175. if(isset($xml['value'])){
  176. $this->assertEquals((string)$xml['value'],$value, $name.': bad value. '.$errormessage);
  177. }
  178. return true;
  179. case 'int':
  180. case 'integer':
  181. $this->assertTrue(is_integer($value), $name.': not an integer ('.$value.') '.$errormessage);
  182. if (isset($xml['value'])) {
  183. $this->assertEquals(intval((string)$xml['value']),$value, $name.': bad value. '.$errormessage);
  184. }
  185. return true;
  186. case 'float':
  187. case 'double':
  188. $this->assertInternalType('float', $value,$name.': not a float ('.$value.') '.$errormessage);
  189. if(isset($xml['value'])){
  190. $this->assertEquals( floatval((string)$xml['value']),$value,$name.': bad value. '.$errormessage);
  191. }
  192. return true;
  193. case 'boolean':
  194. $this->assertInternalType('boolean', $value,$name.': not a boolean ('.$value.') '.$errormessage);
  195. if(isset($xml['value'])){
  196. $v = ((string)$xml['value'] == 'true');
  197. $this->assertEquals($v ,$value, $name.': bad value. '.$errormessage);
  198. }
  199. return true;
  200. case 'null':
  201. $this->assertNull($value, $name.': not null ('.$value.') '.$errormessage);
  202. return true;
  203. case 'notnull':
  204. $this->assertNotNull($value, $name.' is null'.$errormessage);
  205. return true;
  206. case 'resource':
  207. $this->assertInternalType('resource', $value,$name.': not a resource'.$errormessage);
  208. return true;
  209. default:
  210. $this->fail("_checkIdentical: balise inconnue ".$nodename.$errormessage);
  211. return false;
  212. }
  213. }
  214. }