<commit_before><?php

namespace CultuurNet\UDB3\MyOrganizers\ReadModel;

use CultuurNet\UDB3\EventSourcing\DomainMessageBuilder;
use CultuurNet\UDB3\Language;
use CultuurNet\UDB3\Organizer\Events\OrganizerCreated;
use CultuurNet\UDB3\Organizer\Events\OrganizerCreatedWithUniqueWebsite;
use CultuurNet\UDB3\Organizer\Events\OrganizerEvent;
use CultuurNet\UDB3\Organizer\OrganizerProjectedToJSONLD;
use CultuurNet\UDB3\Title;
use ValueObjects\Web\Url;

class ProjectorTest extends \PHPUnit_Framework_TestCase
{
    private const DATETIME = '2018-08-07T12:01:00.034024+00:00';
    private const USER_ID = '1adf21b4-711d-4e33-b9ef-c96843582a56';

    /**
     * @var Projector
     */
    private $projector;

    /**
     * @var RepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
     */
    private $repository;

    /**
     * @var DomainMessageBuilder
     */
    private $domainMessageBuilder;

    /**
     * @inheritdoc
     */
    public function setUp()
    {
        $this->repository = $this->createMock(RepositoryInterface::class);
        $this->projector = new Projector(
            $this->repository
        );

        $this->domainMessageBuilder = (new DomainMessageBuilder())
            ->setRecordedOnFromDateTimeString(self::DATETIME)
            ->setUserId(self::USER_ID);
    }

    /**
     * @test
     */
    public function it_sets_the_readmodel_updated_date_when_organizer_was_projected_to_jsonld()
    {
        $itemId = '2fe1b3e4-45d2-422a-8155-17e271e60315';

        $this->repository->expects($this->once())
            ->method('setUpdateDate')
            ->with(
                $itemId,
                new \DateTime(self::DATETIME)
            );

        $projectedToJSONLD = new OrganizerProjectedToJSONLD(
            $itemId,
            'some-in-this-case-irrelevant-iri'
        );

        $msg = $this->domainMessageBuilder->create($projectedToJSONLD);

        $this->projector->handle($msg);
    }

    public function organizerCreatedEventsDataProvider()
    {
        return [
            'legacy organizer created' => [
                new OrganizerCreated(
                    'dffd8f41-77d7-49b4-832a-cd343bf153e8',
                    new Title('some title'),
                    [],
                    [],
                    [],
                    []
                ),
            ],
            'created with unique website' => [
                new OrganizerCreatedWithUniqueWebsite(
                    'dffd8f41-77d7-49b4-832a-cd343bf153e8',
                    new Language('nl'),
                    Url::fromNative('https://example.com'),
                    new Title('some title')
                ),
            ],
        ];
    }

    /**
     * @dataProvider organizerCreatedEventsDataProvider
     *
     * @test
     */
    public function it_adds_organizer_when_an_organizer_is_created(OrganizerEvent $organizerEvent)
    {
        $msg = $this->domainMessageBuilder->create($organizerEvent);

        $this->repository->expects($this->once())
            ->method('add')
            ->with(
                $organizerEvent->getOrganizerId(),
                self::USER_ID,
                new \DateTime(self::DATETIME)
            );

        $this->projector->handle($msg);
    }
}<commit_msg>III-2711: Test organizer deleted event<commit_after><?php

namespace CultuurNet\UDB3\MyOrganizers\ReadModel;

use CultuurNet\UDB3\EventSourcing\DomainMessageBuilder;
use CultuurNet\UDB3\Language;
use CultuurNet\UDB3\Organizer\Events\OrganizerCreated;
use CultuurNet\UDB3\Organizer\Events\OrganizerCreatedWithUniqueWebsite;
use CultuurNet\UDB3\Organizer\Events\OrganizerDeleted;
use CultuurNet\UDB3\Organizer\Events\OrganizerEvent;
use CultuurNet\UDB3\Organizer\OrganizerProjectedToJSONLD;
use CultuurNet\UDB3\Title;
use ValueObjects\Web\Url;

class ProjectorTest extends \PHPUnit_Framework_TestCase
{
    private const DATETIME = '2018-08-07T12:01:00.034024+00:00';
    private const USER_ID = '1adf21b4-711d-4e33-b9ef-c96843582a56';

    /**
     * @var Projector
     */
    private $projector;

    /**
     * @var RepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
     */
    private $repository;

    /**
     * @var DomainMessageBuilder
     */
    private $domainMessageBuilder;

    /**
     * @inheritdoc
     */
    public function setUp()
    {
        $this->repository = $this->createMock(RepositoryInterface::class);
        $this->projector = new Projector(
            $this->repository
        );

        $this->domainMessageBuilder = (new DomainMessageBuilder())
            ->setRecordedOnFromDateTimeString(self::DATETIME)
            ->setUserId(self::USER_ID);
    }

    /**
     * @test
     */
    public function it_sets_the_readmodel_updated_date_when_organizer_was_projected_to_jsonld()
    {
        $itemId = '2fe1b3e4-45d2-422a-8155-17e271e60315';

        $this->repository->expects($this->once())
            ->method('setUpdateDate')
            ->with(
                $itemId,
                new \DateTime(self::DATETIME)
            );

        $projectedToJSONLD = new OrganizerProjectedToJSONLD(
            $itemId,
            'some-in-this-case-irrelevant-iri'
        );

        $msg = $this->domainMessageBuilder->create($projectedToJSONLD);

        $this->projector->handle($msg);
    }

    public function organizerCreatedEventsDataProvider()
    {
        return [
            'legacy organizer created' => [
                new OrganizerCreated(
                    'dffd8f41-77d7-49b4-832a-cd343bf153e8',
                    new Title('some title'),
                    [],
                    [],
                    [],
                    []
                ),
            ],
            'created with unique website' => [
                new OrganizerCreatedWithUniqueWebsite(
                    'dffd8f41-77d7-49b4-832a-cd343bf153e8',
                    new Language('nl'),
                    Url::fromNative('https://example.com'),
                    new Title('some title')
                ),
            ],
        ];
    }

    /**
     * @dataProvider organizerCreatedEventsDataProvider
     *
     * @test
     */
    public function it_adds_organizer_when_an_organizer_is_created(OrganizerEvent $organizerEvent)
    {
        $msg = $this->domainMessageBuilder->create($organizerEvent);

        $this->repository->expects($this->once())
            ->method('add')
            ->with(
                $organizerEvent->getOrganizerId(),
                self::USER_ID,
                new \DateTime(self::DATETIME)
            );

        $this->projector->handle($msg);
    }

    /**
     * @test
     */
    public function it_removes_organizer_from_readmodel_when_organizer_is_deleted()
    {
        $organizerId = 'my-organizer';
        $organizerDeleted = new OrganizerDeleted($organizerId);

        $this->repository->expects($this->once())
            ->method('delete')
            ->with($organizerId);

        $this->projector->handle(
            $this->domainMessageBuilder->create($organizerDeleted)
        );
    }
}
{'fraction_non_alphanumeric': 0.1167183535382013, 'fraction_numerical': 0.033126585847194814, 'mean_word_length': 2.028169014084507, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 68, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1361030', 'n_tokens_mistral': 2287, 'n_tokens_neox': 2100, 'n_words': 262}