Skip to content

Commit 1695e23

Browse files
committedMar 22, 2020
Add user asserts
1 parent 069cca2 commit 1695e23

13 files changed

+247
-65
lines changed
 

‎README.md

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# crf-resop
2-
=======
32

43
[![pipeline status](https://gitlab.com/mRoca/resop/badges/master/pipeline.svg)](https://gitlab.com/mRoca/resop/commits/master)
54
[![coverage report](https://gitlab.com/mRoca/resop/badges/master/coverage.svg)](https://gitlab.com/mRoca/resop/commits/master)
@@ -26,11 +25,11 @@ make
2625
# On MacOS, run:
2726
make pre-configure
2827
make configure
29-
# Now, update the docker-compose.override.yml and the .env files to match with your host
28+
# Now, update the docker-compose.override.yml file to match with your host
3029
make all
3130
```
3231

33-
Then, go to [http://resop.vcap.me:7500/](http://resop.vcap.me:7500/).
32+
Then, go to [http://resop.vcap.me:7500/](http://resop.vcap.me:7500/), or [https://resop.vcap.me:7583/](https://resop.vcap.me:7583/) for https.
3433

3534
If you want to run a symfony or a php command: `bin/tools <command>`, example: `bin/tools bin/console`
3635

@@ -89,3 +88,11 @@ make test-advanced
8988
make test-unit
9089
make test-unit-coverage
9190
```
91+
92+
## Node
93+
94+
A node container is available in order to run `yarn` commands for `webpack encore`:
95+
96+
```bash
97+
bin/node-tools yarn encore dev
98+
```

‎composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"beberlei/assert": "^3.2",
99
"doctrine/annotations": "^1.8",
1010
"doctrine/doctrine-fixtures-bundle": "^3.3",
11+
"martin-georgiev/postgresql-for-doctrine": "^1.3",
1112
"sensio/framework-extra-bundle": "5.*",
1213
"symfony/console": "5.*",
1314
"symfony/dotenv": "5.*",

‎composer.lock

+67-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎config/packages/doctrine.yaml

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
doctrine:
22
dbal:
33
url: '%env(resolve:DATABASE_URL)%'
4-
5-
# IMPORTANT: You MUST configure your server version,
6-
# either here or in the DATABASE_URL env var (see .env file)
7-
#server_version: '5.7'
4+
server_version: '11'
5+
types:
6+
text[]: MartinGeorgiev\Doctrine\DBAL\Types\TextArray
7+
# Some other types are available in MartinGeorgiev\Doctrine
8+
mapping_types:
9+
text[]: text[]
10+
_text: text[]
811
orm:
912
auto_generate_proxy_classes: true
1013
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
1114
auto_mapping: true
15+
dql:
16+
string_functions:
17+
ALL_OF: MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\All
18+
ANY_OF: MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Any
19+
IN_ARRAY: MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\InArray
1220
mappings:
1321
App:
1422
is_bundle: false

‎src/DataFixtures/ApplicationFixtures.php

+22-6
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
use App\Entity\Organization;
99
use App\Entity\User;
1010
use App\Entity\UserAvailability;
11+
use App\Exception\ConstraintViolationListException;
1112
use Doctrine\Bundle\FixturesBundle\Fixture;
1213
use Doctrine\Persistence\ObjectManager;
1314
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
15+
use Symfony\Component\Validator\Validator\ValidatorInterface;
1416

1517
final class ApplicationFixtures extends Fixture
1618
{
@@ -35,17 +37,20 @@ final class ApplicationFixtures extends Fixture
3537
'UL 20',
3638
];
3739

40+
private ValidatorInterface $validator;
41+
42+
private EncoderFactoryInterface $encoders;
43+
3844
/** @var Organization[] */
3945
private array $organizations = [];
4046

4147
/** @var User[] */
4248
private array $users = [];
4349

44-
private EncoderFactoryInterface $encoders;
45-
46-
public function __construct(EncoderFactoryInterface $encoders)
50+
public function __construct(EncoderFactoryInterface $encoders, ValidatorInterface $validator)
4751
{
4852
$this->encoders = $encoders;
53+
$this->validator = $validator;
4954
}
5055

5156
public function load(ObjectManager $manager): void
@@ -74,7 +79,7 @@ private function loadOrganizations(ObjectManager $manager): void
7479

7580
// Persist all organizations
7681
foreach ($this->organizations as $organization) {
77-
$manager->persist($organization);
82+
$this->validateAndPersist($manager, $organization);
7883
}
7984
}
8085

@@ -114,13 +119,13 @@ private function loadUsers(ObjectManager $manager): void
114119
$user->birthday = '1990-02-28';
115120
$user->occupation = 'Pharmacien';
116121
$user->organizationOccupation = 'Secouriste';
117-
$user->skillSet = ['CI Alpha', 'CI Réseau'];
122+
$user->skillSet = ['ci_alpha', 'ci_reseau'];
118123
$user->vulnerable = true;
119124
$user->fullyEquipped = true;
120125

121126
$this->users[$user->getIdentificationNumber()] = $user;
122127

123-
$manager->persist($user);
128+
$this->validateAndPersist($manager, $user);
124129
}
125130

126131
private function loadUserAvailabilities(ObjectManager $manager): void
@@ -171,4 +176,15 @@ private function addOrganization(Organization $organization): void
171176
{
172177
$this->organizations[$organization->name] = $organization;
173178
}
179+
180+
private function validateAndPersist(ObjectManager $manager, object $object): void
181+
{
182+
$violations = $this->validator->validate($object);
183+
184+
if ($violations->count() > 0) {
185+
throw new ConstraintViolationListException($violations);
186+
}
187+
188+
$manager->persist($object);
189+
}
174190
}

‎src/Entity/AvailabilitableTrait.php

+8-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace App\Entity;
66

77
use Assert\Assertion;
8+
use Symfony\Component\Validator\Constraints as Assert;
89

910
trait AvailabilitableTrait
1011
{
@@ -13,7 +14,7 @@ trait AvailabilitableTrait
1314
* @ORM\GeneratedValue
1415
* @ORM\Column(type="integer", options={"unsigned": true})
1516
*/
16-
public ?int $id;
17+
public ?int $id = null;
1718

1819
/**
1920
* @ORM\Column(type="datetimetz_immutable")
@@ -27,19 +28,21 @@ trait AvailabilitableTrait
2728

2829
/**
2930
* @ORM\Column
31+
* @Assert\NotBlank
32+
* @Assert\Choice(choices=AvailabilityInterface::STATUSES)
3033
*/
31-
public string $status;
34+
public string $status = '';
3235

3336
/**
3437
* @ORM\ManyToOne(targetEntity="App\Entity\User")
3538
* @ORM\JoinColumn(nullable=true)
3639
*/
37-
public ?User $planningAgent;
40+
public ?User $planningAgent = null;
3841

3942
/**
4043
* @ORM\Column(type="datetimetz_immutable", nullable=true)
4144
*/
42-
public ?\DateTimeImmutable $bookedAt;
45+
public ?\DateTimeImmutable $bookedAt = null;
4346

4447
/**
4548
* @ORM\Column(type="datetimetz_immutable")
@@ -49,7 +52,7 @@ trait AvailabilitableTrait
4952
/**
5053
* @ORM\Column(type="datetimetz_immutable", nullable=true)
5154
*/
52-
public ?\DateTimeImmutable $updatedAt;
55+
public ?\DateTimeImmutable $updatedAt = null;
5356

5457
public static function createImmutableDateTime(): \DateTimeImmutable
5558
{
@@ -63,8 +66,6 @@ public static function createImmutableDateTime(): \DateTimeImmutable
6366

6467
private function initialize(?int $id, \DateTimeImmutable $startTime, \DateTimeImmutable $endTime, string $status = self::STATUS_LOCKED): void
6568
{
66-
Assertion::inArray($status, AvailabilityInterface::STATUSES);
67-
6869
$this->id = $id;
6970
$this->startTime = $startTime;
7071
$this->endTime = $endTime;

‎src/Entity/CommissionableAsset.php

+9-8
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
namespace App\Entity;
66

7-
use Assert\Assertion;
87
use Doctrine\ORM\Mapping as ORM;
8+
use Symfony\Component\Validator\Constraints as Assert;
99

1010
/**
1111
* @ORM\Entity(repositoryClass="App\Repository\CommissionableAssetRepository")
1212
*/
1313
class CommissionableAsset
1414
{
15-
private const TYPES = [
15+
public const TYPES = [
1616
'Véhicule léger' => 'VL',
1717
'Véhicule de premiers secours' => 'VPSP',
1818
];
@@ -22,17 +22,20 @@ class CommissionableAsset
2222
* @ORM\GeneratedValue
2323
* @ORM\Column(type="integer", options={"unsigned": true})
2424
*/
25-
private ?int $id;
25+
private ?int $id = null;
2626

2727
/**
2828
* @ORM\Column
29+
* @Assert\NotBlank
30+
* @Assert\Choice(choices=CommissionableAsset::TYPES)
2931
*/
30-
public string $type;
32+
public string $type = '';
3133

3234
/**
3335
* @ORM\Column
36+
* @Assert\NotBlank
3437
*/
35-
public string $name;
38+
public string $name = '';
3639

3740
/**
3841
* @ORM\ManyToOne(targetEntity="App\Entity\Organization")
@@ -43,16 +46,14 @@ class CommissionableAsset
4346
/**
4447
* @ORM\Column(type="datetimetz_immutable", nullable=true)
4548
*/
46-
public ?\DateTimeImmutable $lastCommissionDate;
49+
public ?\DateTimeImmutable $lastCommissionDate = null;
4750

4851
public function __construct(
4952
?int $id,
5053
Organization $organization,
5154
string $type,
5255
string $name
5356
) {
54-
Assertion::inArray($type, self::TYPES);
55-
5657
$this->id = $id;
5758
$this->organization = $organization;
5859
$this->type = $type;

‎src/Entity/Organization.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Doctrine\ORM\Mapping as ORM;
88
use Symfony\Component\Security\Core\User\UserInterface;
9+
use Symfony\Component\Validator\Constraints as Assert;
910

1011
/**
1112
* @ORM\Table(
@@ -22,12 +23,13 @@ class Organization implements UserInterface
2223
* @ORM\GeneratedValue
2324
* @ORM\Column(type="integer", options={"unsigned": true})
2425
*/
25-
public ?int $id;
26+
public ?int $id = null;
2627

2728
/**
2829
* @ORM\Column
30+
* @Assert\NotBlank
2931
*/
30-
public string $name;
32+
public string $name = '';
3133

3234
/**
3335
* @ORM\Column(nullable=true)
@@ -37,7 +39,7 @@ class Organization implements UserInterface
3739
/**
3840
* @ORM\ManyToOne(targetEntity="App\Entity\Organization")
3941
*/
40-
public ?self $parent;
42+
public ?self $parent = null;
4143

4244
public function __construct(?int $id, string $name, self $parent = null)
4345
{

0 commit comments

Comments
 (0)