Skip to content

Commit

Permalink
Merge pull request #169 from loic425/features/improve-infection-scoring
Browse files Browse the repository at this point in the history
Improve infection scoring
  • Loading branch information
loic425 committed Apr 1, 2019
2 parents c6c05a8 + 2eb2f5a commit ef8c511
Show file tree
Hide file tree
Showing 9 changed files with 253 additions and 18 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"friends-of-behat/symfony-extension": "^2.0",
"friends-of-behat/variadic-extension": "^1.1",
"friendsofphp/php-cs-fixer": "^2.13",
"infection/infection": "^0.10.6",
"infection/infection": "^0.12",
"lakion/mink-debug-extension": "^1.2",
"lchrusciel/api-test-case": "^3.1",
"leanphp/phpspec-code-coverage": "^4.2",
Expand Down
100 changes: 84 additions & 16 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions spec/App/Entity/AdminUserSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Entity\AdminUser;
use App\Entity\AdminUserInterface;
use Doctrine\Common\Collections\Collection;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Component\User\Model\User;
Expand All @@ -26,6 +27,21 @@ function it_implements_a_user_interface(): void
$this->shouldImplement(UserInterface::class);
}

function it_has_a_generated_salt_by_default(): void
{
$this->getSalt()->shouldNotReturn(null);
}

function it_initializes_oauth_accounts_collection_by_default(): void
{
$this->getOAuthAccounts()->shouldHaveType(Collection::class);
}

function its_not_enabled_by_default()
{
$this->shouldNotBeEnabled();
}

function it_has_no_first_name_by_default(): void
{
$this->getFirstName()->shouldReturn(null);
Expand Down
9 changes: 9 additions & 0 deletions spec/App/Entity/AppUserSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ function its_customer_is_mutable(CustomerInterface $customer)
$this->getCustomer()->shouldReturn($customer);
}

function it_sets_customer_email(CustomerInterface $customer): void
{
$customer->setEmail('jon@snow.wall')->shouldBeCalled();

$this->setCustomer($customer);

$this->setEmail('jon@snow.wall');
}

function it_returns_customer_email(CustomerInterface $customer): void
{
$customer->getEmail()->willReturn('jon@snow.wall');
Expand Down
14 changes: 14 additions & 0 deletions spec/App/EventListener/CanonicalizerListenerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ function let(CanonicalizerInterface $canonicalizer): void
$this->beConstructedWith($canonicalizer);
}

function it_canonicalize_user_username($canonicalizer, LifecycleEventArgs $event, UserInterface $user): void
{
$event->getEntity()->willReturn($user);
$user->getUsername()->willReturn('testUser');
$user->getEmail()->willReturn('test@email.com');

$user->setUsernameCanonical('testuser')->shouldBeCalled();
$user->setEmailCanonical('test@email.com')->shouldBeCalled();
$canonicalizer->canonicalize('testUser')->willReturn('testuser')->shouldBeCalled();
$canonicalizer->canonicalize('test@email.com')->willReturn('test@email.com')->shouldBeCalled();

$this->canonicalize($event);
}

