Search
 
SCRIPT & CODE EXAMPLE
 

PHP

Inject interface and not concrete class

class DB implements DBInterface
{
    // A bunch of methods fetching from real DB
}

class MockDB implements DBInterface
{
    // Has the same (interface) methods but returns some fixed data for testing.
}

class Posts
{
    public function __construct(DBInterface $db)
    {
        $this->db = $db;
    }

    public function get()
    {
        $this->db->query("....");
    }
}

$posts = new Posts(new DB);
$data  = $posts->get(); // This fetches from the real db

// And for testing
$posts = new Posts(new MockDB);
$data  = posts->get(); // Fetches from the mock DB instead
Comment

PREVIOUS NEXT
Code Example
Php :: storefront header cart 
Php :: php pdo connect to database 
Php :: merge two entity symfony 
Php :: html vs php 
Php :: woocommerce php customer reset password length 
Php :: old codestar textarea field 
Php :: How to prevent repeating the same option value of a selection in a php loop 
Php :: laravel openstreetmap 
Php :: hi we add file in orders the other hide and order show in laravel view 
Php :: php decrement variable by 1 
Php :: user ,role ,permission customize laravel application 
Php :: file upload yii2 rest api 
Php :: drupal 9 custom local stream wrapper 
Php :: Trongate custom routing 
Php :: laravel title dynamic 
Php :: nl2br is not working for database in php 
Php :: php strom key 2 
Php :: does grepper use PHP 
Php :: lista 
Php :: php function to show nav menu with example 
Php :: laravel ignition dark mode 
Php :: how to convert array into json php 
Php :: laravel disable cors 
Php :: php obtener slug wordpress 
Php :: php if simple 
Php :: laravel relationship hasmany 
Php :: php like button counter 
Php :: laravel api get controller 
Php :: laravel chain query builder 
Php :: php define() 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =