This shows you the differences between two versions of the page.
— |
oo_phpunit [2016/05/24 15:41] (current) mh created |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | personTest.php | ||
+ | <code> | ||
+ | <?php | ||
+ | require("../db.php"); | ||
+ | require("lib/Person.php"); | ||
+ | class personTest extends PHPUnit_Framework_TestCase { | ||
+ | function testPersonReadWrite() { | ||
+ | $dbh = get_db(); | ||
+ | $fullname = 'Mein Test'; | ||
+ | $birthdate = '2002-05-05'; | ||
+ | $country_code = 'AUT'; | ||
+ | $person = new Person(); | ||
+ | $person->dbh = $dbh; | ||
+ | $person->setFullname($fullname); | ||
+ | $person->setBirthdate($birthdate); | ||
+ | $person->setCountryCode($country_code); | ||
+ | //print $person->getFullname() ."\n"; | ||
+ | //var_dump($person); | ||
+ | $person->store(); | ||
+ | // print "id: ". $person->getId() ."\n";s | ||
+ | $person->setFullname('New Name'); | ||
+ | $person->store(); | ||
+ | $person2 = new Person(); | ||
+ | $person2->dbh = $dbh; | ||
+ | $person2->read($person->getId()); | ||
+ | // print "Name: ". $person2->getFullname() ."\n"; | ||
+ | $this->assertEquals($person2->getFullname(), $person->getFullname()); | ||
+ | $this->assertEquals($person2->getBirthdate(), $person->getBirthdate()); | ||
+ | |||
+ | $person2->delete(); | ||
+ | # $person3 = new Person(); | ||
+ | # $person3->read($person->getId()); # sollte einen Fehler produzieren | ||
+ | |||
+ | } | ||
+ | } | ||
+ | |||
+ | </code> | ||
+ | |||
+ | lib/Init.php | ||
+ | <code> | ||
+ | <?php | ||
+ | |||
+ | class Init { | ||
+ | var $dbh; | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | |||
+ | lib/Person.php | ||
+ | <code> | ||
+ | <?php | ||
+ | |||
+ | require('Init.php'); | ||
+ | |||
+ | class Person extends Init | ||
+ | { | ||
+ | private $fullname; | ||
+ | private $birthdate; | ||
+ | private $country_code; | ||
+ | private $id = ''; | ||
+ | |||
+ | function setFullname($value) { | ||
+ | $this->fullname = $value; | ||
+ | } | ||
+ | |||
+ | function getFullname() { | ||
+ | return $this->fullname; | ||
+ | } | ||
+ | |||
+ | function setBirthdate($value) { | ||
+ | $this->birthdate = $value; | ||
+ | } | ||
+ | |||
+ | function getBirthdate() { | ||
+ | return $this->birthdate; | ||
+ | } | ||
+ | |||
+ | function setCountryCode($value) { | ||
+ | $this->country_code = $value; | ||
+ | } | ||
+ | |||
+ | function getCountryCode() { | ||
+ | return $this->country_code; | ||
+ | } | ||
+ | |||
+ | function setId($value) { | ||
+ | $this->id = $value; | ||
+ | } | ||
+ | |||
+ | function getId() { | ||
+ | return $this->id; | ||
+ | } | ||
+ | | ||
+ | function store() { | ||
+ | $op = ''; | ||
+ | if ($this->id != '') { | ||
+ | $op = 'update'; | ||
+ | $sth = $this->dbh->prepare( | ||
+ | "update person | ||
+ | set fullname = ?, | ||
+ | birthdate = ?, | ||
+ | country_code = ? | ||
+ | where id = ?" | ||
+ | ); | ||
+ | $sth->bind_param("sssi", | ||
+ | $this->fullname, | ||
+ | $this->birthdate, | ||
+ | $this->country_code, | ||
+ | $this->id); | ||
+ | } else { | ||
+ | $op = 'insert'; | ||
+ | $sth = $this->dbh->prepare( | ||
+ | "insert into person | ||
+ | (fullname, | ||
+ | birthdate, | ||
+ | country_code) | ||
+ | values (?, ?, ?)" | ||
+ | ); | ||
+ | $sth->bind_param("sss", | ||
+ | $this->fullname, | ||
+ | $this->birthdate, | ||
+ | $this->country_code); | ||
+ | } | ||
+ | $sth->execute(); | ||
+ | if ($op == 'insert') { | ||
+ | $this->id = $sth->insert_id; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | function read($per_id) { | ||
+ | $sth = $this->dbh->prepare( | ||
+ | 'select id, fullname, birthdate, country_code from person where id = ?' | ||
+ | ); | ||
+ | $sth->bind_param("i", $per_id); | ||
+ | $sth->execute(); | ||
+ | $sth->bind_result($this->id, $this->fullname, $this->birthdate, $this->country_code); | ||
+ | $sth->fetch(); | ||
+ | } | ||
+ | |||
+ | function delete() { | ||
+ | $sth = $this->dbh->prepare( | ||
+ | 'delete from person where id = ?' | ||
+ | ); | ||
+ | $sth->bind_param("i", $this->id); | ||
+ | $sth->execute(); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </code> | ||