From 50827515eb1fe1650bb4f9076c3c20a90ca3682c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Mon, 1 Jul 2024 15:45:40 +0200 Subject: [PATCH] Add tests for introduction (#2664) --- docs/en/reference/introduction.rst | 29 +++++---- docs/en/tutorials/getting-started.rst | 10 +-- tests/Documentation/Introduction/Address.php | 23 +++++++ .../Introduction/BaseEmployee.php | 34 ++++++++++ tests/Documentation/Introduction/Employee.php | 17 +++++ .../Introduction/IntroductionTest.php | 62 +++++++++++++++++++ tests/Documentation/Introduction/Manager.php | 25 ++++++++ tests/Documentation/Introduction/Project.php | 20 ++++++ 8 files changed, 202 insertions(+), 18 deletions(-) create mode 100644 tests/Documentation/Introduction/Address.php create mode 100644 tests/Documentation/Introduction/BaseEmployee.php create mode 100644 tests/Documentation/Introduction/Employee.php create mode 100644 tests/Documentation/Introduction/IntroductionTest.php create mode 100644 tests/Documentation/Introduction/Manager.php create mode 100644 tests/Documentation/Introduction/Project.php diff --git a/docs/en/reference/introduction.rst b/docs/en/reference/introduction.rst index b71626d0a..42b44f2be 100644 --- a/docs/en/reference/introduction.rst +++ b/docs/en/reference/introduction.rst @@ -54,23 +54,23 @@ the features. #[ODM\Id] public string $id; - #[ODM\Field(type: 'int', strategy: 'increment')] + #[ODM\Field(strategy: 'increment')] public int $changes = 0; /** @var string[] */ - #[ODM\Field(type: 'collection')] + #[ODM\Field] public array $notes = []; - #[ODM\Field(type: 'string')] + #[ODM\Field] public string $name; - #[ODM\Field(type: 'int')] + #[ODM\Field] public int $salary; - #[ODM\Field(type: 'date_immutable')] + #[ODM\Field] public DateTimeImmutable $started; - #[ODM\Field(type: 'date')] + #[ODM\Field] public DateTime $left; #[ODM\EmbedOne(targetDocument: Address::class)] @@ -101,16 +101,16 @@ the features. class Address { public function __construct( - #[ODM\Field(type: 'string')] + #[ODM\Field] public string $address, - #[ODM\Field(type: 'string')] + #[ODM\Field] public string $city, - #[ODM\Field(type: 'string')] + #[ODM\Field] public string $state, - #[ODM\Field(type: 'string')] + #[ODM\Field] public string $zipcode, ) { } @@ -123,7 +123,7 @@ the features. public string $id; public function __construct( - #[ODM\Field(type: 'string')] + #[ODM\Field] public string $name, ) { } @@ -160,9 +160,12 @@ Doctrine: $manager->started = new DateTimeImmutable(); $manager->projects->add($project); - /** @var Doctrine\ODM\MongoDB\DocumentManager $dm */ + /** + * Learn how to setup the DocumentManager in the next chapter. + * + * @var Doctrine\ODM\MongoDB\DocumentManager $dm + */ $dm->persist($employee); - $dm->persist($address); $dm->persist($project); $dm->persist($manager); $dm->flush(); diff --git a/docs/en/tutorials/getting-started.rst b/docs/en/tutorials/getting-started.rst index b346e4497..11ef0ccc8 100755 --- a/docs/en/tutorials/getting-started.rst +++ b/docs/en/tutorials/getting-started.rst @@ -95,10 +95,10 @@ You can provide your mapping information in Annotations or XML: #[ODM\Id] public ?string $id = null, - #[ODM\Field(type: 'string')] + #[ODM\Field] public string $name = '', - #[ODM\Field(type: 'string')] + #[ODM\Field] public string $email = '', #[ODM\ReferenceMany(targetDocument: BlogPost::class, cascade: 'all')] @@ -116,13 +116,13 @@ You can provide your mapping information in Annotations or XML: #[ODM\Id] public ?string $id = null, - #[ODM\Field(type: 'string')] + #[ODM\Field] public string $title = '', - #[ODM\Field(type: 'string')] + #[ODM\Field] public string $body = '', - #[ODM\Field(type: 'date_immutable')] + #[ODM\Field] public DateTimeImmutable $createdAt = new DateTimeImmutable(), ) { } diff --git a/tests/Documentation/Introduction/Address.php b/tests/Documentation/Introduction/Address.php new file mode 100644 index 000000000..99573d3b0 --- /dev/null +++ b/tests/Documentation/Introduction/Address.php @@ -0,0 +1,23 @@ +name = 'Employee'; + $employee->salary = 50000; + $employee->started = new DateTimeImmutable(); + $employee->address = new Address( + address: '555 Doctrine Rd.', + city: 'Nashville', + state: 'TN', + zipcode: '37209', + ); + + $project = new Project('New Project'); + $manager = new Manager(); + $manager->name = 'Manager'; + $manager->salary = 100_000; + $manager->started = new DateTimeImmutable(); + $manager->projects->add($project); + + $this->dm->persist($employee); + $this->dm->persist($project); + $this->dm->persist($manager); + $this->dm->flush(); + $this->dm->clear(); + + $employee = $this->dm->find(Employee::class, $employee->id); + $this->assertInstanceOf(Address::class, $employee->address); + + $manager = $this->dm->find(Manager::class, $manager->id); + $this->assertInstanceOf(Project::class, $manager->projects[0]); + + // Update + $newProject = new Project('Another Project'); + $manager->salary = 200_000; + $manager->notes[] = 'Gave user 100k a year raise'; + $manager->changes++; + $manager->projects->add($newProject); + + $this->dm->persist($newProject); + $this->dm->flush(); + $this->dm->clear(); + + $manager = $this->dm->find(Manager::class, $manager->id); + $this->assertSame(200_000, $manager->salary); + $this->assertCount(1, $manager->notes); + $this->assertSame(1, $manager->changes); + $this->assertCount(2, $manager->projects); + } +} diff --git a/tests/Documentation/Introduction/Manager.php b/tests/Documentation/Introduction/Manager.php new file mode 100644 index 000000000..451799858 --- /dev/null +++ b/tests/Documentation/Introduction/Manager.php @@ -0,0 +1,25 @@ + */ + #[ODM\ReferenceMany(targetDocument: Project::class)] + public Collection $projects; + + public function __construct() + { + $this->projects = new ArrayCollection(); + } +} diff --git a/tests/Documentation/Introduction/Project.php b/tests/Documentation/Introduction/Project.php new file mode 100644 index 000000000..c8f587c64 --- /dev/null +++ b/tests/Documentation/Introduction/Project.php @@ -0,0 +1,20 @@ +