Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
/*
4
 * This file is part of the symfony package.
5
 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
 
11
require_once(dirname(__FILE__).'/../bootstrap/unit.php');
12
 
13
$t = new lime_test(10);
14
 
15
class myClass
16
{
17
  public $ret = '';
18
  static public $retStatic = '';
19
 
20
  public function myMethod()
21
  {
22
    $this->ret  = "before myMethod\n";
23
    sfMixer::callMixins();
24
    $this->ret .= "after myMethod\n";
25
 
26
    return $this->ret;
27
  }
28
 
29
  public function myMethodWithSeveralHooks()
30
  {
31
    $this->ret  = "before myMethodWithSeveralHooks\n";
32
    sfMixer::callMixins('before');
33
 
34
    $this->ret .= "myMethodWithSeveralHooks\n";
35
    sfMixer::callMixins();
36
 
37
    $this->ret .= "after myMethodWithSeveralHooks\n";
38
    sfMixer::callMixins('after');
39
 
40
    return $this->ret;
41
  }
42
 
43
  public function myMethodWithArgs($arg1, $arg2 = 'default')
44
  {
45
    $this->ret  = "before myMethodWithArgs\n";
46
    sfMixer::callMixins();
47
    $this->ret .= "after myMethodWithArgs\n";
48
 
49
    return $this->ret;
50
  }
51
 
52
  static public function myStaticMethod()
53
  {
54
    self::$retStatic = "before myStaticMethod\n";
55
    sfMixer::callMixins();
56
    self::$retStatic .= "after myStaticMethod\n";
57
 
58
    return self::$retStatic;
59
  }
60
 
61
  static public function myStaticMethodWithArgs($arg1, $arg2 = 'default')
62
  {
63
    self::$retStatic = "before myStaticMethodWithArgs\n";
64
    sfMixer::callMixins();
65
    self::$retStatic .= "after myStaticMethodWithArgs\n";
66
 
67
    return self::$retStatic;
68
  }
69
 
70
  function __call($method, $arguments)
71
  {
72
    $r  = "before __call\n";
73
    $r .= sfMixer::callMixins();
74
    $r .= "after __call\n";
75
 
76
    return $r;
77
  }
78
}
79
 
80
$m = new myClass();
81
 
82
$t->is($m->myMethod(), "before myMethod\nafter myMethod\n", 'method call without mixins');
83
$t->is(myClass::myStaticMethod(), "before myStaticMethod\nafter myStaticMethod\n", 'static method call without mixins');
84
 
85
try
86
{
87
  $m->newMethod();
88
  $t->fail('method call that does not exist');
89
}
90
catch (Exception $e)
91
{
92
  $t->pass('method call that does not exist');
93
}
94
 
95
class myClassMixins
96
{
97
  public function myMixinMethod($object)
98
  {
99
    $object->ret .= "in myMethod mixin method\n";
100
  }
101
 
102
  public function myMethodWithSeveralHooks($object)
103
  {
104
    $object->ret .= "in myMethodWithSeveralHooks mixin method for default hook\n";
105
  }
106
 
107
  public function myMethodWithSeveralHooksBefore($object)
108
  {
109
    $object->ret .= "in myMethodWithSeveralHooks mixin method for before hook\n";
110
  }
111
 
112
  public function myMethodWithSeveralHooksAfter($object)
113
  {
114
    $object->ret .= "in myMethodWithSeveralHooks mixin method for after hook\n";
115
  }
116
 
117
// TODO
118
  public function myStaticMixinMethod($object)
119
  {
120
    $object->ret .= "in myStaticMethod mixin method\n";
121
  }
122
 
123
  public function myMixinMethodWithArgs($object, $arg1, $arg2 = 'default')
124
  {
125
    $object->ret .= "in myMethodWithArgs mixin method ($arg1, $arg2)\n";
126
  }
127
 
128
  public function myMixinStaticMethod()
129
  {
130
    myClass::$retStatic .= "in myStaticMethod mixin method\n";
131
  }
132
 
133
  public function myMixinStaticMethodWithArgs($class, $arg1, $arg2 = 'default')
134
  {
135
    myClass::$retStatic .= "in myStaticMethodWithArgs mixin method ($arg1, $arg2)\n";
136
  }
137
 
138
  public function newMethod($object)
139
  {
140
    return "in newMethod mixin method\n";
141
  }
142
 
143
  public function newMethodWithArgs($object, $arg1, $arg2 = 'default')
144
  {
145
    return "in newMethodWithArgs mixin method ($arg1, $arg2)\n";
146
  }
147
}
148
 
149
sfMixer::register('myClass:myMethod', array('myClassMixins', 'myMixinMethod'));
150
sfMixer::register('myClass:myStaticMethod', array('myClassMixins', 'myMixinStaticMethod'));
151
 
152
$t->is($m->myMethod(), "before myMethod\nin myMethod mixin method\nafter myMethod\n", 'method call with a mixin');
153
$t->is(myClass::myStaticMethod(), "before myStaticMethod\nin myStaticMethod mixin method\nafter myStaticMethod\n", 'static method call with a mixin');
154
 
155
sfMixer::register('myClass:myMethodWithArgs', array('myClassMixins', 'myMixinMethodWithArgs'));
156
$t->is($m->myMethodWithArgs('value'), "before myMethodWithArgs\nin myMethodWithArgs mixin method (value, default)\nafter myMethodWithArgs\n", 'method call with arguments with a mixin');
157
 
158
sfMixer::register('myClass:myStaticMethodWithArgs', array('myClassMixins', 'myMixinStaticMethodWithArgs'));
159
$t->is(myClass::myStaticMethodWithArgs('value'), "before myStaticMethodWithArgs\nin myStaticMethodWithArgs mixin method (value, default)\nafter myStaticMethodWithArgs\n", 'static method call with arguments with a mixin');
160
 
161
sfMixer::register('myClass', array('myClassMixins', 'newMethod'));
162
$t->is($m->newMethod(), "before __call\nin newMethod mixin method\nafter __call\n", 'method call from a mixin');
163
 
164
sfMixer::register('myClass', array('myClassMixins', 'newMethodWithArgs'));
165
$t->is($m->newMethodWithArgs('value'), "before __call\nin newMethodWithArgs mixin method (value, default)\nafter __call\n", 'method call from a mixin with arguments');
166
 
167
sfMixer::register('myClass:myMethodWithSeveralHooks:before', array('myClassMixins', 'myMethodWithSeveralHooksBefore'));
168
sfMixer::register('myClass:myMethodWithSeveralHooks', array('myClassMixins', 'myMethodWithSeveralHooks'));
169
sfMixer::register('myClass:myMethodWithSeveralHooks:after', array('myClassMixins', 'myMethodWithSeveralHooksAfter'));
170
$t->is($m->myMethodWithSeveralHooks(), "before myMethodWithSeveralHooks\nin myMethodWithSeveralHooks mixin method for before hook\nmyMethodWithSeveralHooks\nin myMethodWithSeveralHooks mixin method for default hook\nafter myMethodWithSeveralHooks\nin myMethodWithSeveralHooks mixin method for after hook\n", 'method call with several registered hooks');