Licitator 1.0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
6.6 KiB

5 years ago
  1. <?= "<?php\n" ?>
  2. <?php
  3. function getStaticValue($tipo){
  4. switch($tipo){
  5. case 'integer': return 1;break;
  6. case 'text': return "'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'";break;
  7. case 'string': return "'Lorem Ipsum'";break;
  8. case 'boolean': return true;break;
  9. case 'datetime': return "new \DateTime()"; break;
  10. case 'array': return "array()"; break;
  11. default: return "new \\$tipo()";
  12. }
  13. }
  14. function dashesToCamelCase($string, $capitalizeFirstCharacter = false)
  15. {
  16. if(strpos($string, '_')!==false){
  17. $str = str_replace('_', '', ucwords($string, '_'));
  18. }else{
  19. $str = str_replace('.', '', ucwords($string, '.'));
  20. }
  21. if (!$capitalizeFirstCharacter) {
  22. $str = lcfirst($str);
  23. }
  24. return $str;
  25. }
  26. ?>
  27. namespace <?= $namespace; ?>;
  28. use PHPUnit\Framework\TestCase;
  29. use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
  30. use Doctrine\ORM\Tools\SchemaTool;
  31. use App\Entity\<?= $originClass ?>;
  32. <?php
  33. foreach($classes as $class){?>
  34. use <?= $class ?>;
  35. <?php
  36. }
  37. foreach($classesArray as $class){?>
  38. use <?= $class ?>;
  39. <?php
  40. }?>
  41. /**
  42. * @covers \App\Entity\<?= $originClass ?>
  43. */
  44. class <?= $class_name ?> extends KernelTestCase
  45. {
  46. /**
  47. * @var Doctrine\ORM\EntityManager
  48. */
  49. protected $entityManager;
  50. public function setUp(): void
  51. {
  52. // Boot the AppKernel in the test environment and with the debug.
  53. self::bootKernel();
  54. // Store the container and the entity manager in test case properties
  55. $this->entityManager = static::$kernel->getContainer()->get('doctrine.orm.entity_manager');
  56. parent::setUp();
  57. }
  58. /**
  59. <?php foreach($properties as $property){
  60. if($property=='id') continue;
  61. if(!empty($classes[$property])) continue;
  62. if(!empty($classesArray[$property])) continue;
  63. ?>
  64. <?php } ?>
  65. */
  66. public function testCreate<?= $class_name ?>()
  67. {<?php
  68. //dependantClasses must be recursive
  69. foreach($dependantClasses as $class=>$deps){
  70. $variablename=explode('\\',$class)?>
  71. $<?= strtolower(end($variablename)) ?>= new \<?= $class ?>();
  72. <?php
  73. //Create dependant classes
  74. foreach($deps['properties'] as $property){
  75. //Ignore autonumeric
  76. if($property=='id') continue;
  77. if(empty($deps['tipos'][$property])) continue;
  78. if(!empty($deps['clasesArray'][$property])){
  79. $propertyrep=$property;
  80. if(substr($property,-2)=='ia'){
  81. $propertyrep=substr($property,0,-2).'ium';
  82. }
  83. ?>
  84. $<?= strtolower(end($variablename)) ?>->add<?= dashesToCamelCase($propertyrep,true) ?>(<?= getStaticValue(($deps['tipos'][$property])?$deps['tipos'][$property]:null)?>);
  85. <?php
  86. }else{
  87. ?>
  88. $<?= strtolower(end($variablename)) ?>->set<?= dashesToCamelCase($property,true) ?>(<?= getStaticValue(($deps['tipos'][$property])?$deps['tipos'][$property]:null)?>);
  89. <?php }
  90. } ?>
  91. $this->entityManager->persist($<?= strtolower(end($variablename)) ?>);
  92. <?php }?>
  93. $class= new <?= $originClass ?>();
  94. <?php
  95. //Create dependant classes
  96. foreach($properties as $property){
  97. //Ignore autonumeric
  98. if($property=='id') continue;
  99. if(empty($types[$property])) continue;
  100. if(!empty($classesArray[$property])){
  101. $propertyrep=$property;
  102. if(substr($property,-2)=='ia'){
  103. $propertyrep=substr($property,0,-2).'ium';
  104. }
  105. $varname=explode('\\', $types[$property]);
  106. ?>
  107. $class->add<?= dashesToCamelCase($propertyrep,true) ?>($<?= ($types[$property])? strtolower(end($varname)) :null ?>);
  108. <?php
  109. }else{
  110. ?>
  111. $class->set<?= dashesToCamelCase($property,true) ?>(<?= getStaticValue(($types[$property])?$types[$property]:null)?>);
  112. <?php }
  113. } ?>
  114. $this->entityManager->persist($class);
  115. $this->entityManager->flush();
  116. $this->assertIsInt($class->getId());
  117. return $class->getId();
  118. }
  119. /**
  120. * @depends testCreate<?= $class_name ?>
  121. <?php foreach($properties as $property){
  122. if($property=='id') continue;
  123. if(!empty($classes[$property])) continue;
  124. if(!empty($classesArray[$property])) continue;
  125. ?>
  126. <?php } ?>
  127. */
  128. public function testRead<?= $class_name ?>($id)
  129. {
  130. $class=$this->entityManager->getRepository('App:<?= $originClass ?>')
  131. ->findOneById($id);
  132. $this->assertIsObject($class);
  133. return $class;
  134. }
  135. /**
  136. * @depends testRead<?= $class_name ?>
  137. <?php foreach($properties as $property){
  138. if($property=='id') continue;
  139. if(!empty($classes[$property])) continue;
  140. if(!empty($classesArray[$property])) continue;
  141. ?>
  142. <?php } ?>
  143. */
  144. public function testUpdate<?= $class_name ?>($class)
  145. {
  146. <?php
  147. foreach($properties as $property) {
  148. //Ignore autonumeric
  149. if ($property == 'id') continue;
  150. if (empty($types[$property])) continue;
  151. if (!empty($classesArray[$property])) {
  152. $propertyrep = $property;
  153. if (substr($property, -2) == 'ia') {
  154. $propertyrep = substr($property, 0, -2) . 'ium';
  155. }
  156. ?>
  157. $class->add<?= ucfirst($propertyrep) ?>(<?= getStaticValue(($types[$property]) ? $types[$property] : null) ?>);
  158. <?php
  159. } else {
  160. ?>
  161. $class->set<?= ucfirst($property) ?>(<?= getStaticValue(($types[$property]) ? $types[$property] : null) ?>);
  162. <?php
  163. }
  164. }?>
  165. $this->entityManager->persist($class);
  166. $this->entityManager->flush();
  167. $this->assertIsInt($class->getId());
  168. return $class->getId();
  169. }
  170. /**
  171. * @depends testUpdate<?= $class_name ?>
  172. */
  173. public function testDelete<?= $class_name ?>($id)
  174. {
  175. $class = $this->entityManager->getReference('App:<?= $originClass ?>', $id);
  176. // Remove it and flush
  177. $this->entityManager->remove($class);
  178. $this->entityManager->flush();
  179. $class= $this->getRepository('App:<?= $originClass ?>')->findOneById($id);
  180. $this->assetTrue(empty($class));
  181. }
  182. }