Initial commit: CloudOps infrastructure platform
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\LeadBundle\Tests\Provider;
|
||||
|
||||
use Mautic\LeadBundle\Event\ListFieldChoicesEvent;
|
||||
use Mautic\LeadBundle\Exception\ChoicesNotFoundException;
|
||||
use Mautic\LeadBundle\LeadEvents;
|
||||
use Mautic\LeadBundle\Provider\FieldChoicesProvider;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
|
||||
final class FieldChoicesProviderTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var MockObject|EventDispatcherInterface
|
||||
*/
|
||||
private MockObject $dispatcher;
|
||||
|
||||
private FieldChoicesProvider $provider;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
|
||||
$this->provider = new FieldChoicesProvider($this->dispatcher);
|
||||
}
|
||||
|
||||
public function testGetChoicesForFieldThatDoesNotHaveAnyChoices(): void
|
||||
{
|
||||
$this->dispatcher->expects($this->once())
|
||||
->method('dispatch')
|
||||
->with(
|
||||
$this->callback($this->setSomeChoicesLikeASubscriber()),
|
||||
LeadEvents::COLLECT_FILTER_CHOICES_FOR_LIST_FIELD_TYPE
|
||||
);
|
||||
|
||||
$this->expectException(ChoicesNotFoundException::class);
|
||||
$this->provider->getChoicesForField('text', 'email');
|
||||
}
|
||||
|
||||
public function testGetChoicesForFieldThatHasTypeChoices(): void
|
||||
{
|
||||
$this->dispatcher->expects($this->once())
|
||||
->method('dispatch')
|
||||
->with(
|
||||
$this->callback($this->setSomeChoicesLikeASubscriber()),
|
||||
LeadEvents::COLLECT_FILTER_CHOICES_FOR_LIST_FIELD_TYPE
|
||||
);
|
||||
|
||||
// Calling it twice to ensure the cache is working and the event is triggered only once.
|
||||
$this->provider->getChoicesForField('country', 'country_field_a');
|
||||
$choices = $this->provider->getChoicesForField('country', 'country_field_a');
|
||||
|
||||
$this->assertSame(['Czech Republic' => 'Czech Republic'], $choices);
|
||||
}
|
||||
|
||||
public function testGetChoicesForFieldThatHasAliasChoices(): void
|
||||
{
|
||||
$this->dispatcher->expects($this->once())
|
||||
->method('dispatch')
|
||||
->with(
|
||||
$this->callback($this->setSomeChoicesLikeASubscriber()),
|
||||
LeadEvents::COLLECT_FILTER_CHOICES_FOR_LIST_FIELD_TYPE
|
||||
);
|
||||
|
||||
// Calling it twice to ensure the cache is working and the event is triggered only once.
|
||||
$this->provider->getChoicesForField('select', 'select_a');
|
||||
$choices = $this->provider->getChoicesForField('select', 'select_a');
|
||||
|
||||
$this->assertSame(['Choice A' => 'choice_a'], $choices);
|
||||
}
|
||||
|
||||
private function setSomeChoicesLikeASubscriber(): callable
|
||||
{
|
||||
return function (ListFieldChoicesEvent $event) {
|
||||
$event->setChoicesForFieldAlias(
|
||||
'select_a',
|
||||
['Choice A' => 'choice_a']
|
||||
);
|
||||
|
||||
$event->setChoicesForFieldType(
|
||||
'country',
|
||||
['Czech Republic' => 'Czech Republic']
|
||||
);
|
||||
|
||||
return true;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\LeadBundle\Tests\Provider;
|
||||
|
||||
use Mautic\LeadBundle\Event\LeadListFiltersOperatorsEvent;
|
||||
use Mautic\LeadBundle\LeadEvents;
|
||||
use Mautic\LeadBundle\Provider\FilterOperatorProvider;
|
||||
use Mautic\LeadBundle\Segment\OperatorOptions;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
final class FilterOperatorProviderTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var MockObject|EventDispatcherInterface
|
||||
*/
|
||||
private MockObject $dispatcher;
|
||||
|
||||
/**
|
||||
* @var MockObject|TranslatorInterface
|
||||
*/
|
||||
private MockObject $translator;
|
||||
|
||||
private FilterOperatorProvider $provider;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
|
||||
$this->translator = $this->createMock(TranslatorInterface::class);
|
||||
$this->provider = new FilterOperatorProvider(
|
||||
$this->dispatcher,
|
||||
$this->translator
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetAllOperators(): void
|
||||
{
|
||||
$this->dispatcher->expects($this->once())
|
||||
->method('dispatch')
|
||||
->with(
|
||||
$this->callback(function (LeadListFiltersOperatorsEvent $event) {
|
||||
// Emulate a subscriber.
|
||||
$event->addOperator(
|
||||
OperatorOptions::EQUAL_TO,
|
||||
[
|
||||
'label' => 'equals to',
|
||||
'expr' => 'eq',
|
||||
'negate_expr' => 'neq',
|
||||
]
|
||||
);
|
||||
|
||||
return true;
|
||||
}),
|
||||
LeadEvents::LIST_FILTERS_OPERATORS_ON_GENERATE
|
||||
);
|
||||
|
||||
$this->translator->expects($this->once())
|
||||
->method('trans')
|
||||
->with('equals to')
|
||||
->willReturnArgument(0);
|
||||
|
||||
// Call it for the first time so the class cache would be populated.
|
||||
$this->provider->getAllOperators();
|
||||
|
||||
// Call it for the second time to ensure the cache is used and the event is fired only once.
|
||||
$operators = $this->provider->getAllOperators();
|
||||
|
||||
$this->assertSame([
|
||||
OperatorOptions::EQUAL_TO => [
|
||||
'label' => 'equals to',
|
||||
'expr' => 'eq',
|
||||
'negate_expr' => 'neq',
|
||||
],
|
||||
], $operators);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\LeadBundle\Tests\Provider;
|
||||
|
||||
use Mautic\LeadBundle\Event\FormAdjustmentEvent;
|
||||
use Mautic\LeadBundle\LeadEvents;
|
||||
use Mautic\LeadBundle\Provider\FormAdjustmentsProvider;
|
||||
use Mautic\LeadBundle\Segment\OperatorOptions;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
|
||||
final class FormAdjustmentsProviderTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var MockObject&EventDispatcherInterface
|
||||
*/
|
||||
private MockObject $dispatcher;
|
||||
|
||||
/**
|
||||
* @var MockObject&FormInterface<mixed>
|
||||
*/
|
||||
private MockObject $form;
|
||||
|
||||
private FormAdjustmentsProvider $provider;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
|
||||
$this->form = $this->createMock(FormInterface::class);
|
||||
$this->provider = new FormAdjustmentsProvider($this->dispatcher);
|
||||
}
|
||||
|
||||
public function testAdjustForm(): void
|
||||
{
|
||||
$this->dispatcher->expects($this->once())
|
||||
->method('dispatch')
|
||||
->with(
|
||||
$this->callback(function (FormAdjustmentEvent $event) {
|
||||
$this->assertSame($this->form, $event->getForm());
|
||||
$this->assertSame('email', $event->getFieldAlias());
|
||||
$this->assertSame('lead', $event->getFieldObject());
|
||||
$this->assertSame(OperatorOptions::EQUAL_TO, $event->getOperator());
|
||||
$this->assertSame('text', $event->getFieldType());
|
||||
|
||||
return true;
|
||||
}),
|
||||
LeadEvents::ADJUST_FILTER_FORM_TYPE_FOR_FIELD
|
||||
);
|
||||
|
||||
$this->provider->adjustForm(
|
||||
$this->form,
|
||||
'email',
|
||||
'lead',
|
||||
OperatorOptions::EQUAL_TO,
|
||||
['properties' => ['type' => 'text']]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,199 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\LeadBundle\Tests\Provider;
|
||||
|
||||
use Mautic\LeadBundle\Event\FieldOperatorsEvent;
|
||||
use Mautic\LeadBundle\Event\TypeOperatorsEvent;
|
||||
use Mautic\LeadBundle\LeadEvents;
|
||||
use Mautic\LeadBundle\Provider\FilterOperatorProviderInterface;
|
||||
use Mautic\LeadBundle\Provider\TypeOperatorProvider;
|
||||
use Mautic\LeadBundle\Segment\OperatorOptions;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
|
||||
final class TypeOperatorProviderTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var MockObject|EventDispatcherInterface
|
||||
*/
|
||||
private MockObject $dispatcher;
|
||||
|
||||
/**
|
||||
* @var MockObject|FilterOperatorProviderInterface
|
||||
*/
|
||||
private MockObject $filterOperatorPovider;
|
||||
|
||||
private TypeOperatorProvider $provider;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
|
||||
$this->filterOperatorPovider = $this->createMock(FilterOperatorProviderInterface::class);
|
||||
$this->provider = new TypeOperatorProvider(
|
||||
$this->dispatcher,
|
||||
$this->filterOperatorPovider
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetOperatorsIncluding(): void
|
||||
{
|
||||
$this->filterOperatorPovider->expects($this->any())
|
||||
->method('getAllOperators')
|
||||
->willReturn([
|
||||
OperatorOptions::EQUAL_TO => [
|
||||
'label' => 'equals',
|
||||
'expr' => 'eq',
|
||||
'negagte_expr' => 'neq',
|
||||
],
|
||||
OperatorOptions::NOT_EQUAL_TO => [
|
||||
'label' => 'not equal',
|
||||
'expr' => 'neq',
|
||||
'negagte_expr' => 'eq',
|
||||
],
|
||||
]);
|
||||
|
||||
$this->assertSame(
|
||||
['equals' => OperatorOptions::EQUAL_TO],
|
||||
$this->provider->getOperatorsIncluding([OperatorOptions::EQUAL_TO])
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetOperatorsExcluding(): void
|
||||
{
|
||||
$this->filterOperatorPovider->expects($this->any())
|
||||
->method('getAllOperators')
|
||||
->willReturn([
|
||||
OperatorOptions::EQUAL_TO => [
|
||||
'label' => 'equals',
|
||||
'expr' => 'eq',
|
||||
'negagte_expr' => 'neq',
|
||||
],
|
||||
OperatorOptions::NOT_EQUAL_TO => [
|
||||
'label' => 'not equal',
|
||||
'expr' => 'neq',
|
||||
'negagte_expr' => 'eq',
|
||||
],
|
||||
]);
|
||||
|
||||
$this->assertNotContains(
|
||||
OperatorOptions::EQUAL_TO,
|
||||
$this->provider->getOperatorsExcluding([OperatorOptions::EQUAL_TO])
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetOperatorsForFieldType(): void
|
||||
{
|
||||
$this->filterOperatorPovider->expects($this->any())
|
||||
->method('getAllOperators')
|
||||
->willReturn([
|
||||
OperatorOptions::EQUAL_TO => [
|
||||
'label' => 'equals',
|
||||
'expr' => 'eq',
|
||||
'negagte_expr' => 'neq',
|
||||
],
|
||||
OperatorOptions::NOT_EQUAL_TO => [
|
||||
'label' => 'not equal',
|
||||
'expr' => 'neq',
|
||||
'negagte_expr' => 'eq',
|
||||
],
|
||||
OperatorOptions::INCLUDING_ANY => [
|
||||
'label' => 'in',
|
||||
'expr' => 'in',
|
||||
'negagte_expr' => 'notIn',
|
||||
],
|
||||
]);
|
||||
|
||||
$this->dispatcher->expects($this->once())
|
||||
->method('dispatch')
|
||||
->with(
|
||||
$this->callback(function (TypeOperatorsEvent $event) {
|
||||
// Emulate a subscriber.
|
||||
$event->setOperatorsForFieldType('text', [
|
||||
'include' => [
|
||||
OperatorOptions::EQUAL_TO,
|
||||
OperatorOptions::NOT_EQUAL_TO,
|
||||
],
|
||||
]);
|
||||
|
||||
return true;
|
||||
}),
|
||||
LeadEvents::COLLECT_OPERATORS_FOR_FIELD_TYPE
|
||||
);
|
||||
|
||||
$this->assertSame(
|
||||
[
|
||||
'equals' => OperatorOptions::EQUAL_TO,
|
||||
'not equal' => OperatorOptions::NOT_EQUAL_TO,
|
||||
],
|
||||
$this->provider->getOperatorsForFieldType('text')
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetOperatorsForSpecificField(): void
|
||||
{
|
||||
$this->filterOperatorPovider->expects($this->any())
|
||||
->method('getAllOperators')
|
||||
->willReturn([
|
||||
OperatorOptions::EQUAL_TO => [
|
||||
'label' => 'equals',
|
||||
'expr' => 'eq',
|
||||
'negagte_expr' => 'neq',
|
||||
],
|
||||
OperatorOptions::NOT_EQUAL_TO => [
|
||||
'label' => 'not equal',
|
||||
'expr' => 'neq',
|
||||
'negagte_expr' => 'eq',
|
||||
],
|
||||
OperatorOptions::STARTS_WITH => [
|
||||
'label' => 'starts with',
|
||||
'expr' => 'startsWith',
|
||||
'negagte_expr' => 'notStartsWith',
|
||||
],
|
||||
]);
|
||||
$matcher = $this->exactly(2);
|
||||
|
||||
$this->dispatcher->expects($matcher)
|
||||
->method('dispatch')->willReturnCallback(function (...$parameters) use ($matcher) {
|
||||
if (1 === $matcher->numberOfInvocations()) {
|
||||
$callback = function (TypeOperatorsEvent $event) {
|
||||
// Emulate a subscriber.
|
||||
$event->setOperatorsForFieldType('text', [
|
||||
'include' => [
|
||||
OperatorOptions::EQUAL_TO,
|
||||
OperatorOptions::NOT_EQUAL_TO,
|
||||
],
|
||||
]);
|
||||
};
|
||||
$callback($parameters[0]);
|
||||
$this->assertSame(LeadEvents::COLLECT_OPERATORS_FOR_FIELD_TYPE, $parameters[1]);
|
||||
}
|
||||
if (2 === $matcher->numberOfInvocations()) {
|
||||
$callback = function (FieldOperatorsEvent $event) {
|
||||
// Emulate a subscriber.
|
||||
$this->assertSame('text', $event->getType());
|
||||
$this->assertSame('email', $event->getField());
|
||||
|
||||
// This is the important stuff. The Starts with opearator will be added.
|
||||
$event->addOperator(OperatorOptions::STARTS_WITH);
|
||||
};
|
||||
$callback($parameters[0]);
|
||||
$this->assertSame(LeadEvents::COLLECT_OPERATORS_FOR_FIELD, $parameters[1]);
|
||||
}
|
||||
|
||||
return $parameters[0];
|
||||
});
|
||||
|
||||
$this->assertSame(
|
||||
[
|
||||
'equals' => OperatorOptions::EQUAL_TO,
|
||||
'not equal' => OperatorOptions::NOT_EQUAL_TO,
|
||||
'starts with' => OperatorOptions::STARTS_WITH,
|
||||
],
|
||||
$this->provider->getOperatorsForField('text', 'email')
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user