Skip to content

Commit 5ae30d0

Browse files
authored
Add elasticsearch tests and split context tests (#184)
1 parent cfff426 commit 5ae30d0

File tree

19 files changed

+268
-38
lines changed

19 files changed

+268
-38
lines changed

.env

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ BACKOFFICE_DATABASE_USER=root
2525
BACKOFFICE_DATABASE_PASSWORD=
2626

2727
# Elasticsearch
28-
BACKOFFICE_ELASTICSEARCH_HOST=127.0.0.1
28+
BACKOFFICE_ELASTICSEARCH_HOST=codelytv-php_ddd_skeleton-mooc-elastic
2929
BACKOFFICE_ELASTICSEARCH_INDEX_PREFIX=backoffice
3030

3131
# COMMON #

Makefile

+7-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ test: composer-env-file
3131

3232
run-tests: composer-env-file
3333
mkdir -p build/test_results/phpunit
34-
./vendor/bin/phpunit --exclude-group='disabled' --log-junit build/test_results/phpunit/junit.xml tests
34+
./vendor/bin/phpunit --exclude-group='disabled' --log-junit build/test_results/phpunit/junit.xml --testsuite backoffice
35+
./vendor/bin/phpunit --exclude-group='disabled' --log-junit build/test_results/phpunit/junit.xml --testsuite mooc
36+
./vendor/bin/phpunit --exclude-group='disabled' --log-junit build/test_results/phpunit/junit.xml --testsuite shared
3537
./vendor/bin/behat -p mooc_backend --format=progress -v
3638

3739
# 🐳 Docker Compose
@@ -61,7 +63,10 @@ stop-local:
6163
symfony server:stop --dir=apps/mooc/backend/public
6264
symfony server:stop --dir=apps/backoffice/frontend/public
6365
symfony server:stop --dir=apps/backoffice/backend/public
64-
66+
6567
ping-mysql:
6668
@docker exec codelytv-php_ddd_skeleton-mooc-mysql mysqladmin --user=root --password= --host "127.0.0.1" ping --silent
6769

70+
clean-cache:
71+
@rm -rf apps/*/*/var
72+

docker-compose.yml

+12-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,23 @@ services:
55
container_name: codelytv-php_ddd_skeleton-mooc-mysql
66
image: mysql:8
77
ports:
8-
- "3360:3306"
8+
- 3360:3306
99
environment:
1010
- MYSQL_ROOT_PASSWORD=
1111
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
1212
command: ["--default-authentication-plugin=mysql_native_password"]
1313

14+
elasticsearch:
15+
container_name: codelytv-php_ddd_skeleton-mooc-elastic
16+
image: docker.elastic.co/elasticsearch/elasticsearch:6.8.10
17+
ports:
18+
- 9200:9200
19+
- 9300:9300
20+
environment:
21+
- discovery.type=single-node
22+
- xpack.security.enabled=false
23+
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
24+
1425
rabbitmq:
1526
container_name: codelytv-php_ddd_skeleton-rabbitmq
1627
image: 'rabbitmq:3.7-management'
@@ -44,9 +55,6 @@ services:
4455
- "9090:9001"
4556
volumes:
4657
- .:/app:delegated
47-
env_file:
48-
- .env
49-
- .env.local
5058
depends_on:
5159
- mysql
5260
- rabbitmq

phpunit.xml

+8-3
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,19 @@
2424

2525
<php>
2626
<ini name="error_reporting" value="-1" />
27-
<server name="KERNEL_CLASS" value="CodelyTv\Apps\Mooc\Backend\MoocBackendKernel" />
2827
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak" />
2928
<env name="APP_ENV" value="test"/>
3029
</php>
3130

3231
<testsuites>
33-
<testsuite name="Contexts">
34-
<directory>./tests</directory>
32+
<testsuite name="backoffice">
33+
<directory>./tests/Backoffice</directory>
34+
</testsuite>
35+
<testsuite name="mooc">
36+
<directory>./tests/Mooc</directory>
37+
</testsuite>
38+
<testsuite name="shared">
39+
<directory>./tests/Shared</directory>
3540
</testsuite>
3641
</testsuites>
3742
</phpunit>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
services:
2+
# Databases
3+
# @todo this should be from backoffice, no mooc
4+
Doctrine\ORM\EntityManager:
5+
factory: [CodelyTv\Mooc\Shared\Infrastructure\Doctrine\MoocEntityManagerFactory, create]
6+
arguments:
7+
- driver: '%env(MOOC_DATABASE_DRIVER)%'
8+
host: '%env(MOOC_DATABASE_HOST)%'
9+
port: '%env(MOOC_DATABASE_PORT)%'
10+
dbname: '%env(MOOC_DATABASE_NAME)%'
11+
user: '%env(MOOC_DATABASE_USER)%'
12+
password: '%env(MOOC_DATABASE_PASSWORD)%'
13+
- '%env(APP_ENV)%'
14+
tags:
15+
- { name: codely.database_connection }
16+
public: true

src/Mooc/Shared/Infrastructure/Symfony/DependencyInjection/mooc_services.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,3 @@ services:
1717

1818
# Courses
1919
CodelyTv\Mooc\Courses\Domain\CourseRepository: '@CodelyTv\Mooc\Courses\Infrastructure\Persistence\DoctrineCourseRepository'
20-

src/Shared/Infrastructure/Doctrine/DatabaseConnections.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace CodelyTv\Shared\Infrastructure\Doctrine;
66

7-
use CodelyTv\Tests\Shared\Infrastructure\Doctrine\DatabaseCleaner;
7+
use CodelyTv\Tests\Shared\Infrastructure\Doctrine\MySqlDatabaseCleaner;
88
use Doctrine\ORM\EntityManager;
99
use function Lambdish\Phunctional\apply;
1010
use function Lambdish\Phunctional\each;
@@ -25,6 +25,6 @@ public function clear(): void
2525

2626
public function truncate(): void
2727
{
28-
apply(new DatabaseCleaner(), array_values($this->connections));
28+
apply(new MySqlDatabaseCleaner(), array_values($this->connections));
2929
}
3030
}

