forked from sonata-project/SonataPageBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCloneSiteCommandTest.php
168 lines (131 loc) · 5.03 KB
/
CloneSiteCommandTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
declare(strict_types=1);
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\PageBundle\Tests\Functional\Command;
use Doctrine\ORM\EntityManagerInterface;
use Sonata\PageBundle\Tests\App\Entity\SonataPageBlock;
use Sonata\PageBundle\Tests\App\Entity\SonataPagePage;
use Sonata\PageBundle\Tests\App\Entity\SonataPageSite;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Tester\CommandTester;
final class CloneSiteCommandTest extends KernelTestCase
{
private CommandTester $commandTester;
protected function setUp(): void
{
static::bootKernel();
$this->commandTester = new CommandTester(
(new Application(static::createKernel()))->find('sonata:page:clone-site')
);
}
public function testThrowExceptionOnInvalidSourceId(): void
{
$this->prepareData();
static::expectException(\InvalidArgumentException::class);
static::expectExceptionMessage('Please provide a "--source-id=SITE_ID" option.');
$this->commandTester->execute([]);
}
public function testThrowExceptionOnInvalidDestId(): void
{
static::expectException(\InvalidArgumentException::class);
static::expectExceptionMessage('Please provide a "--dest-id=SITE_ID" option.');
$this->commandTester->execute(['--source-id' => 1]);
}
public function testThrowExceptionOnInvalidPrefix(): void
{
static::expectException(\InvalidArgumentException::class);
static::expectExceptionMessage('Please provide a "--prefix=PREFIX" option.');
$this->commandTester->execute([
'--source-id' => 1,
'--dest-id' => 2,
]);
}
public function testCloneSite(): void
{
$this->prepareData();
static::assertSame(3, $this->countPages());
$this->commandTester->execute([
'--source-id' => 1,
'--dest-id' => 2,
'--prefix' => '[CLONED] ',
]);
static::assertSame(6, $this->countPages());
static::assertStringContainsString('done!', $this->commandTester->getDisplay());
}
public function testCloneOnlyHybridPagesFromSite(): void
{
$this->prepareData();
static::assertSame(3, $this->countPages());
$this->commandTester->execute([
'--source-id' => 1,
'--dest-id' => 2,
'--prefix' => '[CLONED] ',
'--only-hybrid' => true,
]);
static::assertSame(4, $this->countPages());
static::assertStringContainsString('done!', $this->commandTester->getDisplay());
}
/**
* @psalm-suppress UndefinedPropertyFetch
*/
private function prepareData(): void
{
// TODO: Simplify this when dropping support for Symfony 4.
// @phpstan-ignore-next-line
$container = method_exists($this, 'getContainer') ? self::getContainer() : self::$container;
$manager = $container->get('doctrine.orm.entity_manager');
\assert($manager instanceof EntityManagerInterface);
$site = new SonataPageSite();
$site->setName('name');
$site->setHost('localhost');
$site2 = new SonataPageSite();
$site2->setName('cloned_site');
$site2->setHost('sonata-project.org');
$page = new SonataPagePage();
$page->setName('name');
$page->setTemplateCode('default');
$page->setSite($site);
$page2 = new SonataPagePage();
$page2->setName('child_page');
$page2->setTemplateCode('default');
$page2->setParent($page);
$page2->setSite($site);
$page3 = new SonataPagePage();
$page3->setName('hybrid_page');
$page3->setRouteName('random_route');
$page3->setTemplateCode('default');
$page3->setSite($site);
$parentBlock = new SonataPageBlock();
$parentBlock->setType('sonata.page.block.container');
$block = new SonataPageBlock();
$block->setType('sonata.block.service.text');
$page->addBlock($parentBlock);
$parentBlock->addChild($block);
$manager->persist($site);
$manager->persist($site2);
$manager->persist($page);
$manager->persist($page2);
$manager->persist($page3);
$manager->flush();
}
/**
* @psalm-suppress UndefinedPropertyFetch
*/
private function countPages(): int
{
// TODO: Simplify this when dropping support for Symfony 4.
// @phpstan-ignore-next-line
$container = method_exists(static::class, 'getContainer') ? static::getContainer() : static::$container;
$manager = $container->get('doctrine.orm.entity_manager');
\assert($manager instanceof EntityManagerInterface);
return $manager->getRepository(SonataPagePage::class)->count([]);
}
}