php - write a PHPUnit Test for some classes i am new PHPUnit user -
i want write phpunit test classes
and sure know how execute phpunit
1000 helper :)
irandomgenerator.php
<?php /** * random generator interface * * @package gns */ interface irandomgenerator { /** * random byte * * @return int 0..255 range integer * @throws nomorebytesexception */ public function getbyte(); } /** * no more bytes can exception * * can useful if generator depends on external source */ class nomorebytesexception extends exception {} ?> arandomgenerator.class.php
<?php /** * base class random generators */ require_once 'irandomgenerator.php'; abstract class arandomgenerator implements irandomgenerator { /** * bytes * * @param int $number * @return array array of 0..255 range integers * @throws nomorebytesexception */ public function getbytes($number = 1) { $bytes = array(); ($i = 0; $i < $number; $i++) $bytes[] = $this->getbyte(); return $bytes; } /** * word * * @return int 0..65535 range integer * @throws nomorebytesexception */ public function getword() { return $this->getbyte() << 8 | $this->getbyte(); } /** * words * * @param int $number * @return array array of 0..65535 range integers * @throws nomorebytesexception */ public function getwords($number = 1) { $words = array(); ($i = 0; $i < $number; $i++) $words[] = $this->getword(); return $words; } /** * long * * @return 4-byte integer * @throws nomorebytesexception */ public function getlong() { return $this->getword() << 16 | $this->getword(); } /** * longs * * @param int $number * @return array array of 4-byte integers * @throws nomorebytesexception */ public function getlongs($number = 1) { $longs = array(); ($i = 0; $i < $number; $i++) $longs[] = $this->getlong(); return $longs; } } ?> filerandomgenerator.class
<?php /** * file random generator (reading data file) * * take care of file holds random data * , contains enough task. don't use same * data twice */ require_once 'arandomgenerator.class.php'; class filerandomgenerator extends arandomgenerator { /** @const int chunk read 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 random data */ public function __construct($fname) { $this->fd = fopen($fname, 'rb'); if ($this->fd === false) throw new nomorebytesexception('cannot open file: ' . $fname); } /** * random byte * * @return int 0..255 range integer * @throws nomorebytesexception */ public function getbyte() { // reading 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); } } ?> and how know function phpunit framework must use test each class ?
one easy way started use phpunit-skelgen. utility set bare-bones test class.
you want read manual, "tl;dr" is:
$ mkdir -pv tests/unit $ phpunit-skelgen --test -- filerandomgenerator filerandomgenerator.class.php \ filerandomgeneratortest tests/unit/filerandomgeneratortest.php hth
Comments
Post a Comment