summaryrefslogtreecommitdiff
path: root/scripts/dev/generate-phpt/src/testcase/gtTestCase.php
blob: e6fa7affcf2c10c3cea3d801103a7093a99b4706 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php

/**
 * Class for all test cases
 */
abstract class gtTestCase {


  /**
   * The subject of the test, may be either a function (gtFunction) or a method (gtMethod)
   *
   * @var gtMethod or gtFunction
   */
  protected $subject;


  /**
   * Arry of strings containing the test case
   *
   * @var array
   */
  protected $testCase;


  /**
   * Object containing the optional sections that may be added to the test case
   *
   * @var gtOptionalSections
   */
  protected $optionalSections;


  /**
   * Convert test case from array to string
   *
   * @return string
   */
  public function toString() {
    $testCaseString = "";
    foreach($this->testCase as $line) {
      $testCaseString .= $line."\n";
    }
    return $testCaseString;
  }



  /**
   * Returns test case as a array
   *
   * @return array
   */
  public function getTestCase() {
    return $this->testCase;
  }


  /**
   * Construct the common headers (title, file section..) of the test case
   *
   */
  public function ConstructCommonHeaders() {
    $this->testHeader();

    if($this->optionalSections->hasSkipif()) {
      $this->addSkipif();
    }

    if($this->optionalSections->hasIni()) {
      $this->addIni();
    }

    $this->fileOpening();
  }


  /**
   * Construct the common closing statements (clean, done, EXPECTF...)
   *
   */
  public function ConstructCommonClosing() {
    $this->fileClosing();

    if ($this->optionalSections->hasDone()) {
      $this->addDone();
    }

    if ($this->optionalSections->hasClean()) {
      $this->addClean();
    }

    $this->addExpectf();
  }

  /**
   * Start the FILE section of the test
   *
   */
  public function fileOpening() {
    $this->testCase[] = "--FILE--";
    $this->testCase[] = "<?php";
    $this->testCase = gtCodeSnippet::appendBlankLines(2, $this->testCase );
  }


  /**
   * Add constructor argument initialisation to test case
   *
   */
  public function constructorArgInit() {
    $conStatements = $this->subject->getConstructorInitStatements();
    foreach($conStatements as $statement) {
      $this->testCase[] = $statement;
    }
  }


  /**
   * Create instance of class in the test case
   *
   */
  public function constructorCreateInstance() {
    $constructorList = $this->subject->getConstructorArgumentList();
    $this->testCase[] = "\$class = new ".$this->subject->getClassName()."( ".$constructorList." );";
    $this->testCase = gtCodeSnippet::appendBlankLines(2, $this->testCase );
  }


  /**
   * Add function or method initilaisation statements to the test case
   *
   */
  public function argInit() {
    $statements = $this->subject->getInitialisationStatements();
    foreach($statements as $statement) {
      $this->testCase[] = $statement;
    }
    $this->testCase = gtCodeSnippet::appendBlankLines(2, $this->testCase );
  }


  /**
   * Add FILE section closing tag to the test case
   *
   */
  public function fileClosing() {
    $this->testCase[] = "?>";
  }


  /**
   * Add a skipif section to the test case
   *
   */
  public function addSkipif() {
    $this->testCase[] = "--SKIPIF--";
    $this->testCase[] = "<?php";
    if($this->optionalSections->hasSkipifKey()) {
      $key = $this->optionalSections->getSkipifKey();
      //test standard skipif sections
      if($key == 'win') {
        $this->testCase = gtCodeSnippet::append('skipifwin', $this->testCase);
      }
      if($key == 'notwin' ) {
        $this->testCase = gtCodeSnippet::append('skipifnotwin', $this->testCase);
      }

      if($key == '64b' ) {
        $this->testCase = gtCodeSnippet::append('skipif64b', $this->testCase);
      }

      if($key == 'not64b' ) {
        $this->testCase = gtCodeSnippet::append('skipifnot64b', $this->testCase);
      }
    }

    if($this->optionalSections->hasSkipifExt()) {
      $ext = $this->optionalSections->getSkipifExt();
      $this->testCase[] = "if (!extension_loaded('$ext')) die ('skip $ext extension not available in this build');";
    }
    $this->testCase[] = "?>";
  }


  /**
   * Add an INI section to the test case
   *
   */
  public function addIni() {
    $this->testCase[] = "--INI--";
    $this->testCase[] = "";
  }


  /**
   * Add a clean section to the test case
   *
   */
  public function addClean() {
    $this->testCase[] = "--CLEAN--";
    $this->testCase[] = "<?php";
    $this->testCase[] = "?>";
  }


  /**
   * Add a ===DONE=== statement to the test case
   *
   */
  public function addDone() {
    $this->testCase[] = "===DONE===";
  }


  /**
   * Add an EXPECTF section
   *
   */
  public function addExpectf() {
    $this->testCase[] = "--EXPECTF--";
    if ($this->optionalSections->hasDone() ){
      $this->testCase[] = '===DONE===';
    }
  }

  public function getOpt() {
    return $this->optionalSections;
  }
}
?>