Commit 72868418 authored by Julien Ait azzouzene's avatar Julien Ait azzouzene
Browse files

test: mise en place de tests unitaires

parent 7d984b68
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
# fichier dotenv
.env

# Contenu de l'edito
assets/edito.txt
 No newline at end of file

src/lib/assertion.php

0 → 100644
+14 −0
Original line number Diff line number Diff line
<?php
class AssertionFailedException extends Exception{}

function assertEquals($var1, $var2){
    if($var1 !== $var2){
        throw new AssertionFailedException("expected value : ".$var1." actual : ".$var2."");
    }
}

function assertTrue($bool){
    if(! $bool){
        throw new AssertionFailedException("expected true value");
    }
}
 No newline at end of file
+15 −4
Original line number Diff line number Diff line
@@ -11,6 +11,11 @@ declare(strict_types=1);
 * @author Julien Ait azzouzene <julien.aitazzouzene@etu.unicaen.fr>
 */
class EditoManager{
    private string $cheminEdito;

    public function __construct($cheminEdito = "assets/edito.txt") {
        $this->cheminEdito = $cheminEdito;
    }

    /**
     * Renvoie le texte de l'édito
@@ -18,9 +23,13 @@ class EditoManager{
     * @return string
     */
    public function getEdito(): string{
        throw new Exception('not implemented yet');
        $contenu = file_get_contents($this->cheminEdito);
        if(! $contenu){
            throw new Exception("Échec de la lecture dans le fichier");
        }

        return $contenu;
    }
        
    /**
     * Modifie le texte de l'édito.
@@ -29,6 +38,8 @@ class EditoManager{
     * @return void
     */
    public function setEdito(string $edito){
        throw new Exception('not implemented yet');
        if(! file_put_contents($this->cheminEdito, $edito)){
            throw new Exception("Échec de l'écriture dans le fichier");
        }
    }
}
 No newline at end of file

test/index.php

0 → 100644
+9 −0
Original line number Diff line number Diff line
<?php 

require_once '../src/lib/assertion.php';

require_once 'testEdito.php';

file_put_contents('test.txt', "test");

echo "<h2>Les tests ont passés avec succès</h2>";
 No newline at end of file

test/testEdito.php

0 → 100644
+12 −0
Original line number Diff line number Diff line
<?php

require_once '../src/model/editoManager.php';

testEdito();

function testEdito(){
    $manager = new EditoManager("../assets/edito.txt");
    $manager->setEdito(" ");
    $manager->setEdito("Bonjour, ceci est l'édito");
    assertEquals("Bonjour, ceci est l'édito", $manager->getEdito());
}
 No newline at end of file