Skip to content

Commit a521767

Browse files
committed
coding challenge 1
1 parent 2aa34d8 commit a521767

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+8110
-1295
lines changed

.env

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# In all environments, the following files are loaded if they exist,
2+
# the latter taking precedence over the former:
3+
#
4+
# * .env contains default values for the environment variables needed by the app
5+
# * .env.local uncommitted file with local overrides
6+
# * .env.$APP_ENV committed environment-specific defaults
7+
# * .env.$APP_ENV.local uncommitted environment-specific overrides
8+
#
9+
# Real environment variables win over .env files.
10+
#
11+
# DO NOT DEFINE PRODUCTION SECRETS IN THIS FILE NOR IN ANY OTHER COMMITTED FILES.
12+
#
13+
# Run "composer dump-env prod" to compile .env files for production use (requires symfony/flex >=1.2).
14+
# https://symfony.com/doc/current/best_practices.html#use-environment-variables-for-infrastructure-configuration
15+
16+
###> symfony/framework-bundle ###
17+
APP_ENV=dev
18+
APP_SECRET=3e770e49c1c79c4dc01aef40b134d86f
19+
###< symfony/framework-bundle ###
20+
21+
###> doctrine/doctrine-bundle ###
22+
# Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
23+
# IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml
24+
#
25+
DATABASE_URL=sqlite:///%kernel.project_dir%/var/data.db
26+
# DATABASE_URL="mysql://db_user:db_password@127.0.0.1:3306/db_name?serverVersion=5.7"
27+
# DATABASE_URL="postgresql://db_user:db_password@127.0.0.1:5432/db_name?serverVersion=13&charset=utf8"
28+
###< doctrine/doctrine-bundle ###

.env.test

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# define your env variables for the test env here
2+
KERNEL_CLASS='App\Kernel'
3+
APP_SECRET='$ecretf0rt3st'
4+
SYMFONY_DEPRECATIONS_HELPER=999999
5+
PANTHER_APP_ENV=panther
6+
PANTHER_ERROR_SCREENSHOT_DIR=./var/error-screenshots
7+
DATABASE_URL=sqlite:///%kernel.project_dir%/var/test.db

.gitignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,19 @@
88
/var/
99
/vendor/
1010
###< symfony/framework-bundle ###
11+
12+
###> friendsofphp/php-cs-fixer ###
13+
/.php-cs-fixer.php
14+
/.php-cs-fixer.cache
15+
###< friendsofphp/php-cs-fixer ###
16+
17+
###> symfony/phpunit-bridge ###
18+
.phpunit
19+
.phpunit.result.cache
20+
/phpunit.xml
21+
###< symfony/phpunit-bridge ###
22+
23+
###> phpunit/phpunit ###
24+
/phpunit.xml
25+
.phpunit.result.cache
26+
###< phpunit/phpunit ###

.php-cs-fixer.dist.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
$finder = (new PhpCsFixer\Finder())
4+
->in(__DIR__)
5+
->exclude('var')
6+
->exclude('vendor')
7+
;
8+
9+
return (new PhpCsFixer\Config())
10+
->setRules([
11+
'@Symfony' => true,
12+
'php_unit_method_casing' => false,
13+
])
14+
->setFinder($finder)
15+
;

CODING-CHALLENGE-1.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# RESTful Webservices in Symfony
2+
3+
## Coding Challenge 1 - Getting started
4+
5+
### Task
6+
7+
- create a controller to list all attendees
8+
- create a controller to view a single attendee
9+
- please check out `Workshop/ListController` and `Workshop/ReadController` as an example
10+
11+
### Solution
12+
13+
- use Symfony's `Route` attribute to configure the route of your controllers
14+
- use HTTP `GET`
15+
- return a JSON representation

