Spaces:
No application file
No application file
File size: 6,474 Bytes
d2897cd |
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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
<?php
namespace Mautic\LeadBundle\Tests;
use Doctrine\ORM\EntityManagerInterface;
use Mautic\CoreBundle\Helper\UserHelper;
use Mautic\CoreBundle\Model\NotificationModel;
use Mautic\CoreBundle\ProcessSignal\ProcessSignalService;
use Mautic\CoreBundle\Security\Permissions\CorePermissions;
use Mautic\CoreBundle\Tests\CommonMocks;
use Mautic\LeadBundle\Entity\Import;
use Mautic\LeadBundle\Entity\ImportRepository;
use Mautic\LeadBundle\Entity\LeadEventLogRepository;
use Mautic\LeadBundle\Model\CompanyModel;
use Mautic\LeadBundle\Model\ImportModel;
use Mautic\LeadBundle\Model\LeadModel;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
abstract class StandardImportTestHelper extends CommonMocks
{
protected $eventEntities = [];
protected static $csvPath;
protected static $largeCsvPath;
/**
* @var MockObject&EventDispatcherInterface
*/
protected MockObject $dispatcher;
/**
* @var MockObject&EntityManagerInterface
*/
protected MockObject $entityManager;
protected static $initialList = [
['email', 'firstname', 'lastname'],
['[email protected]', 'John', 'Doe'],
['[email protected]', 'Bad', 'Doe'],
['[email protected]', 'Don', 'Doe'],
[''],
['[email protected]', 'Ella', 'Doe'],
];
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
static::generateSmallCSV();
static::generateLargeCSV();
}
public static function tearDownAfterClass(): void
{
if (file_exists(self::$csvPath)) {
unlink(self::$csvPath);
}
if (file_exists(self::$largeCsvPath)) {
unlink(self::$largeCsvPath);
}
parent::tearDownAfterClass();
}
public static function generateSmallCSV(): void
{
$tmpFile = tempnam(sys_get_temp_dir(), 'mautic_import_test_');
$file = fopen($tmpFile, 'w');
foreach (self::$initialList as $line) {
fputcsv($file, $line);
}
fclose($file);
self::$csvPath = $tmpFile;
}
public static function generateLargeCSV(): void
{
$tmpFile = tempnam(sys_get_temp_dir(), 'mautic_import_large_test_');
$file = fopen($tmpFile, 'w');
fputcsv($file, ['email', 'firstname', 'lastname']);
$counter = 510;
while ($counter) {
fputcsv($file, [uniqid().'@gmail.com', uniqid(), uniqid()]);
--$counter;
}
fclose($file);
self::$largeCsvPath = $tmpFile;
}
public function setUp(): void
{
defined('MAUTIC_ENV') or define('MAUTIC_ENV', 'test');
$this->eventEntities = [];
}
/**
* @return Import&MockObject
*/
protected function initImportEntity(array $methods = null)
{
/** @var Import&MockObject $entity */
$entity = $this->getMockBuilder(Import::class)
->onlyMethods($methods ?? [])
->getMock();
$entity->setFilePath(self::$csvPath)
->setLineCount(count(self::$initialList))
->setHeaders(self::$initialList[0])
->setParserConfig(
[
'batchlimit' => 10,
'delimiter' => ',',
'enclosure' => '"',
'escape' => '/',
]
);
return $entity;
}
/**
* Initialize the ImportModel object.
*
* @return ImportModel
*/
protected function initImportModel(bool $entityManagerOpen = true)
{
$translator = $this->getTranslatorMock();
$pathsHelper = $this->getPathsHelperMock();
$this->entityManager = $this->getEntityManagerMock();
$coreParametersHelper = $this->getCoreParametersHelperMock();
/** @var MockObject&UserHelper */
$userHelper = $this->createMock(UserHelper::class);
/** @var MockObject&LeadEventLogRepository */
$logRepository = $this->createMock(LeadEventLogRepository::class);
/** @var MockObject&ImportRepository */
$importRepository = $this->createMock(ImportRepository::class);
$importRepository->method('getValue')
->willReturn(true);
$this->entityManager->expects($this->any())
->method('getRepository')
->will(
$this->returnValueMap(
[
[\Mautic\LeadBundle\Entity\LeadEventLog::class, $logRepository],
[Import::class, $importRepository],
]
)
);
$this->entityManager->expects($this->any())
->method('isOpen')
->willReturn($entityManagerOpen);
/** @var MockObject&LeadModel $leadModel */
$leadModel = $this->getMockBuilder(LeadModel::class)
->disableOriginalConstructor()
->setConstructorArgs([16 => $this->entityManager])
->getMock();
$leadModel->expects($this->any())
->method('getEventLogRepository')
->willReturn($logRepository);
/** @var MockObject&CompanyModel $companyModel */
$companyModel = $this->getMockBuilder(CompanyModel::class)
->disableOriginalConstructor()
->setConstructorArgs([3 => $this->entityManager])
->getMock();
/** @var MockObject&NotificationModel $notificationModel */
$notificationModel = $this->getMockBuilder(NotificationModel::class)
->disableOriginalConstructor()
->setConstructorArgs([3 => $this->entityManager])
->getMock();
$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
$importModel = new ImportModel(
$pathsHelper,
$leadModel,
$notificationModel,
$coreParametersHelper,
$companyModel,
$this->entityManager,
$this->createMock(CorePermissions::class),
$this->dispatcher,
$this->createMock(UrlGeneratorInterface::class),
$translator,
$userHelper,
$this->createMock(LoggerInterface::class),
new ProcessSignalService()
);
return $importModel;
}
}
|