PHP

Useful code for file-based Unit Test in PHP

CK CK 3788 
Created at
Updated at  
499 0 0

You may need some kind of unit testing when you develop a project composed of multiple PHP files.

Below code block is what I am using for unit testing

if (is_array($argv) && ($argv[0]==basename(__FILE__))) foreach($argv as $v) if ($v=="-d")
{

	// write your test code here

	


	echo <<< CLI_MESSAGE
	===================
	TEST COMPLETED !
	\n\n
	CLI_MESSAGE;
	
}

You can test your code as following on the command line:

php <your filename> -d

 

Below is the class template I use

<?php
# Copyright by Chun Kang <http://qsok.com> 
# 
# @file		yourfilename.php
# @brief	your class library
# @author	Chun Kang (ck@ckii.com)
#
# @notes
# 2024.06.03	created
# 
#


global $ck;
$__class_obj_name = pathinfo(__FILE__)['filename'];
if (@is_object($ck[ $__class_obj_name ]) == false)
{
	// beginning of class ------------------------------------------------------------------------------------------------------
	class TCKYOURCLASS
	{
		function __construct()
		{
		}
	};
	// end of class  ------------------------------------------------------------------------------------------------------

	if (!is_array($ck)) $ck=[];
	$ck[ $__class_obj_name ] = new TCKYOURCLASS;

	if (!function_exists( 'MYFUNCTION' ) )
	{
		function MYFUNCTION()
		{
		}
	}


	/*
		test code
		-----------

		php <YOURFILENAME> -d
	*/
	if (is_array($argv) && ($argv[0]==basename(__FILE__))) foreach($argv as $v) if ($v=="-d")
	{

		// write your test code here

		


		echo <<< CLI_MESSAGE
		===================
		TEST COMPLETED !
		\n\n
		CLI_MESSAGE;
		
	}

}

// removed end of PHP tag intentionally to avoid unintended CR+LF at the code

 

I hope this is helpful for you

Facebook X
Comments 0
  1. Building a MCP server in PHP
    627
  2. PHP 7 vs PHP 8
    1,240
  3. Upgrading PHP 7.4 to PHP 8.1 in CentOS 7
    642
  4. gRPC Server with PHP
    499
  5. PHP Code to Fetch current Coinbase Assets (Balances)
    438
  6. PHP Code to Fetch PEPE coin pricing based in USD from Coinbase
    362
  7. Google Single Sign-On implementation with PHP
    1,367
  8. PHP Superglobal Variables do not require additional syntax to access it
    2,618
  9. Run Python Code with Virtual Environment in PHP
    671
  10. PHP Simplified Operators
    317
  11. PHP Heredoc (aka Nowdoc)
    428
  12. PHP Post Processing by disconnecting HTTP connection and continuing the remaining logics
    1,800
  13. WordPress Remote Posting with PHP
    1,675
  14. Google BigQuery Example to converted Nested FIelds in JSON
    458
  15. Verifying Apple In-App Purchase validation receipt with PHP
    1,779
  16. Function Overloading Implementation in PHP with Magic Mathods
    1,753
  17. PHP Magic - the Null Coalescing Operator (??) - return the value if the give variable is NULL
    1,666
  18. How do I find php.ini on Mac?
    491
  19. Mastering Excel Data Importation in PHP
    553
  20. Create Blob Image in HTML based on the given Text, Width and Height in the Center of the Image without saving file
    963
  21. How do I determine the client IP type (IPv4/IPv6) in PHP
    830
  22. What is 302 Found Redirection in HTTP 1.1?
    489
  23. Implementing a Versatile DNS Server in PHP: Handling A, AAAA, CNAME, and TXT Records
    615
  24. Building a Basic DNS Server in PHP/Python: A Beginner's Guide
    735
  25. CentOS 7 PHP Apache Kafka: Installation, Examples, and System Diagram
    305
  26. Apache Kafka Explained: Powering Netflix, Building with PHP
    489
  27. Array of arguments passed to script in PHP
    85
  28. Bob Jenkins' One-At-A-Time hashing algorithm in PHP
    50
  29. 32bits simple hash function in PHP
    37
  30. PHP Code Compression Tool by removing whitespace, strips comments - PHP Minifier/Compressor
    1,404
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    27
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    24
  3. How do I create animated background for Google Meet?
    19,733
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    217
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    86
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    153
  7. Array of arguments passed to script in PHP
    85
  8. Invalid Auction Storms in Ad Tech
    93
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    198
  10. The Two Clocks of Ad Server Architecture
    63
  11. Frequency Cap Architecture Overview
    509
  12. Collecting Proof-of-Play from 100,000 Screens
    141
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    280
  14. What is RedisVL?
    105
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    224
  16. TPU & GPU Cluster Management Tools
    234
  17. The Concept of Scaffolding in AI Agents
    222
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    576
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    253
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    328
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    197
  22. Machine Learning: Training vs. Ranking
    213
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    217
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    189
  25. Designing a Data Curation System Around a Vector Database
    215
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    229
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    649
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    185
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    207
  30. Understanding Data Lineage
    252