1
|
|
|
<?php |
2
|
|
|
namespace Tpg\ExtjsBundle\Tests\Command\ORM; |
3
|
|
|
|
4
|
|
|
use Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand; |
5
|
|
|
use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand; |
6
|
|
|
use Doctrine\Bundle\DoctrineBundle\Command\Proxy\CreateSchemaDoctrineCommand; |
7
|
|
|
use Doctrine\ORM\EntityManager; |
8
|
|
|
use FOS\RestBundle\Routing\Loader\RestYamlCollectionLoader; |
9
|
|
|
|
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application; |
11
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
12
|
|
|
use Symfony\Component\Routing\Router; |
13
|
|
|
use Test\TestBundle\Entity\Car; |
14
|
|
|
use Symfony\Bundle\FrameworkBundle\Client; |
15
|
|
|
use Tpg\ExtjsBundle\Tests\Command\BaseTestGeneratedRestController as Base; |
16
|
|
|
|
17
|
|
|
class BaseTestGeneratedRestController extends Base { |
18
|
|
|
/** @var Car[] $records */ |
19
|
|
|
protected $records = array(); |
20
|
|
|
/** @var Client */ |
21
|
|
|
protected $client; |
22
|
|
|
|
23
|
|
|
protected function setUp() { |
24
|
|
|
parent::setUp(); |
25
|
|
|
$client = $this->createClient(); |
26
|
|
|
/** @var EntityManager $manager */ |
27
|
|
|
$manager = $client->getContainer()->get("doctrine.orm.default_entity_manager"); |
28
|
|
|
$toyota = new Car(); |
29
|
|
|
$toyota->setName("Toyota") |
30
|
|
|
->setPlateNumber("KJ342"); |
31
|
|
|
$manager->persist($toyota); |
32
|
|
|
$car = new Car(); |
33
|
|
|
$car->setName("Ford") |
34
|
|
|
->setPlateNumber("AA123") |
35
|
|
|
->addRelatedCar($toyota); |
36
|
|
|
$manager->persist($car); |
37
|
|
|
$this->records[] = $car; |
38
|
|
|
$car = new Car(); |
39
|
|
|
$car->setName("Honda") |
40
|
|
|
->setPlateNumber("BB243"); |
41
|
|
|
$manager->persist($car); |
42
|
|
|
$this->records[] = $car; |
43
|
|
|
$this->records[] = $toyota; |
44
|
|
|
$manager->flush(); |
45
|
|
|
/** @var RestYamlCollectionLoader $loader */ |
46
|
|
|
$loader = $client->getContainer()->get("fos_rest.routing.loader.yaml_collection"); |
47
|
|
|
$router = $client->getContainer()->get('router'); |
48
|
|
|
$router->getRouteCollection()->addCollection( |
49
|
|
|
$loader->load(__DIR__.'/../../Fixtures/Test/TestBundle/Resources/config/routing.rest.yml') |
50
|
|
|
); |
51
|
|
|
$this->client = $client; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function tearDown() { |
55
|
|
|
/** @var EntityManager $manager */ |
56
|
|
|
$manager = $this->createClient()->getContainer()->get("doctrine.orm.default_entity_manager"); |
57
|
|
|
$manager->createQueryBuilder()->delete('Test\TestBundle\Entity\Car', 'c')->getQuery()->execute(); |
58
|
|
|
parent::tearDown(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public static function setUpBeforeClass() { |
62
|
|
|
parent::setUpBeforeClass(); |
63
|
|
|
$kernel = new \AppKernel('test', true); |
64
|
|
|
$app = new Application($kernel); |
65
|
|
|
$app->addCommands(array( |
66
|
|
|
new CreateDatabaseDoctrineCommand(), |
67
|
|
|
new CreateSchemaDoctrineCommand(), |
68
|
|
|
)); |
69
|
|
|
$kernel->boot(); |
70
|
|
|
$command = $app->find('doctrine:database:create'); |
71
|
|
|
$commandTester = new CommandTester($command); |
72
|
|
|
$commandTester->execute(array( |
73
|
|
|
'command' => $command->getName(), |
74
|
|
|
)); |
75
|
|
|
$command = $app->find('doctrine:schema:create'); |
76
|
|
|
$commandTester = new CommandTester($command); |
77
|
|
|
$commandTester->execute(array( |
78
|
|
|
'command' => $command->getName(), |
79
|
|
|
)); |
80
|
|
|
$kernel->shutdown(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
View Code Duplication |
public static function tearDownAfterClass() { |
|
|
|
|
84
|
|
|
parent::tearDownAfterClass(); |
85
|
|
|
$kernel = new \AppKernel('test', true); |
86
|
|
|
$app = new Application($kernel); |
87
|
|
|
$app->addCommands(array( |
88
|
|
|
new DropDatabaseDoctrineCommand(), |
89
|
|
|
)); |
90
|
|
|
$kernel->boot(); |
91
|
|
|
$command = $app->find('doctrine:database:drop'); |
92
|
|
|
$commandTester = new CommandTester($command); |
93
|
|
|
$commandTester->execute(array( |
94
|
|
|
'command' => $command->getName(), |
95
|
|
|
'--force' => true, |
96
|
|
|
)); |
97
|
|
|
$kernel->shutdown(); |
98
|
|
|
} |
99
|
|
|
} |
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.