2012-06-05 52 views
0

我想写的PHPUnit测试这个类写PHPUnit的测试对于某些类我新的PHPUnit用户

,并确保我知道如何执行PHPUnit的

1000感谢帮助:)

IRandomGenerator.php

<?php 
/** 
* Random Generator interface 
* 
* @package GNS 
*/ 

interface IRandomGenerator 
{ 
    /** 
    * Get Random Byte 
    * 
    * @return int 0..255 range integer 
    * @throws NoMoreBytesException 
    */ 
    public function getByte(); 
} 

/** 
* No more bytes can be get exception 
* 
* It can be useful if generator depends on external source 
*/ 
class NoMoreBytesException extends Exception {} 
?> 

ARandomGenerator.class.php

<?php 
/** 
* Base class for random generators 
*/ 

require_once 'IRandomGenerator.php'; 

abstract class ARandomGenerator implements IRandomGenerator 
{ 
    /** 
    * Get Bytes 
    * 
    * @param int $number 
    * @return array Array of 0..255 range integers 
    * @throws NoMoreBytesException 
    */ 
    public function getBytes($number = 1) 
    { 
     $bytes = array(); 
     for ($i = 0; $i < $number; $i++) $bytes[] = $this->getByte(); 
     return $bytes; 
    } 


    /** 
    * Get Word 
    * 
    * @return int 0..65535 range integer 
    * @throws NoMoreBytesException 
    */ 
    public function getWord() 
    { 
     return $this->getByte() << 8 | $this->getByte(); 
    } 


    /** 
    * Get Words 
    * 
    * @param int $number 
    * @return array Array of 0..65535 range integers 
    * @throws NoMoreBytesException 
    */ 
    public function getWords($number = 1) 
    { 
     $words = array(); 
     for ($i = 0; $i < $number; $i++) $words[] = $this->getWord(); 
     return $words; 
    } 


    /** 
    * Get Long 
    * 
    * @return 4-byte integer 
    * @throws NoMoreBytesException 
    */ 
    public function getLong() 
    { 
     return $this->getWord() << 16 | $this->getWord(); 
    } 


    /** 
    * Get Longs 
    * 
    * @param int $number 
    * @return array Array of 4-byte integers 
    * @throws NoMoreBytesException 
    */ 
    public function getLongs($number = 1) 
    { 
     $longs = array(); 
     for ($i = 0; $i < $number; $i++) $longs[] = $this->getLong(); 
     return $longs; 
    } 
} 
?> 

FileRandomGenerator.class

<?php 
/** 
* File random generator (reading data from file) 
* 
* Take care of your file holds really random data 
* and contains it enough for task. Don't use the same 
* data twice 
*/ 

require_once 'ARandomGenerator.class.php'; 

class FileRandomGenerator extends ARandomGenerator 
{ 
    /** @const int Chunk to read from file */ 
    const CHUNKSIZE = 128; 

    /** @var array $pool of data */ 
    private $pool = array(); 

    /** @var resourse Open file descriptor */ 
    private $fd; 


    /** 
    * Constructor 
    * 
    * @param string $fname File name with random data 
    */ 
    public function __construct($fname) 
    { 
    $this->fd = fopen($fname, 'rb'); 
    if ($this->fd === false) throw new NoMoreBytesException('Cannot open file: ' . $fname); 
    } 


    /** 
    * Get Random Byte 
    * 
    * @return int 0..255 range integer 
    * @throws NoMoreBytesException 
    */ 
    public function getByte() 
    { 
     // reading to pool 
     if (count($this->pool) === 0) 
     { 
     if (($data = fread($this->fd, self::CHUNKSIZE)) !== false) 
     $this->pool = unpack('C*', $data); 
     else 
      throw new NoMoreBytesException('No more data in file left'); 
     } 

     return array_pop($this->pool); 
    } 
} 
?> 

和人如何知道从PHPUnit的框架的功能必须要使用测试每个类?

回答

1

一个简单的入门方法是使用phpunit-skelgen。此实用程序将快速为您的班级设置一个简单的测试。

您将要阅读说明书,而是一个 “文艺青年最爱的” 是:

$ mkdir -pv tests/unit 
$ phpunit-skelgen --test -- FileRandomGenerator FileRandomGenerator.class.php \ 
    FileRandomGeneratorTest tests/unit/FileRandomGeneratorTest.php 

HTH