-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTestCaseCommand.php
80 lines (68 loc) · 2.52 KB
/
TestCaseCommand.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
<?php
declare(strict_types=1);
namespace App\Command;
use App\Entity\Page;
use App\Repository\BookRepository;
use App\Repository\PageRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand('app:test:case')]
class TestCaseCommand extends Command
{
private SymfonyStyle $io;
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly BookRepository $bookRepository,
private readonly PageRepository $pageRepository,
) {
parent::__construct();
}
protected function initialize(InputInterface $input, OutputInterface $output): void
{
$this->io = new SymfonyStyle($input, $output);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// Remove all entries with odd IDs
foreach ($this->pageRepository->findAll() as $page) {
if ($page->getId() % 2) {
$this->entityManager->remove($page);
}
}
/**
* In doctrine/orm v2.19.5 for above remove() to have effect we need to either:
* - place a flush below remove()
* - or a persist(newPage), not book
* - or remove persist(book)
*/
foreach ($this->bookRepository->findAll() as $book) {
$book->setName('Book from Command');
// The loop here is not required since the same bug would occur with just ONE new entity,
// here we're just creating more of them to showcase a possible real world example
for ($i = 0; $i < 5; $i++) {
$newPage = new Page();
$newPage->setTitle('New Page from Command');
$book->addPage($newPage);
}
$this->entityManager->persist($book);
}
$this->entityManager->flush();
// Retrieve and output the results
$pages = $this->pageRepository->findAll();
$this->io->table(
['ID', 'Book ID', 'Title'],
array_map(function($page) {
return [
'id' => $page->getId(),
'bookId' => $page->getBook()->getId(),
'title' => $page->getTitle()
];
}, $pages)
);
return Command::SUCCESS;
}
}