1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the CrudGeneratorBundle |
5
|
|
|
* |
6
|
|
|
* It is based/extended from SensioGeneratorBundle |
7
|
|
|
* |
8
|
|
|
* (c) Petko Petkov <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Petkopara\CrudGeneratorBundle\Tests\Generator; |
15
|
|
|
|
16
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
17
|
|
|
use Petkopara\CrudGeneratorBundle\Generator\PetkoparaFormGenerator; |
18
|
|
|
use Sensio\Bundle\GeneratorBundle\Tests\Generator\GeneratorTest; |
19
|
|
|
|
20
|
|
|
class PetkoparaFormGeneratorTest extends GeneratorTest |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
View Code Duplication |
public function testGenerate() |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
$this->generateForm(false); |
26
|
|
|
$this->assertTrue(file_exists($this->tmpDir . '/Form/PostType.php')); |
27
|
|
|
$content = file_get_contents($this->tmpDir . '/Form/PostType.php'); |
28
|
|
|
$this->assertContains('->add(\'title\')', $content); |
29
|
|
|
$this->assertContains('->add(\'createdAt\')', $content); |
30
|
|
|
$this->assertContains('->add(\'publishedAt\')', $content); |
31
|
|
|
$this->assertContains('->add(\'updatedAt\')', $content); |
32
|
|
|
$this->assertContains('->add(\'parent\')', $content); |
33
|
|
|
$this->assertContains("'class' => 'FooBundle\Entity\Parent'", $content); |
34
|
|
|
$this->assertContains("'choice_label' => 'name'", $content); |
35
|
|
|
$this->assertContains('class PostType extends AbstractType', $content); |
36
|
|
|
$this->assertContains("'data_class' => 'Foo\BarBundle\Entity\Post'", $content); |
37
|
|
|
if (!method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) { |
38
|
|
|
$this->assertContains('getName', $content); |
39
|
|
|
$this->assertContains("'foo_barbundle_post'", $content); |
40
|
|
|
} else { |
41
|
|
|
$this->assertNotContains('getName', $content); |
42
|
|
|
$this->assertNotContains("'foo_barbundle_post'", $content); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param boolean $overwrite |
48
|
|
|
*/ |
49
|
|
|
private function generateForm($overwrite) |
50
|
|
|
{ |
51
|
|
|
|
52
|
|
|
$guesser = $this->getMockBuilder('Petkopara\CrudGeneratorBundle\Generator\Guesser\MetadataGuesser') |
53
|
|
|
->setMethods(array('guessChoiceLabelFromClass')) |
54
|
|
|
->disableOriginalConstructor() |
55
|
|
|
->getMock(); |
56
|
|
|
$guesser->expects($this->any())->method('guessChoiceLabelFromClass')->will($this->returnValue('name')); |
57
|
|
|
|
58
|
|
|
$generator = new PetkoparaFormGenerator($guesser); |
59
|
|
|
$generator->setSkeletonDirs(array(__DIR__ . '/../../Resources/skeleton')); |
60
|
|
|
|
61
|
|
|
$bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\BundleInterface')->getMock(); |
62
|
|
|
$bundle->expects($this->any())->method('getPath')->will($this->returnValue($this->tmpDir)); |
63
|
|
|
$bundle->expects($this->any())->method('getNamespace')->will($this->returnValue('Foo\BarBundle')); |
64
|
|
|
|
65
|
|
|
$metadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadataInfo')->disableOriginalConstructor()->getMock(); |
66
|
|
|
$metadata->identifier = array('id'); |
67
|
|
|
$metadata->fieldMappings = array( |
68
|
|
|
'title' => array('type' => 'string'), |
69
|
|
|
'createdAt' => array('type' => 'date'), |
70
|
|
|
'publishedAt' => array('type' => 'time'), |
71
|
|
|
'updatedAt' => array('type' => 'datetime'), |
72
|
|
|
'parent' => array('type' => ClassMetadataInfo::MANY_TO_ONE, 'isOwningSide'=> true ,'targetEntity' => 'FooBundle\Entity\Parent'), |
73
|
|
|
); |
74
|
|
|
$metadata->fieldNames = array( |
75
|
|
|
'title' => 'title', |
76
|
|
|
'createdAt' => 'createdAt', |
77
|
|
|
'publishedAt' => 'publishedAt', |
78
|
|
|
'updatedAt' => 'updatedAt', |
79
|
|
|
'parent' => 'parent', |
80
|
|
|
); |
81
|
|
|
$metadata->associationMappings = $metadata->fieldMappings; |
82
|
|
|
|
83
|
|
|
$generator->generate($bundle, 'Post', $metadata, $overwrite); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.