tests/Backoffice/Courses/BackofficeCoursesModuleInfrastructureTestCase.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@
44

55
namespace CodelyTv\Tests\Backoffice\Courses;
66

7+
use CodelyTv\Backoffice\Courses\Infrastructure\Persistence\ElasticsearchBackofficeCourseRepository;
78
use CodelyTv\Backoffice\Courses\Infrastructure\Persistence\MySqlBackofficeCourseRepository;
8-
use CodelyTv\Tests\Mooc\Shared\Infrastructure\PhpUnit\MoocContextInfrastructureTestCase;
9+
use CodelyTv\Tests\Backoffice\Shared\Infraestructure\PhpUnit\BackofficeContextInfrastructureTestCase;
910
use Doctrine\ORM\EntityManager;
1011

11-
abstract class BackofficeCoursesModuleInfrastructureTestCase extends MoocContextInfrastructureTestCase
12+
abstract class BackofficeCoursesModuleInfrastructureTestCase extends BackofficeContextInfrastructureTestCase
1213
{
13-
protected function repository(): MySqlBackofficeCourseRepository
14+
protected function mySqlRepository(): MySqlBackofficeCourseRepository
1415
{
1516
return new MySqlBackofficeCourseRepository($this->service(EntityManager::class));
1617
}
18+
19+
protected function elasticRepository(): ElasticsearchBackofficeCourseRepository
20+
{
21+
return $this->service(ElasticsearchBackofficeCourseRepository::class);
22+
}
1723
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodelyTv\Tests\Backoffice\Courses\Infrastructure\Persistence;
6+
7+
use CodelyTv\Tests\Backoffice\Courses\BackofficeCoursesModuleInfrastructureTestCase;
8+
use CodelyTv\Tests\Backoffice\Courses\Domain\BackofficeCourseCriteriaMother;
9+
use CodelyTv\Tests\Backoffice\Courses\Domain\BackofficeCourseMother;
10+
use CodelyTv\Tests\Shared\Domain\Criteria\CriteriaMother;
11+
12+
final class ElasticsearchBackofficeCourseRepositoryTest extends BackofficeCoursesModuleInfrastructureTestCase
13+
{
14+
/** @test */
15+
public function it_should_save_a_valid_course(): void
16+
{
17+
$this->elasticRepository()->save(BackofficeCourseMother::random());
18+
}
19+
20+
/** @test */
21+
public function it_should_search_all_existing_courses(): void
22+
{
23+
$existingCourse = BackofficeCourseMother::random();
24+
$anotherExistingCourse = BackofficeCourseMother::random();
25+
$existingCourses = [$existingCourse, $anotherExistingCourse];
26+
27+
$this->elasticRepository()->save($existingCourse);
28+
$this->elasticRepository()->save($anotherExistingCourse);
29+
30+
$this->eventually(fn() => $this->assertSimilar($existingCourses, $this->elasticRepository()->searchAll()));
31+
}
32+
33+
/** @test */
34+
public function it_should_search_all_existing_courses_with_an_empty_criteria(): void
35+
{
36+
$existingCourse = BackofficeCourseMother::random();
37+
$anotherExistingCourse = BackofficeCourseMother::random();
38+
$existingCourses = [$existingCourse, $anotherExistingCourse];
39+
40+
$this->elasticRepository()->save($existingCourse);
41+
$this->elasticRepository()->save($anotherExistingCourse);
42+
43+
$this->eventually(
44+
fn() => $this->assertSimilar(
45+
$existingCourses,
46+
$this->elasticRepository()->matching(CriteriaMother::empty())
47+
)
48+
);
49+
}
50+
51+
/** @test */
52+
public function it_should_filter_by_criteria(): void
53+
{
54+
$dddInPhpCourse = BackofficeCourseMother::withName('DDD en PHP');
55+
$dddInJavaCourse = BackofficeCourseMother::withName('DDD en Java');
56+
$intellijCourse = BackofficeCourseMother::withName('Exprimiendo Intellij');
57+
$dddCourses = [$dddInPhpCourse, $dddInJavaCourse];
58+
59+
$nameContainsDddCriteria = BackofficeCourseCriteriaMother::nameContains('DDD');
60+
61+
$this->elasticRepository()->save($dddInJavaCourse);
62+
$this->elasticRepository()->save($dddInPhpCourse);
63+
$this->elasticRepository()->save($intellijCourse);
64+
65+
$this->eventually(
66+
fn() => $this->assertSimilar($dddCourses, $this->elasticRepository()->matching($nameContainsDddCriteria))
67+
);
68+
}
69+
}

tests/Backoffice/Courses/Infrastructure/Persistence/MySqlBackofficeCourseRepositoryTest.php

+11-11
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ final class MySqlBackofficeCourseRepositoryTest extends BackofficeCoursesModuleI
1414
/** @test */
1515
public function it_should_save_a_valid_course(): void
1616
{
17-
$this->repository()->save(BackofficeCourseMother::random());
17+
$this->mySqlRepository()->save(BackofficeCourseMother::random());
1818
}
1919

2020
/** @test */
@@ -24,10 +24,10 @@ public function it_should_search_all_existing_courses(): void
2424
$anotherExistingCourse = BackofficeCourseMother::random();
2525
$existingCourses = [$existingCourse, $anotherExistingCourse];
2626

27-
$this->repository()->save($existingCourse);
28-
$this->repository()->save($anotherExistingCourse);
27+
$this->mySqlRepository()->save($existingCourse);
28+
$this->mySqlRepository()->save($anotherExistingCourse);
2929

30-
$this->assertSimilar($existingCourses, $this->repository()->searchAll());
30+
$this->assertSimilar($existingCourses, $this->mySqlRepository()->searchAll());
3131
}
3232

