IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Bibliothèques et frameworks PHP Discussion :

[PHPUnit] expectedException


Sujet :

Bibliothèques et frameworks PHP

  1. #1
    Membre éprouvé
    Avatar de Antoine_935
    Profil pro
    Développeur web/mobile
    Inscrit en
    Juillet 2006
    Messages
    883
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur web/mobile

    Informations forums :
    Inscription : Juillet 2006
    Messages : 883
    Points : 1 066
    Points
    1 066
    Par défaut [PHPUnit] expectedException
    Salut à toutes et à tous.

    Je découvre doucement PHPUnit, et voici que je me heurte à ce qui me semble être un bug... Voici la classe testée :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    /**
     * Holds several slots.
     * A slot is a cross-template variable, like a block, a title or even an object.
     * 
     * @since 0.1
     */
    class SlotHolder {
    	/**
    	 * Contains slots indexed by names.
    	 * 
    	 * @var array
    	 */
    	private $slots = array();
     
    	/**
    	 * The name of the slot that has been started with
    	 * {@link SlotHolder::start()}. Null if no slot is open.
    	 * 
    	 * @var string
    	 */
    	private $open = Null;
     
    	/**
    	 * Adds a slot.
    	 * Slot $name must not exist.
    	 * 
    	 * @param string $name name for this slot
    	 * @param mixed $value value for this slot
    	 * @since 0.1
    	 */
    	public function add($name, $value) {
    		$this->crashIfExists($name);
     
    		$this->slots[$name] = $value;
    	}
     
    	/**
    	 * Returns True if a slot is opened.
    	 * 
    	 * @return bool
    	 * @since 0.1
    	 */
    	public function isOpen() {
    		return $this->open !== Null;
    	}
     
    	/**
    	 * Throws a SlotException if slot $name exists.
    	 * 
    	 * @throws SlotException
    	 * @since 0.1
    	 */
    	public function crashIfExists($name) {
    		if ($this->exist($name)) {
    			throw new SlotException(
    				'Caught attempt to duplicate slot "' . $name . '"');
    		}
    	}
     
    	/**
    	 * If a slot is open, terminates the output buffer and throws a
    	 * SlotException.
    	 * 
    	 * @throws SlotException
    	 * @since 0.1
    	 */
    	public function crashIfOpen() {
    		if ($this->isOpen()) {
    			$this->terminateBuffer();
    			throw new SlotException(
    				'Slot "' . $this->open . '" has not been closed.');
    		}
    	}
     
    	/**
    	 * Opens a slot. Its contents will be the text printed until a call to
    	 * {@link SlotHolder::close()} is issued.
    	 * No slot must be opened, and slot $name must not exist.
    	 * 
    	 * @param string $name the name of this slot
    	 * @since 0.1
    	 */
    	public function open($name) {
    		$this->crashIfOpen();
     
    		$this->open = $name;
    		ob_start();
    	}
     
    	/**
    	 * Stores the previously opened slot. Throws a SlotException if no slot is
    	 * open.
    	 * 
    	 * @throws SlotException
    	 * @since 0.1
    	 */
    	public function close() {
    		if (!$this->isOpen()) {
    			throw new SlotException('No slot is open.');
    		}
     
    		$this->add($this->open, ob_get_contents());
    		$this->terminateBuffer();
    	}
     
    	/**
    	 * Terminates the buffer and resets $open to Null.
    	 * This must not be called unless a buffer has been opened by this template.
    	 */
    	private function terminateBuffer() {
    		ob_end_clean();
    		$this->open = Null;
    	}
     
    	/**
    	 * Returns the value of slot $name if it exists,
    	 * the default value otherwise.
    	 * 
    	 * @param string $name
    	 * @param mixed $default
    	 * @return mixed
    	 * @since 0.1
    	 */
    	public function getOrDefault($name, $default) {
    		return array_get_default($this->slots, $name, $default);
    	}
     
    	/**
    	 * Returns the slot $name.
    	 * Throws a SlotException if this slot does not exist.
    	 * 
    	 * @param string $name
    	 * @throws SlotException
    	 * @return mixed
    	 * @since 0.1
    	 */
    	public function getOrCrash($name) {
    		if (array_key_exists($name, $this->slots)) {
    			return $this->slots[$name];
    		}
    		else {
    			throw new SlotException(
    				'Required slot "' . $name . '" is missing.');
    		}
    	}
     
    	/**
    	 * Returns True if slot $name exists, False otherwise.
    	 * 
    	 * @param string $name
    	 * @return bool
    	 * @since 0.1
    	 */
    	public function exist($name) {
    		return array_key_exists($name, $this->slots) || $this->open === $name;
    	}
    }
    Et voici le test qui me donne du fil à retordre :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    class SlotHolderTest extends PHPUnit_Framework_TestCase {
    	private $h;
     
    	public function setUp() {
    		$this->h = new SlotHolder();
    	}
     
    	public function tearDown() {
    		if ($this->h->isOpen()) {
    			$this->h->close();
    		}
    	}
     
    	/**
    	 * Cannot add a slot that has the same name as currently opened slot.
    	 * 
    	 * @expectedException SlotException
    	 */
    	public function testAddThrows2() {
    		$name = 'slot';
     
    		$this->h->open($name);
    		$this->h->add($name, '');
    	}
    }
    Et la sortie console :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    1) SlotHolderTest::testAddThrows2
    SlotException: Caught attempt to duplicate slot "slot"
     
    /home/antoine/zavaee/Web Commons/WebCommons/view.php:64
    /home/antoine/zavaee/Web Commons/WebCommons/view.php:41
    /home/antoine/zavaee/Web Commons/WebCommons/view.php:111
    /home/antoine/zavaee/Web Commons/test/SlotHolderTest.php:21
    Le souci, c'est que je demande justement à PHPUnit d'attendre une SlotException. Ça fonctionne bien pour les autres méthodes, mais pas celle-ci.
    Au point où j'en suis, je suis quasiment convaincu que ce bug est lié à l'output buffer, mais je n'arrive pas à le résoudre. Quelqu'un a une idée ?

  2. #2
    Membre éprouvé
    Avatar de Antoine_935
    Profil pro
    Développeur web/mobile
    Inscrit en
    Juillet 2006
    Messages
    883
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur web/mobile

    Informations forums :
    Inscription : Juillet 2006
    Messages : 883
    Points : 1 066
    Points
    1 066
    Par défaut
    Bon bah je me réponds vu que j'ai trouvé.

    Le problème était ici :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    public function close() {
    		if (!$this->isOpen()) {
    			throw new SlotException('No slot is open.');
    		}
     
    		$this->add($this->open, ob_get_contents());
    		$this->terminateBuffer();
    	}
    Le add lançait une SlotException, alors que ce n'était pas voulu. C'est donc dans le tearDown que le souci était lancé. Merci le rapport de code coverage, qui m'a permi de déceler l'erreur

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Zend Framework et PHPUnit comment l'utiliser ?
    Par Yoteco dans le forum MVC
    Réponses: 8
    Dernier message: 13/05/2009, 19h35
  2. [PHPUnit] Valeur de $_SERVER[DOCUMENT_ROOT]
    Par karl3i dans le forum Bibliothèques et frameworks
    Réponses: 2
    Dernier message: 14/01/2008, 09h49
  3. [Upload] Tests PHPUnit et is_uploaded_file()
    Par Grégory PLANCHAT dans le forum Langage
    Réponses: 2
    Dernier message: 06/12/2007, 23h48
  4. [test] Limites des outils Jmeter , JUnit, PhpUnit ?
    Par foufa007 dans le forum Outils
    Réponses: 5
    Dernier message: 19/06/2007, 11h12

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo