1<?php
2
3/**
4 * Class for method under test (see gtFunction for non-OO tests)
5 */
6class gtMethod  extends gtTestSubject {
7
8  private $className;
9  private $methodName;
10  private $constructorArgumentNames;
11  private $constructorArgumentList = '';
12  private $constructorInitialisationStatements;
13
14
15
16  /**
17   * Construct gtMethod object from the class and method names
18   *
19   * @param string $className
20   * @param string $methodName
21   */
22  public function __construct($className, $methodName) {
23    $this->className = $className;
24    $this->methodName = $methodName;
25  }
26
27
28  /**
29   * Set the names of the class constructor arguments. Take only mandatory argument names.
30   *
31   */
32  public function setConstructorArgumentNames() {
33    $reflectionClass = new ReflectionClass($this->className);
34    $constructor = $reflectionClass->getConstructor();
35    foreach($constructor->getParameters() as $i => $param) {
36      //if(!$param->isOptional()) {
37        $this->constructorArgumentNames[] = $param->getName();
38      //}
39    }
40  }
41
42
43  /**
44   * Set the names of the mandatory and optional arguments to the method
45   *
46   */
47  public function setArgumentNames() {
48
49    $methodClass  = new reflectionMethod($this->className, $this->methodName);
50    $parameters = $methodClass->getParameters();
51
52    foreach ($methodClass->getParameters() as $i => $param) {
53      if($param->isOptional()) {
54        $this->optionalArgumentNames[] = $param->getName();
55      } else {
56        $this->mandatoryArgumentNames[] = $param->getName();
57      }
58
59    }
60  }
61
62
63  /**
64   * Return the list of constructor argument names
65   *
66   * @return array
67   */
68  public function getConstructorArgumentNames() {
69    return $this->constructorArgumentNames;
70  }
71
72  /**
73   * Return the name of the method
74   *
75   * @return string
76   */
77  public function getName() {
78    return $this->methodName;
79  }
80
81
82  /**
83   * Return the name of the class
84   *
85   * @return string
86   */
87  public function getClassName() {
88    return $this->className;
89  }
90
91  /**
92   * Set the list of arguments to be passed to the constructor
93   *
94   */
95  public function setConstructorArgumentList() {
96    if(count ($this->constructorArgumentNames) > 0) {
97
98      for( $i = 0; $i < count( $this->constructorArgumentNames ); $i++) {
99        $this->constructorArgumentList .= "\$".$this->constructorArgumentNames[$i].", ";
100      }
101      $this->constructorArgumentList = substr($this->constructorArgumentList, 0, -2);
102    }
103  }
104
105
106  /**
107   * Return the list of the arguments to be passed to the constructor
108   *
109   * @return string
110   */
111  public function getConstructorArgumentList() {
112    return $this->constructorArgumentList;
113  }
114
115
116  /**
117   * Set up the source statements that initialise constructor arguments;
118   *
119   */
120  public function setConstructorInitStatements() {
121    if(count ($this->constructorArgumentNames) > 0) {
122      foreach( $this->constructorArgumentNames as $name) {
123        $this->constructorInitialisationStatements[] = "\$".$name." = ";
124      }
125    }
126
127  }
128
129
130  /**
131   * Return the constructor initialisation statements
132   *
133   * @return array
134   */
135  public function getConstructorInitStatements() {
136    return $this->constructorInitialisationStatements;
137  }
138}
139?>