Skip to content

Commit 555d547

Browse files
authored
chore: change test context with .env (#375)
1 parent 2eb52ed commit 555d547

File tree

3 files changed

+55
-37
lines changed

3 files changed

+55
-37
lines changed

Makefile

+38-24
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,52 @@
11
.PHONY: $(filter-out vendor bin/tools/phpstan/vendor bin/tools/cs-fixer/vendor,$(MAKECMDGOALS))
22

3+
.DEFAULT_GOAL := help
4+
5+
# DB variables
36
MYSQL_URL="mysql://root:root@mysql:3306/zenstruck_foundry?charset=utf8"
47
MONGO_URL="mongodb://mongo:mongo@mongo:27017/mongo?compressors=disabled&gssapiServiceName=mongodb&authSource=mongo"
58

9+
# Default test context variables
10+
USE_FOUNDRY_BUNDLE=1
11+
USE_ORM=1
12+
USE_ODM=1
13+
USE_DAMA_DOCTRINE_TEST_BUNDLE=1
14+
SYMFONY_REQUIRE=6.0.*
15+
16+
# Override test context variables with `.env` file
17+
ifneq (,$(wildcard .env))
18+
include .env
19+
export $(shell sed 's/=.*//' .env)
20+
endif
21+
22+
ifeq ($(USE_DAMA_DOCTRINE_TEST_BUNDLE),1)
23+
PHPUNIT_CONFIG_FILE='phpunit-dama-doctrine.xml.dist'
24+
else
25+
PHPUNIT_CONFIG_FILE='phpunit.xml.dist'
26+
endif
27+
28+
# Define docker executable
629
ifeq ($(shell docker --help | grep "compose"),)
730
DOCKER_COMPOSE_BIN=docker-compose
831
else
932
DOCKER_COMPOSE_BIN=docker compose
1033
endif
1134

35+
# Create special context for CI
1236
INTERACTIVE:=$(shell [ -t 0 ] && echo 1)
1337
ifdef INTERACTIVE
1438
CI='false'
1539
DOCKER_COMPOSE=$(DOCKER_COMPOSE_BIN)
16-
DC_EXEC=$(DOCKER_COMPOSE) exec -e USE_FOUNDRY_BUNDLE=1 -e DATABASE_URL=${MYSQL_URL} -e MONGO_URL=${MONGO_URL}
40+
DC_EXEC=$(DOCKER_COMPOSE) exec -e USE_FOUNDRY_BUNDLE=${USE_FOUNDRY_BUNDLE} -e DATABASE_URL=${MYSQL_URL} -e MONGO_URL=${MONGO_URL}
1741
else
1842
CI='true'
1943
DOCKER_COMPOSE=$(DOCKER_COMPOSE_BIN) -f docker-compose.yaml -f docker-compose.ci.yaml
20-
DC_EXEC=$(DOCKER_COMPOSE) exec -e USE_FOUNDRY_BUNDLE=1 -e DATABASE_URL=${MYSQL_URL} -e MONGO_URL=${MONGO_URL} -T
44+
# CI needs to be ran in no-tty mode
45+
DC_EXEC=$(DOCKER_COMPOSE) exec -e USE_FOUNDRY_BUNDLE=${USE_FOUNDRY_BUNDLE} -e DATABASE_URL=${MYSQL_URL} -e MONGO_URL=${MONGO_URL} -T
2146
endif
2247

2348
DOCKER_PHP=${DC_EXEC} php
2449

25-
.DEFAULT_GOAL := help
26-
27-
UID = $(shell id -u)
28-
29-
DOCKER_IS_UP = $(shell $(DOCKER_COMPOSE) ps | grep "Up (healthy)")
30-
31-
ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
32-
$(eval $(RUN_ARGS):;@:)
33-
3450
# From inside the containers, docker host ip is different in linux and macos
3551
UNAME_S := $(shell uname -s)
3652
ifeq ($(UNAME_S),Linux)
@@ -43,19 +59,11 @@ endif
4359
help:
4460
@fgrep -h "###" $(MAKEFILE_LIST) | fgrep -v fgrep | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
4561

46-
validate: fixcs sca test-full database-validate-mapping ### Run fixcs, sca, full test suite and validate migrations
47-
48-
test-full: docker-start vendor ### Run full PHPUnit (MySQL + Mongo)
49-
@$(eval filter ?= '.')
50-
@${DC_EXEC} -e USE_ORM=1 -e USE_ODM=1 php vendor/bin/simple-phpunit --configuration phpunit-dama-doctrine.xml.dist --filter=$(filter)
51-
52-
test-mysql: docker-start vendor ### Run PHPUnit with MySQL
53-
@$(eval filter ?= '.')
54-
@${DC_EXEC} -e USE_ORM=1 php vendor/bin/simple-phpunit --configuration phpunit-dama-doctrine.xml.dist --filter=$(filter)
62+
validate: fixcs sca test database-validate-mapping ### Run fixcs, sca, full test suite and validate migrations
5563

56-
test-mongo: docker-start vendor ### Run PHPUnit with Mongo
64+
test: docker-start vendor ### Run PHPUnit tests suite
5765
@$(eval filter ?= '.')
58-
@${DC_EXEC} -e USE_ODM=1 php vendor/bin/simple-phpunit --configuration phpunit.xml.dist --filter=$(filter)
66+
@${DC_EXEC} -e USE_ORM=${USE_ORM} -e USE_ODM=${USE_ODM} php vendor/bin/simple-phpunit --configuration ${PHPUNIT_CONFIG_FILE} --filter=$(filter)
5967

6068
fixcs: docker-start bin/tools/cs-fixer/vendor ### Run PHP-CS-Fixer
6169
@${DOCKER_PHP} bin/tools/cs-fixer/vendor/friendsofphp/php-cs-fixer/php-cs-fixer --no-interaction --diff -v fix
@@ -84,8 +92,11 @@ database-drop-schema: docker-start vendor ### Drop database schema
8492
@${DOCKER_PHP} vendor/bin/doctrine-migrations migrations:sync-metadata-storage # prevents the next command to fail if migrations table does not exist
8593
@${DOCKER_PHP} bin/doctrine dbal:run-sql "TRUNCATE doctrine_migration_versions" --quiet
8694

87-
vendor: composer.json $(wildcard composer.lock)
88-
@${DOCKER_PHP} composer update
95+
vendor: composer.json $(wildcard composer.lock) $(wildcard .env)
96+
@${DC_EXEC} -e SYMFONY_REQUIRE=${SYMFONY_REQUIRE} php composer update --prefer-dist
97+
98+
UID = $(shell id -u)
99+
DOCKER_IS_UP = $(shell $(DOCKER_COMPOSE) ps | grep "Up (healthy)")
89100

90101
docker-start: ### Build and run containers
91102
ifeq ($(DOCKER_IS_UP),)
@@ -104,6 +115,9 @@ docker-stop: ### Stop containers
104115
docker-purge: docker-stop ### Purge containers
105116
@$(DOCKER_COMPOSE) down --volumes
106117

118+
ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
119+
$(eval $(RUN_ARGS):;@:)
120+
107121
composer: ### Run composer command
108122
@${DC_EXEC} php composer $(ARGS)
109123

README.md

+14-13
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,7 @@ Each target will build and start the docker stack and install composer only if n
4545
```shell
4646
$ make help
4747
validate Run fixcs, sca, full test suite and validate migrations
48-
test-full Run full PHPunit (MySQL + Mongo)
49-
test-fast Run PHPUnit with SQLite
50-
test-mysql Run PHPUnit with MySQL
51-
test-postgresql Run PHPUnit with PostgreSQL
52-
test-mongo Run PHPUnit with Mongo
48+
test Run PHPUnit tests suite
5349
fixcs Run PHP-CS-Fixer
5450
sca Run static analysis
5551
database-generate-migration Generate new migration based on mapping in Zenstruck\Foundry\Tests\Fixtures\Entity
@@ -62,16 +58,26 @@ composer Run composer command
6258
clear Start from a fresh install (needed if vendors have already been installed with another php version)
6359
```
6460
65-
You can run each `test-*` target with a special argument `filter`:
61+
You can run each `make test` target with a special argument `filter`:
6662
```shell
67-
$ make test-mysql filter=FactoryTest
63+
$ make test filter=FactoryTest
6864
```
6965
7066
which will use PHPUnit's `--filter` option.
7167
68+
#### Run tests in different environments
69+
70+
You can create a `.env` file to change the context in which tests will execute:
71+
```dotenv
72+
USE_ORM=1
73+
USE_ODM=1
74+
USE_DAMA_DOCTRINE_TEST_BUNDLE=1
75+
SYMFONY_REQUIRE=6.0.*
76+
```
77+
7278
### Change docker's ports
7379
74-
You can create a `.env` file to change the ports used by docker:
80+
You can also add these variables to the `.env` file to change the ports used by docker:
7581
```dotenv
7682
MYSQL_PORT=3307
7783
POSTGRES_PORT=5434
@@ -92,11 +98,6 @@ create a server called `FOUNDRY` in your PHP Remote Debug, with the IDE key `xde
9298
9399
![PhpStorm with xdebug](docs/phpstorm-xdebug-config.png)
94100
95-
### Run tests without docker
96-
97-
If for any reason docker is not available on your computer, the target `make test-fast` will run tests with your local
98-
php version, and sqlite will be used as database. Results may differ from the CI!
99-
100101
## Migrations
101102
102103
Whenever an entity in the fixtures is added or updated a migration must be generated with `make migrations-generate`

docker/Dockerfile

+3
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,7 @@ RUN addgroup --system --gid ${UID} ${USER} \
4747
&& chown -R ${USER}:${USER} /app
4848
USER ${USER}
4949

50+
RUN composer global require --no-progress --no-scripts --no-plugins symfony/flex ; \
51+
composer global config --no-plugins allow-plugins.symfony/flex true
52+
5053
CMD tail -f /dev/null

0 commit comments

Comments
 (0)