Makefile

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
COMPOSER_CMD=composer
2+
SYMFONY_CMD=bin/console
3+
PHP_CS_FIXER_CMD=vendor/bin/php-cs-fixer
4+
PHPUNIT_CMD=vendor/bin/phpunit
5+
6+
help:
7+
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_\-\.]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
8+
9+
dev-init: ## setup application
10+
$(COMPOSER_CMD) install
11+
$(SYMFONY_CMD) doctrine:database:drop --force
12+
$(SYMFONY_CMD) doctrine:database:create
13+
$(SYMFONY_CMD) doctrine:schema:create
14+
$(SYMFONY_CMD) hautelook:fixtures:load -n --no-bundles
15+
16+
fixtures: ## load fixtures
17+
$(SYMFONY_CMD) hautelook:fixtures:load -n -vvv --no-bundles
18+
19+
cache-clear: ## clear symfony cache
20+
$(SYMFONY_CMD) cache:clear
21+
22+
lint-config: ## lint configuration files
23+
$(SYMFONY_CMD) lint:yaml config
24+
25+
lint-container: ## lint container configuration
26+
$(SYMFONY_CMD) lint:container
27+
28+
lint: lint-config lint-container ## lint config/templates/container
29+
30+
cs-check: ## run cs fixer (dry-run)
31+
PHP_CS_FIXER_FUTURE_MODE=1 $(PHP_CS_FIXER_CMD) fix --allow-risky=yes --diff --dry-run
32+
33+
cs-fix: ## run cs fixer
34+
PHP_CS_FIXER_FUTURE_MODE=1 $(PHP_CS_FIXER_CMD) fix --allow-risky=yes
35+
36+
qa: lint cs-check ## run qa tools
37+
38+
tests: ## run tests
39+
$(PHPUNIT_CMD)
40+
41+
tests-update-snapshots: ## run tests and update snapshots
42+
$(PHPUNIT_CMD) -d -update-snapshots
43+
44+
.PHONY: help dev-init fixtures cache-clear lint-config lint-templates lint-container lint cs-check cs-fix qa tests tests-update-snapshots

README.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# RESTful Webservices in Symfony
2+
3+
## [Jan Schädlich](http://janschaedlich.de) | Team Lead PHP Development @ Siemens Mobility Portugal
4+
5+
### Workshop @ SymfonyWorld Online Winter Edition 2021 on 7th of December 2021
6+
7+
> Nowadays RESTful APIs are powering the web and are used in almost every web application.
8+
> In this workshop you will learn the fundamental principles of REST and how you can implement a RESTful application using Symfony.
9+
>
10+
> We will start with the basics of REST and will cover some more advanced topics like Serialization, Content-Negotiation, Security afterwards.
11+
> Besides all the theory you can deepen your learnings on every topic while doing the provided coding challenges.
12+
>
13+
> In addition to an IDE (or text editor) you need a current version of PHP, Composer and Symfony CLI.
14+
15+
Code written for the "RESTful Webservices in Symfony" workshop @ SymfonyWorld Online Winter Edition 2021.
16+
17+
This is example code that is not production-ready. It is intended for studying and learning purposes.
18+
19+
(c) 2021 Jan Schädlich All rights reserved.
20+
21+
## Installation
22+
23+
# checkout the project
24+
$ git clone git@github.com:jschaedl/RESTful-Webservices-in-Symfony.git
25+
26+
# initialize dev environment
27+
$ make dev-init
28+
29+
# start webserver
30+
$ symfony serve -d
31+
32+
## Tools
33+
34+
- Symfony Binary: https://symfony.com/download
35+
- Postman App: https://www.getpostman.com/downloads
36+
37+
## Coding Challenge
38+
39+
For a 2-day conference, workshops and participants must be recorded.
40+
Each workshop last one day.
41+
All workshops have a participation limit of 25 people.
42+
Each attendee can only take part in one workshop per day.
43+
44+
We are going to build an API that can be used to organize workshops.
45+
Our API should offer the following basic features:
46+
47+
- List all workshops
48+
- Read a single workshop
49+
- Create a workshop
50+
- Update a workshop
51+
- Delete a workshop
52+
- List all attendees
53+
- Read a single attendee
54+
- Create an attendee
55+
- Update an attendee
56+
- Delete an attendee
57+
- Add an attendee to a workshop
58+
- Remove an attendee from a workshop
59+
- The listing of workshops and attendees should support pagination
60+
- The API should support JSON and XML
61+
62+
We also want to limit access to our API as follows:
63+
64+
- Listing of all workshops is allowed for everyone
65+
- Reading a single workshop is only allowed for logged-in users with the `ROLE_USER` role
66+
- Creating and updating a workshop is only allowed for logged-in users with the `ROLE_USER` role
67+
- Deleting a workshop is only allowed for logged-in users with the `ROLE_ADMIN` role
68+
- Listing of all attendees is allowed for everyone
69+
- Reading a single attendee is only allowed for logged-in users with the `ROLE_USER` role
70+
- Creating and updating a attendee is only allowed for logged-in users with the `ROLE_USER` role
71+
- Deleting an attendee is only allowed for logged-in users with the `ROLE_ADMIN` role
72+
- Adding/removing an attendee to/from a workshop is only allowed for logged-in users with the `ROLE_USER` role
73+
74+
## Endpoints
75+
76+
### Workshop
77+
78+
HTTP Method | Endpoint
79+
----------- | --------
80+
GET | /workshops
81+
POST | /workshops
82+
GET | /workshops/{workshopId}
83+
PUT | /workshops/{workshopId}
84+
DELETE | /workshops/{workshopId}
85+
POST | /workshops/{workshopId}/attendees/{attendeeId}/add
86+
POST | /workshops/{workshopId}/attendees/{attendeeId}/remove
87+
88+
### Attendee
89+
90+
HTTP Method | Endpoint
91+
----------- | --------
92+
GET | /attendees
93+
POST | /attendees
94+
GET | /attendees/{attendeeId}
95+
PUT | /attendees/{attendeeId}
96+
DELETE | /attendees/{attendeeId}
97+
98+
## Testing
99+
100+
We will add functional tests using a snapshot testing approach to make sure that our endpoints are working correctly.
101+
102+
# run the test suite
103+
$ make phpunit

bin/phpunit

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
if (!file_exists(dirname(__DIR__).'/vendor/symfony/phpunit-bridge/bin/simple-phpunit.php')) {
5+
echo "Unable to find the `simple-phpunit.php` script in `vendor/symfony/phpunit-bridge/bin/`.\n";
6+
exit(1);
7+
}
8+
9+
if (false === getenv('SYMFONY_PHPUNIT_DIR')) {
10+
putenv('SYMFONY_PHPUNIT_DIR='.__DIR__.'/.phpunit');
11+
}
12+
13+
require dirname(__DIR__).'/vendor/symfony/phpunit-bridge/bin/simple-phpunit.php';

composer.json

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,34 @@
44
"minimum-stability": "dev",
55
"prefer-stable": true,
66
"require": {
7-
"php": ">=7.2.5",
7+
"php": ">=8",
88
"ext-ctype": "*",
99
"ext-iconv": "*",
10+
"composer/package-versions-deprecated": "1.11.99.4",
11+
"doctrine/doctrine-bundle": "^2.5",
12+
"doctrine/doctrine-migrations-bundle": "^3.2",
13+
"doctrine/orm": "^2.10",
14+
"ramsey/uuid": "^4.2",
15+
"ramsey/uuid-doctrine": "^1.8",
16+
"sensio/framework-extra-bundle": "^6.2",
1017
"symfony/console": "5.4.*",
18+
"symfony/deprecation-contracts": "^2.1|^3",
1119
"symfony/dotenv": "5.4.*",
1220
"symfony/flex": "^1.17",
1321
"symfony/framework-bundle": "5.4.*",
22+
"symfony/proxy-manager-bridge": "5.4.*",
1423
"symfony/runtime": "5.4.*",
15-
"symfony/yaml": "5.4.*"
24+
"symfony/yaml": "5.4.*",
25+
"webmozart/assert": "^1.10"
1626
},
1727
"require-dev": {
28+
"friendsofphp/php-cs-fixer": "^3.3",
29+
"hautelook/alice-bundle": "^2.9",
30+
"phpunit/phpunit": "^9.5",
31+
"spatie/phpunit-snapshot-assertions": "^4.2",
32+
"symfony/browser-kit": "5.4.*",
33+
"symfony/css-selector": "5.4.*",
34+
"symfony/phpunit-bridge": "^5.2"
1835
},
1936
"config": {
2037
"optimize-autoloader": true,

0 commit comments

Comments
 (0)