Solution 1 :

In order to mock the queries, you’ll need to make two changes.

  1. Inject the connection object rather than instantiating it within the methods
  2. Use the object oriented mysqli interface rather than the procedural one.

class methods {

    protected $connect;

    // This way you can inject either the real connection object or the mock

    public function __construct($connect) {
        $this->connect = $connect;

    // Using object methods here rather than procedural functions that require
    // specific built-in objects like `mysqli_result` will let your method use
    // either the real connection or the mock

    public function showData($sql) {
        $connection = $this->connect->connection();
        $result = $connection->query($sql);
        return $result->fetch_all(MYSQLI_ASSOC);
    // ... (make similar adjustments to the other methods)

If you make these changes then you can create a mock object with the necessary methods. Here’s a basic example using anonymous classes. You can adjust it to make it as complex as necessary for whatever testing you decide to do. If you’re using PHPUnit, you can set up something similar using its mock objects.

$mockConnection = new class {
    function connection() {
        return new class() {
            function query($sql) {
                return new class() {
                    function fetch_all($fetchStyle) {
                        return [
                            ['col1Name' => 'col1ValueA', 'col2Name' => 'col2ValueA'],
                            ['col1Name' => 'col1ValueB', 'col2Name' => 'col2ValueB']

$testInstance = new methods($mockConnection);

$testResult = $testInstance->showData('SELECT etc.');

Problem :

I was making changes to my code to pass it to objects and wanted to know if it is now possible to mock the call to the database to perform unit tests with PHPUnit or if something else needs to be changed.

the class that has the database methods

<? php

class methods {
    public function showData ($ sql) {
        $c = new connect();
        $connection = $c->connection();

        $result = mysqli_query ($connection, $sql);
        return mysqli_fetch_all ($result, MYSQLI_ASSOC);

    public function insertDataName ($data) {
        $c = new connect();
        $connection = $c->connection();

        $sql = "INSERT into agenda (nro_trans, user, anecdote, image)
                values ('$data[0]','$data[1]','$data[2]','$data[3]') ";

        return $result = mysqli_query($connection, $sql);

call the method to use and insert the data taken from the html in an array

<? php
require_once "conect.php";
require_once "metodosCrud.php";

$nro_trans = $_POST['nro_trans'];
$user = $_POST['user'];
$anecdote = $_POST['anecdote'];
$image = $_FILES['image'];

$filename = $_FILES['image']['name']; // To know the name of the file
$path = "../imagenes/". $filename; // The file path contains the new name and extension type
$file = $_FILES['image']['tmp_name']; // the file to upload
$upload = move_uploaded_file ($file, $path); //

$data = array (
$obj = new methods();
if ($obj-> insertDataName($data) == 1) {
    echo '<script>
            alert ("Anecdote entered correctly !!");
            ; window.location.href = "../model/agenda.php";
            </script> ';
} else {
    echo "failed to add";

the index

<! DOCTYPE html>
<meta charset = "utf-8">
<meta http-equiv = "X-UA-Compatible" content = "IE = edge">
<title> Tourism </title>
<link href = 'http: // family = Open + Sans: 400,700,400italic' rel = 'stylesheet' type = 'text / css'>
<link rel = "stylesheet" href = "css / styles.css">
<link rel = "stylesheet" href = "css / normalize.css">

<div class = "holder">
    <section class = "logo">
        <img src = "gallery / logo.jpg" alt = "logo">

    <h1> National and International Tourism </h1>

    <div id = "facebook">
        <p> <a href="" target="_blank"> <img alt = "Follow us on Facebook" src = " T3nNydojf_I / AAAAAAAAAr0 / P5OtlZxV4rk / s32 / facebook32.png "width = 32 height = 32 /> </a> </p>


<div id = "twitter">

    <a href="" target="_blank"> <img src = " "width = 32 height = 32 alt =" Follow us on Twitter "/> </a>

</header> <! - / header ->

    <div class = "holder">

        <li> <a href="index.php" title=""> Home </a> </li>
        <li> <a href="#"> Where do I travel? </a>
            <li> <a href="provincias.html"> Argentine Provinces </a> </li>
            <li> <a href="#"> Continents </a>
                <li> <a href="vistas/america.html"> America </a> </li>
                <li> <a href="vistas/europa.html"> Europe </a> </li>
                <li> <a href="vistas/asia.html"> Asia </a> </li>
        <li> <a href="services.html" title=""> Services </a> </li>
        <li> <a href="pay.html" title=""> Payment Methods </a> </li>
        <li> <a href="agenda.php" title=""> Agenda </a> <li>
        <li> <a href="contact.html" title=""> Contact </a> <li>


<section class = "holder">
    <h2> Traveler's agenda </h2>
        <div class = "column left">
            <p> Enter the anecdote you want to save. You have the option to delete and delete available if you are logged in to the page </p>

            <table style = "margin: auto; width: 800px; border-collapse: separate; border-spacing: 10px 5px;">
              <th> Nro Anecdota </th>
              <th> User </th>
              <th> Description </th>
              <th> Image </th>
              <th> <a href="agnewNew.php"> <button type = 'button' class = 'btn btn-info'> New </button> </a> </th>

<? php
$obj = new methods ();
$sql = "SELECT * FROM agenda";
$data = $obj-> show Data ($ sql);
// show data on screen
echo "<tr>";
    echo "<td>"; echo $row ['nro_trans']; echo "</td>";
    echo "<td>"; echo $row ['user']; echo "</td>";
    echo "<td>"; echo $row ['anecdote']; echo "</td>";
  // echo "<td>"; echo $row ['image']; echo "</td>";
    echo "<td>"; echo "<img src = '". $row ['image']. "' width = '300'>"; echo "</td>";
    echo "<td> <a href='modify.php?nro_trans=".$fila['nro_trans'[."'> <button type = 'button' class = 'btn btn-success'> Modify </button> </a> </td> ";
    echo "<td> <a href='eliminarAnecdota.php?nro_trans=".$fila['nro_trans'[."'> <button type = 'button' class = 'btn btn-danger' onclick = 'return ConfirmDelete() '> Remove </button> </a> </td> ";

echo "</tr>";

<script type="text/javascript">
function ConfirmDelete () {
    var answer = confirm ("Are you sure you want the anecdote?");

    if (answer == true) {
        return true;
    } else {
    return false;



    National and International Tourism
     Homemade Buenos Aires Argentina, Valentín Gómez 4772, B1678
     [email protected] Tel. / Fax: +54 11 4575-5012

    <p> Copyright 2020: Design and Programming: Ezequiel Ledesma </p>



Comment posted by RiggsFolly

Make fun???

Comment posted by Take a quick look at a coding standard

Good code indentation would help us read the code and more importantly it will help

Comment posted by Ezequiel Ledesma

@RiggsFolly I have in mind that I have to fix the sangria but I still didn’t have time to find out how to configure sublimetext, I want to make the mock to be able to perform unit tests of the crud comparing the mock with value with another

Comment posted by RiggsFolly

i/we are still not sure WHAT >it< is you are trying to do

Comment posted by Don’t Panic

I think “make fun of” was probably a translation error. While it generally is a synonym for mock, it doesn’t really mean the same thing in this context. I apologize if I was mistaken.

Comment posted by Ezequiel Ledesma

thanks, I’m already at least showing the dependency injection, as soon as I can modify the rest and I see how to make the mock with what you added

Comment posted by Ezequiel Ledesma

One question, using this example to do the assertEquals is asking me for an array to compare, which would be because I am not realizing


Leave a Reply

Your email address will not be published. Required fields are marked *