TransformEntryFunctionTest

The Contact Form DB TransformEntryFunctionTest class.

Defined (1)

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

/phpunit/TransformEntryFunctionTest.php  
  1. class TransformEntryFunctionTest extends PHPUnit_Framework_TestCase { 
  2.  
  3.  
  4. public function tearDown() { 
  5. CFDBQueryResultIteratorFactory::getInstance()->clearMock(); 
  6. $wpdb = null; 
  7. try { 
  8. ob_flush(); 
  9. ob_end_clean(); 
  10. } catch (Exception $e) { 
  11.  
  12. public function setUp() { 
  13. date_default_timezone_set('America/New_York'); 
  14.  
  15. $str = file_get_contents('TransformEntryFunctionTest.json'); 
  16. $data = json_decode($str, true); 
  17.  
  18. $mock = new MockQueryResultIterator($data); 
  19. CFDBQueryResultIteratorFactory::getInstance()->setQueryResultsIteratorMock($mock); 
  20.  
  21. global $wpdb; 
  22. $wpdb = new WPDB_Mock; 
  23.  
  24. $fields = array(); 
  25. foreach (array_keys($data[0]) as $key) { 
  26. $fields[] = (object)array('field_name' => $key); 
  27. $wpdb->getResultReturnVal = $fields; 
  28.  
  29.  
  30. public function testAddNewFieldInTransformEntry() { 
  31.  
  32. $options = array(); 
  33. $options['trans'] = 'sumToNewField'; 
  34.  
  35. $exp = new ExportToJson(); 
  36. ob_start(); 
  37. $exp->export('form', $options); 
  38. $text = ob_get_contents(); 
  39.  
  40. $stuff = json_decode($text); 
  41. $idx = 0; 
  42. $this->assertTrue(is_array($stuff)); 
  43. $this->assertEquals('1', $stuff[$idx]->a); 
  44. $this->assertEquals('2', $stuff[$idx]->b); 
  45. $this->assertEquals('3', $stuff[$idx]->c); 
  46. ++$idx; 
  47. $this->assertEquals('20', $stuff[$idx]->a); 
  48. $this->assertEquals('30', $stuff[$idx]->b); 
  49. $this->assertEquals('50', $stuff[$idx]->c); 
  50.  
  51. public function testAddNewFieldRemoveOldInTransformEntry() { 
  52.  
  53. $options = array(); 
  54. $options['trans'] = 'sumToNewFieldRemoveB'; 
  55.  
  56. $exp = new ExportToJson(); 
  57. ob_start(); 
  58. $exp->export('form', $options); 
  59. $text = ob_get_contents(); 
  60.  
  61. $stuff = json_decode($text); 
  62. $idx = 0; 
  63. $this->assertTrue(is_array($stuff)); 
  64. $this->assertEquals('1', $stuff[$idx]->a); 
  65. $this->assertFalse(isset($stuff[0]->b)); 
  66. $this->assertEquals('3', $stuff[$idx]->c); 
  67. ++$idx; 
  68. $this->assertEquals('20', $stuff[$idx]->a); 
  69. $this->assertFalse(isset($stuff[0]->b)); 
  70. $this->assertEquals('50', $stuff[$idx]->c); 
  71.  
  72. public function testAddAllNewsFieldInTransformEntry() { 
  73.  
  74. $options = array(); 
  75. $options['trans'] = 'allNewFields'; 
  76.  
  77. $exp = new ExportToJson(); 
  78. ob_start(); 
  79. $exp->export('form', $options); 
  80. $text = ob_get_contents(); 
  81.  
  82. $stuff = json_decode($text); 
  83. $this->assertTrue(is_array($stuff)); 
  84.  
  85. $this->assertEquals('100', $stuff[0]->c); 
  86. $this->assertEquals('200', $stuff[0]->d); 
  87. $this->assertFalse(isset($stuff[0]->a)); 
  88. $this->assertFalse(isset($stuff[0]->b)); 
  89.  
  90. public function testSometimesAddNewFieldInTransformEntry() { 
  91. $options = array(); 
  92. $options['trans'] = 'sometimesNewFields'; 
  93.  
  94. $exp = new ExportToJson(); 
  95. ob_start(); 
  96. $exp->export('form', $options); 
  97. $text = ob_get_contents(); 
  98.  
  99. $stuff = json_decode($text); 
  100. $idx = 0; 
  101. $this->assertTrue(is_array($stuff)); 
  102. $this->assertEquals('1', $stuff[$idx]->a); 
  103. $this->assertEquals('2', $stuff[$idx]->b); 
  104. $this->assertEquals('3', $stuff[$idx]->c); 
  105. ++$idx; 
  106. $this->assertEquals('20', $stuff[$idx]->a); 
  107. $this->assertEquals('30', $stuff[$idx]->b); 
  108. $this->assertEquals('', $stuff[$idx]->c); 
  109.  
  110. public function testSometimesAddNewFieldInTransformEntry2() { 
  111. $options = array(); 
  112. $options['trans'] = 'sometimesNewFields2'; 
  113.  
  114. $exp = new ExportToJson(); 
  115. ob_start(); 
  116. $exp->export('form', $options); 
  117. $text = ob_get_contents(); 
  118.  
  119. $stuff = json_decode($text); 
  120. $idx = 0; 
  121. $this->assertTrue(is_array($stuff)); 
  122. $this->assertEquals('1', $stuff[$idx]->a); 
  123. $this->assertEquals('2', $stuff[$idx]->b); 
  124.  
  125. // todo: should be first following line, not second 
  126. //$this->assertEquals('', $stuff[$idx]->c); 
  127. $this->assertFalse(isset($stuff[$idx]->c)); 
  128.  
  129. ++$idx; 
  130. $this->assertEquals('20', $stuff[$idx]->a); 
  131. $this->assertEquals('30', $stuff[$idx]->b); 
  132.  
  133. // todo: should be first following line, not second 
  134. // $this->assertEquals('50', $stuff[$idx]->c); 
  135. $this->assertFalse(isset($stuff[$idx]->c)); 
  136.