<commit_before>open Common
open OUnit

module Ast = Ast_php

(*****************************************************************************)
(* Prelude *)
(*****************************************************************************)

(*****************************************************************************)
(* Helpers *)
(*****************************************************************************)

(*****************************************************************************)
(* Unit tests *)
(*****************************************************************************)

let unittest =
 "checkers_php" >::: [
  "basic checkers" >:: (fun () ->
  let p path = Filename.concat Config_pfff.path path in

  let test_files = [
    p "tests/php/scheck/common.php";
    p "tests/php/scheck/includes.php";
    p "tests/php/scheck/variables.php";
    p "tests/php/scheck/variables_fp.php";
    p "tests/php/scheck/arrays.php";
    p "tests/php/scheck/functions.php";
    p "tests/php/scheck/builtins.php";
    p "tests/php/scheck/static_methods.php";
    p "tests/php/scheck/methods.php";
    p "tests/php/scheck/classes.php";
    p "tests/php/scheck/traits.php";
    p "tests/php/scheck/cfg.php";
    p "tests/php/scheck/references.php";
    p "tests/php/scheck/xhp.php";
    p "tests/php/scheck/endpoint.php";
    p "tests/php/scheck/dynamic_bailout.php";
    p "tests/php/scheck/edit_distance.php";
    p "tests/php/scheck/misc.php";
    p "tests/php/scheck/lint.php";
  ] 
  in
  let builtin_files =
    Lib_parsing_php.find_php_files_of_dir_or_files [p "/data/php_stdlib"]
  in
  let files = builtin_files ++ test_files in

  let (expected_errors :(Common.filename * int (* line *)) list) =
    test_files +> List.map (fun file ->
      Common.cat file +> Common.index_list_1 +> Common.map_filter 
        (fun (s, idx) -> 
          (* Right now we don't care about the actual error messages. We
           * don't check if they match. We are just happy to check for 
           * correct lines error reporting.
           *)
          if s =~ ".*//ERROR:.*" 
          (* + 1 because the comment is one line before *)
          then Some (file, idx + 1) 
          else None
        )
    ) +> List.flatten
  in
  Error_php._errors := [];

  let verbose = true in

  (* old:
   *  let db = Database_php_build.db_of_files_or_dirs files in
   *  let find_entity = Some (Database_php_build.build_entity_finder db) in
   *)
  let skip_code = [] in
  let cg = 
    Graph_code_php.build ~verbose (Right files) skip_code in
  let find_entity = 
    Some (Entity_php.entity_finder_of_graph_code ~check_dupes:true cg) in
  
  let env = Env_php.mk_env ~php_root:"/" in

  (* run the bugs finders *)
  test_files +> List.iter (fun file ->
    Check_all_php.check_file ~verbose ~find_entity env file
  );
  if verbose then begin
    !Error_php._errors +> List.iter (fun e -> pr (Error_php.string_of_error e))
  end;
  
  let (actual_errors: (Common.filename * int (* line *)) list) = <commit_msg>* lang_php/analyze/checker/unit_checker_php.ml: verbose to false<commit_after>open Common
open OUnit

module Ast = Ast_php

(*****************************************************************************)
(* Prelude *)
(*****************************************************************************)

(*****************************************************************************)
(* Helpers *)
(*****************************************************************************)

(*****************************************************************************)
(* Unit tests *)
(*****************************************************************************)

let unittest =
 "checkers_php" >::: [
  "basic checkers" >:: (fun () ->
  let p path = Filename.concat Config_pfff.path path in

  let test_files = [
    p "tests/php/scheck/common.php";
    p "tests/php/scheck/includes.php";
    p "tests/php/scheck/variables.php";
    p "tests/php/scheck/variables_fp.php";
    p "tests/php/scheck/arrays.php";
    p "tests/php/scheck/functions.php";
    p "tests/php/scheck/builtins.php";
    p "tests/php/scheck/static_methods.php";
    p "tests/php/scheck/methods.php";
    p "tests/php/scheck/classes.php";
    p "tests/php/scheck/traits.php";
    p "tests/php/scheck/cfg.php";
    p "tests/php/scheck/references.php";
    p "tests/php/scheck/xhp.php";
    p "tests/php/scheck/endpoint.php";
    p "tests/php/scheck/dynamic_bailout.php";
    p "tests/php/scheck/edit_distance.php";
    p "tests/php/scheck/misc.php";
    p "tests/php/scheck/lint.php";
  ] 
  in
  let builtin_files =
    Lib_parsing_php.find_php_files_of_dir_or_files [p "/data/php_stdlib"]
  in
  let files = builtin_files ++ test_files in

  let (expected_errors :(Common.filename * int (* line *)) list) =
    test_files +> List.map (fun file ->
      Common.cat file +> Common.index_list_1 +> Common.map_filter 
        (fun (s, idx) -> 
          (* Right now we don't care about the actual error messages. We
           * don't check if they match. We are just happy to check for 
           * correct lines error reporting.
           *)
          if s =~ ".*//ERROR:.*" 
          (* + 1 because the comment is one line before *)
          then Some (file, idx + 1) 
          else None
        )
    ) +> List.flatten
  in
  Error_php._errors := [];

  let verbose = false in

  (* old:
   *  let db = Database_php_build.db_of_files_or_dirs files in
   *  let find_entity = Some (Database_php_build.build_entity_finder db) in
   *)
  let skip_code = [] in
  let cg = 
    Graph_code_php.build ~verbose (Right files) skip_code in
  let find_entity = 
    Some (Entity_php.entity_finder_of_graph_code ~check_dupes:true cg) in
  
  let env = Env_php.mk_env ~php_root:"/" in

  (* run the bugs finders *)
  test_files +> List.iter (fun file ->
    Check_all_php.check_file ~verbose ~find_entity env file
  );
  if verbose then begin
    !Error_php._errors +> List.iter (fun e -> pr (Error_php.string_of_error e))
  end;
  
  let (actual_errors: (Common.filename * int (* line *)) list) = 
{'fraction_non_alphanumeric': 0.2674380165289256, 'fraction_numerical': 0.0009917355371900827, 'mean_word_length': 3.381607530774801, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1205377', 'n_tokens_mistral': 1959, 'n_tokens_neox': 1857, 'n_words': 426}