ShortCodeBeforeAfterTest

The Contact Form DB ShortCodeBeforeAfterTest class.

Defined (1)

The class is defined in the following location(s).

/phpunit/ShortCodeBeforeAfterTest.php  
  1. class ShortCodeBeforeAfterTest extends PHPUnit_Framework_TestCase { 
  2.  
  3. var $bufferOutput = false; 
  4.  
  5. public function tearDown() { 
  6. if ($this->bufferOutput) { 
  7. ob_flush(); 
  8. ob_end_clean(); 
  9. $this->bufferOutput = false; 
  10.  
  11. public function exportSetup($data) { 
  12. date_default_timezone_set('America/New_York'); 
  13. $mock = new MockQueryResultIterator($data); 
  14. CFDBQueryResultIteratorFactory::getInstance()->setQueryResultsIteratorMock($mock); 
  15.  
  16. global $wpdb; 
  17. $wpdb = new WPDB_Mock; 
  18.  
  19. $fields = array(); 
  20. foreach (array_keys($data[0]) as $key) { 
  21. $fields[] = (object)array('field_name' => $key); 
  22. $wpdb->getResultReturnVal = $fields; 
  23. $this->bufferOutput = true; 
  24.  
  25.  
  26. public function dataProvider() { 
  27. $data = array(); 
  28.  
  29. // [cfdb-html] 
  30. $data[] = array('[cfdb-html]: no before, no after',  
  31. '<p>To: ${first-name} ${last-name}</p>',  
  32. new ExportToHtmlTemplate(),  
  33. array(),  
  34. '<p>To: John Doe</p>' . 
  35. '<p>To: Richard Roe</p>'); 
  36.  
  37. $data[] = array('[cfdb-html]: before, no after',  
  38. '{{BEFORE}}<p><b>Registered Users</b></p>{{/BEFORE}}' . 
  39. '<p>To: ${first-name} ${last-name}</p>',  
  40. new ExportToHtmlTemplate(),  
  41. array(),  
  42. '<p><b>Registered Users</b></p>' . 
  43. '<p>To: John Doe</p>' . 
  44. '<p>To: Richard Roe</p>' 
  45. ); 
  46.  
  47. $data[] = array('[cfdb-html]: no before, after',  
  48. '<p>To: ${first-name} ${last-name}</p>' . 
  49. '{{AFTER}}<p><b>Thank you!</b></p>{{/AFTER}}',  
  50. new ExportToHtmlTemplate(),  
  51. array(),  
  52. '<p>To: John Doe</p>' . 
  53. '<p>To: Richard Roe</p>' . 
  54. '<p><b>Thank you!</b></p>' 
  55. ); 
  56.  
  57. $data[] = array('[cfdb-html]: before, after',  
  58. '{{BEFORE}}<p><b>Registered Users</b></p>{{/BEFORE}}' . 
  59. '<p>To: ${first-name} ${last-name}</p>' . 
  60. '{{AFTER}}<p><b>Thank you!</b></p>{{/AFTER}}',  
  61. new ExportToHtmlTemplate(),  
  62. array(),  
  63. '<p><b>Registered Users</b></p>' . 
  64. '<p>To: John Doe</p>' . 
  65. '<p>To: Richard Roe</p>' . 
  66. '<p><b>Thank you!</b></p>' 
  67. ); 
  68.  
  69. // [cfdb-json] 
  70. $jsonOutput = '[ 
  71. {"first-name":"John", "last-name":"Doe"},  
  72. {"first-name":"Richard", "last-name":"Roe"} 
  73. ]'; 
  74. $data[] = array('[cfdb-json]: no before, no after',  
  75. '',  
  76. new ExportToJson(),  
  77. array(),  
  78. $jsonOutput 
  79. ); 
  80.  
  81. $data[] = array('[cfdb-json]: before, no after',  
  82. '{{BEFORE}}Hi!{{/BEFORE}}',  
  83. new ExportToJson(),  
  84. array(),  
  85. 'Hi!' . $jsonOutput 
  86. ); 
  87.  
  88. $data[] = array('[cfdb-json]: no before, after',  
  89. '{{AFTER}}Bye!{{/AFTER}}',  
  90. new ExportToJson(),  
  91. array(),  
  92. $jsonOutput . 'Bye!' 
  93. ); 
  94.  
  95. $data[] = array('[cfdb-json]: before, after',  
  96. '{{BEFORE}}Hi!{{/BEFORE}}{{AFTER}}Bye!{{/AFTER}}',  
  97. new ExportToJson(),  
  98. array(),  
  99. 'Hi!' . $jsonOutput . 'Bye!' 
  100. ); 
  101.  
  102. // [cfdb-value] 
  103. $valueOutput = 'John, Doe, Richard, Roe'; 
  104. $data[] = array('[cfdb-value]: no before, no after',  
  105. '',  
  106. new ExportToValue(),  
  107. array(),  
  108. $valueOutput 
  109. ); 
  110.  
  111. $data[] = array('[cfdb-value]: before, no after',  
  112. '{{BEFORE}}Hi!{{/BEFORE}}',  
  113. new ExportToValue(),  
  114. array(),  
  115. 'Hi!' . $valueOutput 
  116. ); 
  117.  
  118. $data[] = array('[cfdb-value]: no before, after',  
  119. '{{AFTER}}Bye!{{/AFTER}}',  
  120. new ExportToValue(),  
  121. array(),  
  122. $valueOutput . 'Bye!' 
  123. ); 
  124.  
  125. $data[] = array('[cfdb-value]: before, after',  
  126. '{{BEFORE}}Hi!{{/BEFORE}}{{AFTER}}Bye!{{/AFTER}}',  
  127. new ExportToValue(),  
  128. array(),  
  129. 'Hi!' . $valueOutput . 'Bye!' 
  130. ); 
  131.  
  132. // [cfdb-count] 
  133. $valueOutput = '2'; 
  134. $data[] = array('[cfdb-count]: no before, no after',  
  135. '',  
  136. new ExportToValue(),  
  137. array('function' => 'count'),  
  138. $valueOutput 
  139. ); 
  140.  
  141. $data[] = array('[cfdb-count]: before, no after',  
  142. '{{BEFORE}}Hi!{{/BEFORE}}',  
  143. new ExportToValue(),  
  144. array('function' => 'count'),  
  145. 'Hi!' . $valueOutput 
  146. ); 
  147.  
  148. $data[] = array('[cfdb-count]: no before, after',  
  149. '{{AFTER}}Bye!{{/AFTER}}',  
  150. new ExportToValue(),  
  151. array('function' => 'count'),  
  152. $valueOutput . 'Bye!' 
  153. ); 
  154.  
  155. $data[] = array('[cfdb-count]: before, after',  
  156. '{{BEFORE}}Hi!{{/BEFORE}}{{AFTER}}Bye!{{/AFTER}}',  
  157. new ExportToValue(),  
  158. array('function' => 'count'),  
  159. 'Hi!' . $valueOutput . 'Bye!' 
  160. ); 
  161.  
  162. // [cfdb-table] 
  163. $tableOutput = $this->getTableOutput(); 
  164. $data[] = array('[cfdb-table]: no before, no after',  
  165. '',  
  166. new ExportToHtmlTable(),  
  167. array('id' => 'mytable', 'class' => 'myclass'),  
  168. $tableOutput 
  169. ); 
  170.  
  171. $data[] = array('[cfdb-table]: before, no after',  
  172. '{{BEFORE}}Hi!{{/BEFORE}}',  
  173. new ExportToHtmlTable(),  
  174. array('id' => 'mytable', 'class' => 'myclass'),  
  175. 'Hi!' . $tableOutput 
  176. ); 
  177.  
  178. $data[] = array('[cfdb-table]: no before, after',  
  179. '{{AFTER}}Bye!{{/AFTER}}',  
  180. new ExportToHtmlTable(),  
  181. array('id' => 'mytable', 'class' => 'myclass'),  
  182. $tableOutput . 'Bye!' 
  183. ); 
  184.  
  185. $data[] = array('[cfdb-table]: before, after',  
  186. '{{BEFORE}}Hi!{{/BEFORE}}{{AFTER}}Bye!{{/AFTER}}',  
  187. new ExportToHtmlTable(),  
  188. array('id' => 'mytable', 'class' => 'myclass'),  
  189. 'Hi!' . $tableOutput . 'Bye!' 
  190. ); 
  191.  
  192. return $data; 
  193.  
  194. /** 
  195. * @dataProvider dataProvider 
  196. * @param $message string error message 
  197. * @param $content string inner short code content 
  198. * @param $exp ExportBase subclass instance 
  199. * @param $options array export code options 
  200. * @param $expected string expected export output 
  201. */ 
  202. public function test_export($message, $content, $exp, $options, $expected) { 
  203. $data = array( 
  204. array('first-name' => 'John', 'last-name' => 'Doe'),  
  205. array('first-name' => 'Richard', 'last-name' => 'Roe') 
  206. ); 
  207. $this->exportSetup($data); 
  208. $options['content'] = $content; 
  209.  
  210. ob_start(); 
  211. $exp->export('Form', $options); 
  212. $text = ob_get_contents(); 
  213.  
  214. $this->assertEquals($expected, $text, $message); 
  215.  
  216. public function getTableOutput() { 
  217. $data = array( 
  218. array('first-name' => 'John', 'last-name' => 'Doe'),  
  219. array('first-name' => 'Richard', 'last-name' => 'Roe') 
  220. ); 
  221. $this->exportSetup($data); 
  222. $options = array('id' => 'mytable', 'class' => 'myclass'); 
  223. $exp = new ExportToHtmlTable(); 
  224. ob_start(); 
  225. $exp->export('Form', $options); 
  226. $text = ob_get_contents(); 
  227. return $text; 
  228.