samedi 25 janvier 2020

How can I mock the call to the database?

hello I was modifying my code to add a dependency injection to the database connection, and ask if they could help me understand what it would be like to make a mock and be able to demonstrate how CRUDs work with assertEquals, I am testing several examples and reading documentation but I don't understand at all, thank you very much, I used phpunit

<?php 

    class metodos {

    private $conect;

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

    public function showData($sql) {
        $connection = $this->conect->conexion();
        $result = $connection->query($sql);
        return $result->fetch_all(MYSQLI_ASSOC);
    }
    public function insertData($datos){
        $connection = $this->conect->conexion();
        $sql="INSERT into agenda (nro_trans,usuario,anecdota,imagen) values ('$datos[0]','$datos[1]','$datos[2]','$datos[3]')";
        return $result=mysqli_query($connection,$sql);
    }

public function actualizaDatos($datos){
    $connection = $this->conect->conexion();
    $sql="UPDATE agenda set anecdota='$datos[2]',imagen='$datos[3]'
                            where nro_trans='$datos[0]'";
    return $result=mysqli_query($connection,$sql);

}
public function deleteData($nro_trans){
    $connection = $this->conect->conexion();
    $sql="DELETE from agenda where nro_trans='$nro_trans'";
    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 (
        $nro_trans,
        $user,
        $anecdote,
        $route
    );
$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>
<html>
<head>
<meta charset = "utf-8">
<meta http-equiv = "X-UA-Compatible" content = "IE = edge">
<title> Tourism </title>
<link href = 'http: //fonts.googleapis.com/css? 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">
</head>
<body>
<header>

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

    <h1> National and International Tourism </h1>

    <div id = "facebook">
        <p> <a href="http://www.facebook.com/" target="_blank"> <img alt = "Follow us on Facebook" src = "https://lh6.googleusercontent.com/-CYt37hfDnQ8/ T3nNydojf_I / AAAAAAAAAr0 / P5OtlZxV4rk / s32 / facebook32.png "width = 32 height = 32 /> </a> </p>
    </div>

</div>

<div id = "twitter">

    <a href="http://twitter.com/" target="_blank"> <img src = "https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6ir04CP3A5ASn6XGijlvDEcLYvftGqGyfIQshBuTgKnHb91ZC71mgGeQxhvLJ8KrsvkjeqNTB-fqFRVPcX95g6JnEbciSocJ23mwOBa05Ew63YTNMJ8axRHzVkQZFZFUkFxuWDuroBlQJ/ "width = 32 height = 32 alt =" Follow us on Twitter "/> </a>
</div>


</header> <! - / header ->

<nav>
    <div class = "holder">

    <ul>
        <li> <a href="index.php" title=""> Home </a> </li>
        <li> <a href="#"> Where do I travel? </a>
        <ul>
            <li> <a href="provincias.html"> Argentine Provinces </a> </li>
            <li> <a href="#"> Continents </a>
            <ul>
                <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>
             </ul>
        </ul>
        </li>
        </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>
    </ul>
</div>

</nav>

<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;">
              <thead>
              <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
 $c = new conectar();
        $obj = new metodos($c);
        $sql="SELECT * FROM agenda";
        $datos=$obj->mostrarDatos($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;
}
</script>

    </thead>
    </table>
</div>

</section>

<footer>
    <p>
    National and International Tourism
     Homemade Buenos Aires Argentina, Valentín Gómez 4772, B1678
     turismo_nacional_internacional@hotmail.com- Tel. / Fax: +54 11 4575-5012
    </p>

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

Aucun commentaire:

Enregistrer un commentaire