|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace MauticPlugin\CustomObjectsBundle\Tests\Functional; |
| 6 | + |
| 7 | +use Mautic\CoreBundle\Test\MauticMysqlTestCase; |
| 8 | +use Mautic\LeadBundle\Entity\Lead; |
| 9 | +use MauticPlugin\CustomObjectsBundle\Entity\CustomItem; |
| 10 | +use MauticPlugin\CustomObjectsBundle\Entity\CustomItemXrefContact; |
| 11 | +use MauticPlugin\CustomObjectsBundle\Entity\CustomObject; |
| 12 | +use MauticPlugin\CustomObjectsBundle\Repository\CustomItemRepository; |
| 13 | +use MauticPlugin\CustomObjectsBundle\Repository\CustomItemXrefContactRepository; |
| 14 | + |
| 15 | +final class CustomApiControllerTest extends MauticMysqlTestCase |
| 16 | +{ |
| 17 | + private ?CustomItemRepository $customItemRepository; |
| 18 | + private ?CustomItemXrefContactRepository $customItemXrefContactRepository; |
| 19 | + private Lead $contact; |
| 20 | + private CustomItem $customItem; |
| 21 | + |
| 22 | + protected function setUp(): void |
| 23 | + { |
| 24 | + $this->configParams['custom_objects_enabled'] = true; |
| 25 | + |
| 26 | + parent::setUp(); |
| 27 | + |
| 28 | + $this->customItemRepository = self::$container->get('custom_item.repository'); |
| 29 | + $this->customItemXrefContactRepository = self::$container->get('custom_item.xref.contact.repository'); |
| 30 | + |
| 31 | + // Create a Contact |
| 32 | + $this->contact = $this->createContact(); |
| 33 | + // Create a Custom Object |
| 34 | + $customObject = $this->createCustomObject('Product', 'Products'); |
| 35 | + // Create a Custom Item |
| 36 | + $this->customItem = $this->createCustomItem($customObject, 'Product 1'); |
| 37 | + // Relate CI with Contact |
| 38 | + $this->createCustomItemXrefContact($this->customItem, $this->contact); |
| 39 | + } |
| 40 | + |
| 41 | + public function testDetachCustomItemFomContact(): void |
| 42 | + { |
| 43 | + $items = $this->customItemRepository->count([]); |
| 44 | + $links = $this->customItemXrefContactRepository->count([]); |
| 45 | + $this->assertEquals(1, $items); |
| 46 | + $this->assertEquals(1, $links); |
| 47 | + |
| 48 | + $this->client->request('DELETE', sprintf('/api/custom/item/%s/unlink/%s', $this->customItem->getId(), $this->contact->getId())); |
| 49 | + $this->assertTrue($this->client->getResponse()->isOk()); |
| 50 | + |
| 51 | + $items = $this->customItemRepository->count([]); |
| 52 | + $links = $this->customItemXrefContactRepository->count([]); |
| 53 | + $this->assertEquals(1, $items); |
| 54 | + $this->assertEquals(0, $links); |
| 55 | + } |
| 56 | + |
| 57 | + public function testDeleteCustomItem(): void |
| 58 | + { |
| 59 | + $items = $this->customItemRepository->count([]); |
| 60 | + $links = $this->customItemXrefContactRepository->count([]); |
| 61 | + $this->assertEquals(1, $items); |
| 62 | + $this->assertEquals(1, $links); |
| 63 | + |
| 64 | + $this->client->request('DELETE', sprintf('/api/custom/item/%s/delete', $this->customItem->getId())); |
| 65 | + $this->assertTrue($this->client->getResponse()->isOk()); |
| 66 | + |
| 67 | + $items = $this->customItemRepository->count([]); |
| 68 | + $links = $this->customItemXrefContactRepository->count([]); |
| 69 | + $this->assertEquals(0, $items); |
| 70 | + $this->assertEquals(0, $links); |
| 71 | + } |
| 72 | + |
| 73 | + private function createCustomObject(string $singular, string $plural): CustomObject |
| 74 | + { |
| 75 | + $customObject = new CustomObject(); |
| 76 | + $customObject->setNameSingular($singular); |
| 77 | + $customObject->setNamePlural($plural); |
| 78 | + $customObject->setAlias(mb_strtolower($plural)); |
| 79 | + $this->em->persist($customObject); |
| 80 | + $this->em->flush(); |
| 81 | + |
| 82 | + return $customObject; |
| 83 | + } |
| 84 | + |
| 85 | + private function createContact(): Lead |
| 86 | + { |
| 87 | + $lead = new Lead(); |
| 88 | + $this->em->persist($lead); |
| 89 | + $this->em->flush(); |
| 90 | + |
| 91 | + return $lead; |
| 92 | + } |
| 93 | + |
| 94 | + private function createCustomItem(CustomObject $customObject, string $name): CustomItem |
| 95 | + { |
| 96 | + $customItem = new CustomItem($customObject); |
| 97 | + $customItem->setName($name); |
| 98 | + $this->em->persist($customItem); |
| 99 | + $this->em->flush(); |
| 100 | + |
| 101 | + return $customItem; |
| 102 | + } |
| 103 | + |
| 104 | + private function createCustomItemXrefContact(CustomItem $customItem, Lead $contact): void |
| 105 | + { |
| 106 | + $customItemXrefContact = new CustomItemXrefContact($customItem, $contact); |
| 107 | + |
| 108 | + $this->em->persist($customItemXrefContact); |
| 109 | + $this->em->flush(); |
| 110 | + } |
| 111 | +} |
0 commit comments