Skip to content

Instantly share code, notes, and snippets.

@tps2015gh
Last active February 9, 2017 07:42
Show Gist options
  • Save tps2015gh/1ec4a179b701f17aacd67a37c1c8f356 to your computer and use it in GitHub Desktop.
Save tps2015gh/1ec4a179b701f17aacd67a37c1c8f356 to your computer and use it in GitHub Desktop.

Check URL and Text

  • This use PHP , CodeIgniter Framework
  • This code is in controller file in project
  • check url with text
  • This in PHP Unit Test Code
<?php 

    // ... SOMECODE BEFORE ..

class MyUnitTest001 extends CI_Controller {

	function __construct() {
       parent::__construct();
      
    }
    // ... SOMECODE BEFORE ..

    function check_url_must_exist(){
    	echo "<h1>" . __FUNCTION__ . "</h1>";
	 	$a_url  = ["controller1/function_that_must_exist/",
	 				'controller1/must_have2'	];
	 	foreach($a_url as $url1 ){
		 	$htm = @file_get_contents(site_url($url1));
		 	//echo "htm = [$htm]";
		 	echo $this->unit->run($htm != "" , true, " '$url1' must exist");

	 	}    	
    }

    // internal function 
    function _url_and_text($url1,$text,$expect){
		 	$htm = @file_get_contents( $url1);
		 	//echo "htm = [$htm]";
		 	// echo "[";
		 	// echo htmlentities($htm);
		 	// echo "]";
		 	$needle = $text ; 
		 	$isfound = $this->_is_found($htm, $needle);
		 	if ($expect == false){ $NOT = " NOT "; }else{ $NOT = "" ;}
		 	return $this->unit->run($isfound, $expect, " '$url1' must  $NOT have text '$needle' ");
    }
    
     // internal function 
    function _is_found($haystack , $needle){
    	$p =  strpos($haystack, $needle );	
		return  ($p==0&&is_int($p))||($p!=0) ;
	}
  
   function index(){
      echo $this->check_url_must_have_text(); 
   }

 // must use in main  index page 
 	function check_url_must_have_text(){
 			echo "<h1>" . __FUNCTION__ . "</h1>";
 			
 			echo $this->_url_and_text(site_url("ctrl1/"),
 								"ข้อมูลสำคัญ" ,true   );

 			echo $this->_url_and_text(site_url("ctrl2/"),
 								"data_that have to show in page" ,true   );
 			// MUST NOT HAVE
 			echo $this->_url_and_text(site_url("ctr1/"),
 								"data that must hide, not show " ,false   ); 
 			echo $this->_url_and_text(site_url("ctrl2/"),
 								"data that must hide" ,false   ); 								
 			 

    }
 
    // CHECK NODE MODULE 
    function check_component_file(){
    	echo "<h1>" . __FUNCTION__ . "</h1>";

	 	$a_fname  = ["bootstrap","jquery","vue"];
	 	foreach($a_fname as $fname1){
	 	 	$fname = "C:\\xampp\\htdocs\\{yourprojectfile}\\node_modules\\$fname1";
		 	echo $this->unit->run(  file_exists($fname) , /*expect */ true 
		 		,"file <b>'$fname'</b> MUST  exist " ,""  );	
	 	}    	

 
    }

  // ... SOMECODE AFTER ..
 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment