This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
kurs:entwickeln_mit_phpunit [2012/01/12 09:47] mh created |
kurs:entwickeln_mit_phpunit [2014/09/10 21:22] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | Das Objekt | + | Die Klasse |
<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) { | ||
+ | die("ERROR: you tried to set $u with $a"); | ||
+ | } | ||
| | ||
+ | function __get($u) { | ||
+ | die("ERROR: you tried to get $u"); | ||
+ | } | ||
+ | |||
+ | function __call($u, $a) { | ||
+ | die("ERROR: you tried to call $u with $a"); | ||
+ | } | ||
} | } | ||
+ | |||
</code> | </code> | ||
+ | |||
+ | phpunit --skeleton-test Employee | ||
<code> | <code> | ||
+ | <?php | ||
+ | require_once 'C:\xampp\htdocs\employee\Employee.php'; | ||
+ | |||
+ | /** | ||
+ | * Test class for Employee. | ||
+ | * Generated by PHPUnit on 2012-01-11 at 15:27:31. | ||
+ | */ | ||
+ | class EmployeeTest extends PHPUnit_Framework_TestCase | ||
+ | { | ||
+ | /** | ||
+ | * @var Employee | ||
+ | */ | ||
+ | protected $object; | ||
+ | |||
+ | /** | ||
+ | * Sets up the fixture, for example, opens a network connection. | ||
+ | * This method is called before a test is executed. | ||
+ | */ | ||
+ | protected function setUp() | ||
+ | { | ||
+ | $this->object = new Employee; | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Tears down the fixture, for example, closes a network connection. | ||
+ | * This method is called after a test is executed. | ||
+ | */ | ||
+ | protected function tearDown() | ||
+ | { | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * @covers {className}::{origMethodName} | ||
+ | * @todo Implement testSet_last_name(). | ||
+ | */ | ||
+ | public function testSet_last_name() | ||
+ | { | ||
+ | $name = 'Bumsti'; | ||
+ | $this->object->last_name = $name; | ||
+ | $this->assertEquals($this->object->last_name, $name); | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * @covers {className}::{origMethodName} | ||
+ | * @todo Implement testRead(). | ||
+ | */ | ||
+ | public function testStore() | ||
+ | { | ||
+ | // Remove the following lines when you implement this test. | ||
+ | $this->markTestIncomplete( | ||
+ | 'This test has not been implemented yet.' | ||
+ | ); | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * @covers {className}::{origMethodName} | ||
+ | * @todo Implement testStore(). | ||
+ | */ | ||
+ | public function testRead() | ||
+ | { | ||
+ | $name = 'Fay'; | ||
+ | $this->object->set_last_name($name); | ||
+ | $this->object->read(); | ||
+ | $this->assertEquals(6000, $this->object->salary); | ||
+ | } | ||
+ | |||
+ | public function testReadFail() | ||
+ | { | ||
+ | $this->setExpectedException('PEAR_Exception'); | ||
+ | $name = 'Bumsti'; | ||
+ | $this->object->set_last_name($name); | ||
+ | $this->object->read(); | ||
+ | |||
+ | } | ||
+ | } | ||
+ | ?> | ||
+ | </code> | ||
+ | phpunit EmployeeTest.php |