Skip to content

Commit

Permalink
Add tests for introduction (#2664)
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN authored Jul 1, 2024
1 parent 43a70e8 commit 5082751
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 18 deletions.
29 changes: 16 additions & 13 deletions docs/en/reference/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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,
) {
}
Expand All @@ -123,7 +123,7 @@ the features.
public string $id;
public function __construct(
#[ODM\Field(type: 'string')]
#[ODM\Field]
public string $name,
) {
}
Expand Down Expand Up @@ -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();
Expand Down
10 changes: 5 additions & 5 deletions docs/en/tutorials/getting-started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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')]
Expand 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(),
) {
}
Expand Down
23 changes: 23 additions & 0 deletions tests/Documentation/Introduction/Address.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Documentation\Introduction;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

#[ODM\EmbeddedDocument]
class Address
{
public function __construct(
#[ODM\Field]
public string $address,
#[ODM\Field]
public string $city,
#[ODM\Field]
public string $state,
#[ODM\Field]
public string $zipcode,
) {
}
}
34 changes: 34 additions & 0 deletions tests/Documentation/Introduction/BaseEmployee.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Documentation\Introduction;

use DateTimeImmutable;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

#[ODM\MappedSuperclass]
abstract class BaseEmployee
{
#[ODM\Field(strategy: 'increment')]
public int $changes = 0;

/** @var string[] */
#[ODM\Field]
public array $notes = [];

#[ODM\Field]
public string $name;

#[ODM\Field]
public int $salary;

#[ODM\Field]
public DateTimeImmutable $started;

#[ODM\Field]
public DateTimeImmutable $left;

#[ODM\EmbedOne]
public Address $address;
}
17 changes: 17 additions & 0 deletions tests/Documentation/Introduction/Employee.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Documentation\Introduction;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

#[ODM\Document]
class Employee extends BaseEmployee
{
#[ODM\Id]
public string $id;

#[ODM\ReferenceOne(targetDocument: Manager::class)]
public ?Manager $manager = null;
}
62 changes: 62 additions & 0 deletions tests/Documentation/Introduction/IntroductionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace Documentation\Introduction;

use DateTimeImmutable;
use Doctrine\ODM\MongoDB\Tests\BaseTestCase;

class IntroductionTest extends BaseTestCase
{
public function testIntroduction(): void
{
// Insert
$employee = new Employee();
$employee->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);
}
}
25 changes: 25 additions & 0 deletions tests/Documentation/Introduction/Manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Documentation\Introduction;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

#[ODM\Document]
class Manager extends BaseEmployee
{
#[ODM\Id]
public string $id;

/** @var Collection<Project> */
#[ODM\ReferenceMany(targetDocument: Project::class)]
public Collection $projects;

public function __construct()
{
$this->projects = new ArrayCollection();
}
}
20 changes: 20 additions & 0 deletions tests/Documentation/Introduction/Project.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Documentation\Introduction;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

#[ODM\Document]
class Project
{
#[ODM\Id]
public string $id;

public function __construct(
#[ODM\Field]
public string $name,
) {
}
}

0 comments on commit 5082751

Please sign in to comment.