3333
/** @test */
@@ -37,11 +37,11 @@ public function it_should_search_all_existing_courses_with_an_empty_criteria():
3737
$anotherExistingCourse = BackofficeCourseMother::random();
3838
$existingCourses = [$existingCourse, $anotherExistingCourse];
3939

40-
$this->repository()->save($existingCourse);
41-
$this->repository()->save($anotherExistingCourse);
40+
$this->mySqlRepository()->save($existingCourse);
41+
$this->mySqlRepository()->save($anotherExistingCourse);
4242
$this->clearUnitOfWork();
4343

44-
$this->assertSimilar($existingCourses, $this->repository()->matching(CriteriaMother::empty()));
44+
$this->assertSimilar($existingCourses, $this->mySqlRepository()->matching(CriteriaMother::empty()));
4545
}
4646

4747
/** @test */
@@ -54,11 +54,11 @@ public function it_should_filter_by_criteria(): void
5454

5555
$nameContainsDddCriteria = BackofficeCourseCriteriaMother::nameContains('DDD');
5656

57-
$this->repository()->save($dddInJavaCourse);
58-
$this->repository()->save($dddInPhpCourse);
59-
$this->repository()->save($intellijCourse);
57+
$this->mySqlRepository()->save($dddInJavaCourse);
58+
$this->mySqlRepository()->save($dddInPhpCourse);
59+
$this->mySqlRepository()->save($intellijCourse);
6060
$this->clearUnitOfWork();
6161

