CiteProcTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: loconox
  5. * Date: 29/03/2017
  6. * Time: 11:46
  7. */
  8. class CiteProcTest
  9. {
  10. public function test() {
  11. $container = $this->getContainer();
  12. $root = $container->get('kernel')->getRootDir();
  13. $dir_handle = opendir($root.'/../tests/suite');
  14. while (FALSE !== ($filename = readdir($dir_handle))) {
  15. if ( !is_dir($root.'/../tests/suite/' . $filename) && $filename[0] != '.') {
  16. $json_data = file_get_contents($root.'/../tests/suite/' . $filename);
  17. // $json_data = substr($json_data, strpos($json_data, '*/{')+2);
  18. $test_data = json_decode($json_data);
  19. switch (json_last_error()) {
  20. case JSON_ERROR_NONE:
  21. echo ' - No errors';
  22. break;
  23. case JSON_ERROR_DEPTH:
  24. echo ' - Maximum stack depth exceeded';
  25. break;
  26. case JSON_ERROR_STATE_MISMATCH:
  27. echo ' - Underflow or the modes mismatch';
  28. break;
  29. case JSON_ERROR_CTRL_CHAR:
  30. echo ' - Unexpected control character found';
  31. break;
  32. case JSON_ERROR_SYNTAX:
  33. echo ' - Syntax error, malformed JSON';
  34. break;
  35. case JSON_ERROR_UTF8:
  36. echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
  37. break;
  38. default:
  39. echo ' - Unknown error';
  40. break;
  41. }
  42. if ($test_data->mode == 'bibliography') {
  43. $citeproc = new CiteProc($test_data->csl);
  44. $input_data = (array)$test_data->input;
  45. $count = count($input_data);
  46. $output = '';
  47. foreach($input_data as $data) {
  48. $output .= $citeproc->render($data, $test_data->mode);
  49. }
  50. //print '<html><body>';
  51. if ($output != $test_data->result) {
  52. print $root.'/../tests/suite/' . $filename . " FAILED\n";
  53. print $output . " != <br>\n" . $test_data->result ."<br><br>\n\n";
  54. }
  55. else {
  56. print $root.'/../tests/suite/' . $filename . " PASSED\n";
  57. }
  58. }
  59. }
  60. }
  61. }
  62. }