This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
kurs:entwickeln_mit_phpunit [2012/01/12 11:07] mh |
kurs:entwickeln_mit_phpunit [2012/01/12 12:43] mh |
||
---|---|---|---|
Line 3: | Line 3: | ||
<code> | <code> | ||
<?php | <?php | ||
+ | |||
+ | require_once 'PEAR/Exception.php'; | ||
class Employee { | class Employee { | ||
var $id, $last_name, $salary; | var $id, $last_name, $salary; | ||
var $db; | var $db; | ||
- | | + | |
+ | function Employee() { | ||
+ | $this->db_connect(); | ||
+ | } | ||
private function db_connect() { | private function db_connect() { | ||
- | $this->db = oci_connect('hr', 'hr', 'orcl'); | + | $this->db = oci_connect('hr', 'hr', 'orcl'); |
} | } | ||
- | | + | |
function set_last_name($last_name) { | function set_last_name($last_name) { | ||
$this->last_name = $last_name; | $this->last_name = $last_name; | ||
} | } | ||
- | | + | |
function read() { | function read() { | ||
- | $this->db_connect(); | + | $q = oci_parse($this->db, |
- | $q = oci_parse($this->db, | + | "select last_name ln, salary |
- | "select last_name ln, salary | + | |
from employees | from employees | ||
- | where last_name = :b_last_name"); | + | where last_name = :b_last_name"); |
oci_bind_by_name | oci_bind_by_name | ||
($q, ":b_last_name", $this->last_name); | ($q, ":b_last_name", $this->last_name); | ||
$r = oci_execute($q, OCI_DEFAULT); | $r = oci_execute($q, OCI_DEFAULT); | ||
$row = oci_fetch_array($q, OCI_ASSOC); | $row = oci_fetch_array($q, OCI_ASSOC); | ||
- | $this->salary = $row['SALARY']; | + | if (!isset($row['LN'])) { |
- | } | + | throw new PEAR_Exception("no employee ".$this->last_name |
- | + | , 404); | |
+ | } | ||
+ | |||
+ | $this->salary = $row['SALARY']; | ||
+ | |||
+ | } | ||
function store() {} | function store() {} | ||
- | | + | |
function __set($u, $a) { | function __set($u, $a) { | ||
die("ERROR: you tried to set $u with $a"); | die("ERROR: you tried to set $u with $a"); | ||
Line 42: | Line 53: | ||
die("ERROR: you tried to call $u with $a"); | die("ERROR: you tried to call $u with $a"); | ||
} | } | ||
- | |||
} | } | ||
+ | |||
</code> | </code> | ||
Line 89: | Line 100: | ||
{ | { | ||
$name = 'Bumsti'; | $name = 'Bumsti'; | ||
- | $this->object->set_last_name($name); | + | $this->object->last_name = $name; |
$this->assertEquals($this->object->last_name, $name); | $this->assertEquals($this->object->last_name, $name); | ||
} | } | ||
Line 110: | Line 121: | ||
*/ | */ | ||
public function testRead() | public function testRead() | ||
- | { | + | { |
$name = 'Fay'; | $name = 'Fay'; | ||
$this->object->set_last_name($name); | $this->object->set_last_name($name); | ||
$this->object->read(); | $this->object->read(); | ||
$this->assertEquals(6000, $this->object->salary); | $this->assertEquals(6000, $this->object->salary); | ||
+ | } | ||
+ | |||
+ | public function testReadFail() | ||
+ | { | ||
+ | $this->setExpectedException('PEAR_Exception'); | ||
+ | $name = 'Bumsti'; | ||
+ | $this->object->set_last_name($name); | ||
+ | $this->object->read(); | ||
+ | |||
} | } | ||
} | } |