function it_canonicalize_user_username_on_pre_persist_doctrine_event($canonicalizer, LifecycleEventArgs $event, UserInterface $user): void
{
$event->getEntity()->willReturn($user);
Expand Down
104 changes: 104 additions & 0 deletions spec/App/EventListener/DefaultUsernameORMListenerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,108 @@ function it_does_nothing_when_there_are_other_objects_than_customer(

$this->onFlush($onFlushEventArgs);
}

function it_skips_objects_other_than_customers(
OnFlushEventArgs $onFlushEventArgs,
EntityManager $entityManager,
UnitOfWork $unitOfWork,
\stdClass $stdObject,
Customer $customer,
UserInterface $user,
ClassMetadata $userMetadata
): void {
$user->getUsername()->willReturn('customer+extra@email.com');
$user->getUsernameCanonical()->willReturn('user@email.com');
$customer->getUser()->willReturn($user);
$customer->getEmail()->willReturn('customer+extra@email.com');
$customer->getEmailCanonical()->willReturn('customer@email.com');

$onFlushEventArgs->getEntityManager()->willReturn($entityManager);
$entityManager->getUnitOfWork()->willReturn($unitOfWork);

$unitOfWork->getScheduledEntityInsertions()->willReturn([$stdObject, $customer]);
$unitOfWork->getScheduledEntityUpdates()->willReturn([]);

$user->setUsername('customer+extra@email.com')->shouldBeCalled();
$user->setUsernameCanonical('customer@email.com')->shouldBeCalled();

$entityManager->getClassMetadata(get_class($user->getWrappedObject()))->willReturn($userMetadata);

$unitOfWork->recomputeSingleEntityChangeSet(Argument::cetera())->shouldBeCalledOnce();

$this->onFlush($onFlushEventArgs);
}

function it_skips_customers_without_users_associated(
OnFlushEventArgs $onFlushEventArgs,
EntityManager $entityManager,
UnitOfWork $unitOfWork,
Customer $customerWithoutUser,
Customer $customerWithUser,
UserInterface $user,
ClassMetadata $userMetadata
): void {
$customerWithoutUser->getUser()->willReturn(null);

$user->getUsername()->willReturn('customer+extra@email.com');
$user->getUsernameCanonical()->willReturn('user@email.com');
$customerWithUser->getUser()->willReturn($user);
$customerWithUser->getEmail()->willReturn('customer+extra@email.com');
$customerWithUser->getEmailCanonical()->willReturn('customer@email.com');

$onFlushEventArgs->getEntityManager()->willReturn($entityManager);
$entityManager->getUnitOfWork()->willReturn($unitOfWork);

$unitOfWork->getScheduledEntityInsertions()->willReturn([$customerWithoutUser, $customerWithUser]);
$unitOfWork->getScheduledEntityUpdates()->willReturn([]);

$user->setUsername('customer+extra@email.com')->shouldBeCalled();
$user->setUsernameCanonical('customer@email.com')->shouldBeCalled();

$entityManager->getClassMetadata(get_class($user->getWrappedObject()))->willReturn($userMetadata);

$unitOfWork->recomputeSingleEntityChangeSet(Argument::cetera())->shouldBeCalledOnce();

$this->onFlush($onFlushEventArgs);
}

function it_skips_customers_with_same_emails(
OnFlushEventArgs $onFlushEventArgs,
EntityManager $entityManager,
UnitOfWork $unitOfWork,
Customer $customerWithSameEmail,
Customer $customerWithDifferentEmail,
UserInterface $userWithSameEmail,
UserInterface $userWithDifferentEmail,
ClassMetadata $userMetadata
): void {
$onFlushEventArgs->getEntityManager()->willReturn($entityManager);
$entityManager->getUnitOfWork()->willReturn($unitOfWork);

$unitOfWork->getScheduledEntityInsertions()->willReturn([]);
$unitOfWork->getScheduledEntityUpdates()->willReturn([$customerWithSameEmail, $customerWithDifferentEmail]);

$userWithSameEmail->getUsername()->willReturn('customer+extra@email.com');
$userWithSameEmail->getUsernameCanonical()->willReturn('customer@email.com');
$customerWithSameEmail->getUser()->willReturn($userWithSameEmail);
$customerWithSameEmail->getEmail()->willReturn('customer+extra@email.com');
$customerWithSameEmail->getEmailCanonical()->willReturn('customer@email.com');

$userWithDifferentEmail->getUsername()->willReturn('customer+extra@email.com');
$userWithDifferentEmail->getUsernameCanonical()->willReturn('user@email.com');
$customerWithDifferentEmail->getUser()->willReturn($userWithDifferentEmail);
$customerWithDifferentEmail->getEmail()->willReturn('customer+extra@email.com');
$customerWithDifferentEmail->getEmailCanonical()->willReturn('customer@email.com');

$userWithSameEmail->setUsername(Argument::any())->shouldNotBeCalled();
$userWithSameEmail->setUsernameCanonical(Argument::any())->shouldNotBeCalled();

$userWithDifferentEmail->setUsername(Argument::any())->shouldBeCalled();
$userWithDifferentEmail->setUsernameCanonical(Argument::any())->shouldBeCalled();

$entityManager->getClassMetadata(get_class($userWithDifferentEmail->getWrappedObject()))->willReturn($userMetadata);
$unitOfWork->recomputeSingleEntityChangeSet(Argument::cetera())->shouldBeCalledOnce();

$this->onFlush($onFlushEventArgs);
}
}
2 changes: 2 additions & 0 deletions spec/App/Form/EventSubscriber/AddUserFormSubscriberSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
use Sylius\Component\User\Model\UserAwareInterface;
use Sylius\Component\User\Model\UserInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Validator\Constraints\Valid;

final class AddUserFormSubscriberSpec extends ObjectBehavior
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace spec\Sylius\Bundle\CoreBundle\Form\EventSubscriber;
namespace spec\App\Form\EventSubscriber;

use App\Entity\Customer;
use PhpSpec\ObjectBehavior;
Expand Down Expand Up @@ -86,4 +86,23 @@ function it_does_not_set_user_if_customer_with_given_email_has_set_user(

$this->preSubmit($event);
}

function it_does_not_set_user_if_email_is_empty(
FormEvent $event,
FormInterface $form,
Customer $customer,
RepositoryInterface $customerRepository,
Customer $existingCustomer,
AppUser $user
): void {
$event->getForm()->willReturn($form);
$form->getData()->willReturn($customer);
$event->getData()->willReturn(['email' => '']);

$customerRepository->findOneBy(['email' => ''])->shouldNotBeCalled();
$existingCustomer->setUser($user)->shouldNotBeCalled();
$form->setData($existingCustomer)->shouldNotBeCalled();

$this->preSubmit($event);
}
}
3 changes: 3 additions & 0 deletions symfony.lock
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@
"ref": "fe60ce509ef04a3f40da96e3979bc8d9b13b2372"
}
},
"justinrainbow/json-schema": {
"version": "5.2.8"
},
"knplabs/doctrine-behaviors": {
"version": "1.5.0"
},
Expand Down

0 comments on commit ef8c511

Please sign in to comment.