62-
$this->assertSimilar($dddCourses, $this->repository()->matching($nameContainsDddCriteria));
62+
$this->assertSimilar($dddCourses, $this->mySqlRepository()->matching($nameContainsDddCriteria));
6363
}
6464
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodelyTv\Tests\Backoffice\Shared\Infraestructure\PhpUnit;
6+
7+
use CodelyTv\Apps\Backoffice\Backend\BackofficeBackendKernel;
8+
use CodelyTv\Shared\Infrastructure\Elasticsearch\ElasticsearchClient;
9+
use CodelyTv\Tests\Shared\Infrastructure\PhpUnit\InfrastructureTestCase;
10+
use Doctrine\ORM\EntityManager;
11+
12+
abstract class BackofficeContextInfrastructureTestCase extends InfrastructureTestCase
13+
{
14+
protected function setUp(): void
15+
{
16+
parent::setUp();
17+
18+
$arranger = new BackofficeEnvironmentArranger(
19+
$this->service(ElasticsearchClient::class),
20+
$this->service(EntityManager::class)
21+
);
22+
23+
$arranger->arrange();
24+
}
25+
26+
protected function kernelClass(): string
27+
{
28+
return BackofficeBackendKernel::class;
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodelyTv\Tests\Backoffice\Shared\Infraestructure\PhpUnit;
6+
7+
use CodelyTv\Shared\Infrastructure\Elasticsearch\ElasticsearchClient;
8+
use CodelyTv\Tests\Shared\Infrastructure\Arranger\EnvironmentArranger;
9+
use CodelyTv\Tests\Shared\Infrastructure\Doctrine\MySqlDatabaseCleaner;
10+
use CodelyTv\Tests\Shared\Infrastructure\Elastic\ElasticDatabaseCleaner;
11+
use Doctrine\ORM\EntityManager;
12+
use function Lambdish\Phunctional\apply;
13+
14+
final class BackofficeEnvironmentArranger implements EnvironmentArranger
15+
{
16+
private ElasticsearchClient $elasticsearchClient;
17+
private EntityManager $entityManager;
18+
19+
public function __construct(ElasticsearchClient $elasticsearchClient, EntityManager $entityManager)
20+
{
21+
$this->elasticsearchClient = $elasticsearchClient;
22+
$this->entityManager = $entityManager;
23+
}
24+
25+
public function arrange(): void
26+
{
27+
apply(new ElasticDatabaseCleaner(), [$this->elasticsearchClient]);
28+
apply(new MySqlDatabaseCleaner(), [$this->entityManager]);
29+
}
30+
31+
public function close(): void
32+
{
33+
}
34+
}

tests/Mooc/Shared/Infrastructure/PhpUnit/MoocContextInfrastructureTestCase.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace CodelyTv\Tests\Mooc\Shared\Infrastructure\PhpUnit;
66

7+
use CodelyTv\Apps\Mooc\Backend\MoocBackendKernel;
78
use CodelyTv\Tests\Shared\Infrastructure\PhpUnit\InfrastructureTestCase;
89
use Doctrine\ORM\EntityManager;
910

@@ -27,8 +28,8 @@ protected function tearDown(): void
2728
parent::tearDown();
2829
}
2930

30-
protected function clearUnitOfWork(): void
31+
protected function kernelClass(): string
3132
{
32-
$this->service(EntityManager::class)->clear();
33+
return MoocBackendKernel::class;
3334
}
3435
}

tests/Mooc/Shared/Infrastructure/PhpUnit/MoocEnvironmentArranger.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace CodelyTv\Tests\Mooc\Shared\Infrastructure\PhpUnit;
66

77
use CodelyTv\Tests\Shared\Infrastructure\Arranger\EnvironmentArranger;
8-
use CodelyTv\Tests\Shared\Infrastructure\Doctrine\DatabaseCleaner;
8+
use CodelyTv\Tests\Shared\Infrastructure\Doctrine\MySqlDatabaseCleaner;
99
use Doctrine\ORM\EntityManager;
1010
use function Lambdish\Phunctional\apply;
1111

@@ -20,7 +20,7 @@ public function __construct(EntityManager $entityManager)
2020

2121
public function arrange(): void
2222
{
23-
apply(new DatabaseCleaner(), [$this->entityManager]);
23+
apply(new MySqlDatabaseCleaner(), [$this->entityManager]);
2424
}
2525

2626
public function close(): void

tests/Shared/Infrastructure/Bus/Event/MySql/MySqlDoctrineEventBusTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace CodelyTv\Tests\Shared\Infrastructure\Bus\Event\MySql;
66

7+
use CodelyTv\Apps\Mooc\Backend\MoocBackendKernel;
78
use CodelyTv\Shared\Domain\Bus\Event\DomainEvent;
89
use CodelyTv\Shared\Infrastructure\Bus\Event\DomainEventMapping;
910
use CodelyTv\Shared\Infrastructure\Bus\Event\MySql\MySqlDoctrineDomainEventsConsumer;
@@ -42,4 +43,9 @@ public function it_should_publish_and_consume_domain_events_from_msql(): void
4243
$eventsToConsume = 2
4344
);
4445
}
46+
47+
protected function kernelClass(): string
48+
{
49+
return MoocBackendKernel::class;
50+
}
4551
}

0 commit comments

Comments
 (0)