1<?php
2
3require "tests.php";
4require "director_exception.php";
5
6// No new functions
7check::functions(array('foo_ping','foo_pong','launder','bar_ping','bar_pong','returnalltypes_return_int','returnalltypes_return_double','returnalltypes_return_const_char_star','returnalltypes_return_std_string','returnalltypes_return_bar','returnalltypes_call_int','returnalltypes_call_double','returnalltypes_call_const_char_star','returnalltypes_call_std_string','returnalltypes_call_bar','is_python_builtin'));
8// No new classes
9check::classes(array('director_exception','Foo','Exception1','Exception2','Base','Bar','ReturnAllTypes'));
10// now new vars
11check::globals(array());
12
13class MyException extends Exception {
14  function __construct($a, $b) {
15    $this->msg = $a . $b;
16  }
17}
18
19class MyFoo extends Foo {
20  function ping() {
21    throw new Exception("MyFoo::ping() EXCEPTION");
22  }
23}
24
25class MyFoo2 extends Foo {
26  function ping() {
27    return true;
28  }
29}
30
31class MyFoo3 extends Foo {
32  function ping() {
33    throw new MyException("foo", "bar");
34  }
35}
36
37# Check that the Exception raised by MyFoo.ping() is returned by
38# MyFoo.pong().
39$ok = 0;
40$a = new MyFoo();
41# TODO: Currently we do not track the dynamic type of returned
42# objects, so we skip the launder() call.
43#$b = director_exception::launder($a);
44$b = $a;
45try {
46  $b->pong();
47} catch (Exception $e) {
48  $ok = 1;
49  check::equal($e->getMessage(), "MyFoo::ping() EXCEPTION", "Unexpected error message #1");
50}
51check::equal($ok, 1, "Got no exception while expected one #1");
52
53# Check that the director can return an exception which requires two
54# arguments to the constructor, without mangling it.
55$ok = 0;
56$a = new MyFoo3();
57#$b = director_exception::launder($a);
58$b = $a;
59try {
60  $b->pong();
61} catch (Exception $e) {
62  $ok = 1;
63  check::equal($e->msg, "foobar", "Unexpected error message #2");
64}
65check::equal($ok, 1, "Got no exception while expected one #2");
66
67try {
68  throw new Exception2();
69} catch (Exception2 $e2) {
70}
71
72try {
73  throw new Exception1();
74} catch (Exception1 $e1) {
75}
76
77// Check that we can throw exceptions from director methods (this didn't used
78// to work in all cases, as the exception gets "set" in PHP and the method
79// then returns PHP NULL, which the directorout template may fail to convert.
80
81class Bad extends ReturnAllTypes {
82  function return_int() { throw new Exception("bad int"); }
83  function return_double() { throw new Exception("bad double"); }
84  function return_const_char_star() { throw new Exception("bad const_char_star"); }
85  function return_std_string() { throw new Exception("bad std_string"); }
86  function return_Bar() { throw new Exception("bad Bar"); }
87}
88
89$bad = new Bad();
90
91try {
92    $bad->call_int();
93    check::fail("Exception wasn't propagated from Bad::return_int()");
94} catch (Exception $e) {
95    check::equal($e->getMessage(), "bad int", "propagated exception incorrect");
96}
97
98try {
99    $bad->call_double();
100    check::fail("Exception wasn't propagated from Bad::return_double()");
101} catch (Exception $e) {
102    check::equal($e->getMessage(), "bad double", "propagated exception incorrect");
103}
104
105try {
106    $bad->call_const_char_star();
107    check::fail("Exception wasn't propagated from Bad::return_const_char_star()");
108} catch (Exception $e) {
109    check::equal($e->getMessage(), "bad const_char_star", "propagated exception incorrect");
110}
111
112try {
113    $bad->call_std_string();
114    check::fail("Exception wasn't propagated from Bad::return_std_string()");
115} catch (Exception $e) {
116    check::equal($e->getMessage(), "bad std_string", "propagated exception incorrect");
117}
118
119try {
120    $bad->call_Bar();
121    check::fail("Exception wasn't propagated from Bad::return_Bar()");
122} catch (Exception $e) {
123    check::equal($e->getMessage(), "bad Bar", "propagated exception incorrect");
124}
125
126check::done();
127?>
128