Skip to content

Commit

Permalink
Expand supported versions of doctrine/annotations (#39)
Browse files Browse the repository at this point in the history
This now supports v1 or v2 of the annotations package. v2 has dropped
support for SimpleAnnotationReader; consequently, a bunch of the tests
needed to be updated with different mappings to handle only the "full"
annotation reader being present in that version.

The default driver now switches based on class presence, still generally
matching Doctrine itself. This should not be a BC break in practice
since updating to that version would have already forced an eqiuivalent
model update.
  • Loading branch information
Firehed committed Jul 17, 2023
1 parent 4a1fda1 commit 3aa43a1
Show file tree
Hide file tree
Showing 15 changed files with 75 additions and 55 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ $driver = $config->getMetadataDriverImpl();
$em = new Mocktrine\InMemoryEntityManager($driver)
```

If a driver is not provided, it will default to the `SimpleAnnotationReader` that's used via `Setup::createAnnotationMetadataConfiguration`.
If a driver is not provided, it will default to either `SimpleAnnotationReader` or `AnnotationReader` that's used via `Setup::createAnnotationMetadataConfiguration`.
The former will be preferred, but the class has been removed in `doctrine/annotations:2.0`; if your local dependencies allow that version then the latter will be used.

It is RECOMMENDED to always explicitly provide a driver, as that best matches Doctrine's own setup behavior.
Future versions of this library may make this required.

## Supported features

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "library",
"require": {
"php": "^7.4 || ^8.0",
"doctrine/annotations": "^1.10",
"doctrine/annotations": "^1.10 || ^2.0",
"doctrine/collections": "^1.6.8",
"doctrine/orm": "^2.9",
"doctrine/persistence": "^1.3 || ^2.0",
Expand Down
15 changes: 13 additions & 2 deletions src/InMemoryEntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Firehed\Mocktrine;

use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\SimpleAnnotationReader;
use Doctrine\ORM\{
Configuration,
Expand Down Expand Up @@ -659,8 +660,18 @@ private static function getDefaultMappingDriver(): MappingDriver
// the file through normal Composer autoloading and avoid having to
// worry about the relative path to the vendor/ directory.
class_exists(DoctrineAnnotations::class);
$reader = new SimpleAnnotationReader();
$reader->addNamespace('Doctrine\ORM\Mapping');
if (class_exists(SimpleAnnotationReader::class)) {
/**
* In Annotations:2.x, SimpleAnnotationReader was removed. This
* re-adds the type info that won't be available in high
* dependencies.
* @var \Doctrine\Common\Annotations\Reader&SimpleAnnotationReader
*/
$reader = new SimpleAnnotationReader();
$reader->addNamespace('Doctrine\ORM\Mapping');
} else {
$reader = new AnnotationReader();
}
self::$defaultMappingDriver = new AnnotationDriver($reader);
}
return self::$defaultMappingDriver;
Expand Down
5 changes: 4 additions & 1 deletion tests/CriteriaEvaluatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
namespace Firehed\Mocktrine;

use DateTimeImmutable;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Firehed\Mocktrine\Entities\GrabBag;

Expand All @@ -22,7 +24,8 @@ class CriteriaEvaluatorTest extends \PHPUnit\Framework\TestCase

public function setUp(): void
{
$em = new InMemoryEntityManager();
$reader = new AnnotationReader();
$em = new InMemoryEntityManager(new AnnotationDriver($reader));
$repo = $em->getRepository(GrabBag::class);
$this->entities = [
new GrabBag(true, 30, 'hello', new DateTimeImmutable()),
Expand Down
16 changes: 8 additions & 8 deletions tests/Entities/GrabBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,41 @@
use Doctrine\ORM\Mapping;

/**
* @Entity
* @Table(name="grab_bags")
* @Mapping\Entity
* @Mapping\Table(name="grab_bags")
*/
#[Mapping\Entity]
#[Mapping\Table(name: 'grab_bags')]
class GrabBag
{
/**
* @Id
* @Column
* @Mapping\Id
* @Mapping\Column
*/
#[Mapping\Id]
#[Mapping\Column]
private int $id;

/**
* @Column(name="bool_field", type="boolean")
* @Mapping\Column(name="bool_field", type="boolean")
*/
#[Mapping\Column]
private bool $boolField;

/**
* @Column(name="float_field", type="float")
* @Mapping\Column(name="float_field", type="float")
*/
#[Mapping\Column]
private float $floatField;

/**
* @Column(name="str_field")
* @Mapping\Column(name="str_field")
*/
#[Mapping\Column]
private string $strField;

/**
* @Column(name="date_field", type="date")
* @Mapping\Column(name="date_field", type="date")
*/
#[Mapping\Column]
private DateTimeInterface $dateField;
Expand Down
4 changes: 2 additions & 2 deletions tests/Entities/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Doctrine\ORM\Mapping;

/**
* @Entity
* @Table(name="groups")
* @Mapping\Entity
* @Mapping\Table(name="groups")
*/
#[Mapping\Entity]
#[Mapping\Table(name: 'groups')]
Expand Down
8 changes: 4 additions & 4 deletions tests/Entities/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
use Doctrine\ORM\Mapping;

/**
* @Entity
* @Table(name="nodes")
* @Mapping\Entity
* @Mapping\Table(name="nodes")
*/
#[Mapping\Entity]
#[Mapping\Table(name: 'nodes')]
class Node
{
/**
* @Id
* @Column
* @Mapping\Id
* @Mapping\Column
* @var string
*/
#[Mapping\Id]
Expand Down
8 changes: 4 additions & 4 deletions tests/Entities/ReadonlyConstructorId.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
use Doctrine\ORM\Mapping;

/**
* @Entity
* @Table(name="readonly_constructor_ids")
* @Mapping\Entity
* @Mapping\Table(name="readonly_constructor_ids")
*/
#[Mapping\Entity]
#[Mapping\Table(name: 'readonly_constructor_ids')]
class ReadonlyConstructorId
{
/**
* @Id
* @Column
* @Mapping\Id
* @Mapping\Column
*/
#[Mapping\Id]
#[Mapping\Column]
Expand Down
10 changes: 5 additions & 5 deletions tests/Entities/ReadonlyGeneratedId.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
use Doctrine\ORM\Mapping;

/**
* @Entity
* @Table(name="readonly_generated_ids")
* @Mapping\Entity
* @Mapping\Table(name="readonly_generated_ids")
*/
#[Mapping\Entity]
#[Mapping\Table(name: 'readonly_generated_ids')]
class ReadonlyGeneratedId
{
/**
* @Id
* @Column
* @GeneratedValue
* @Mapping\Id
* @Mapping\Column
* @Mapping\GeneratedValue
*/
#[Mapping\Id]
#[Mapping\Column]
Expand Down
10 changes: 5 additions & 5 deletions tests/Entities/StringId.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
use Doctrine\ORM\Mapping;

/**
* @Entity
* @Table(name="string_ids")
* @Mapping\Entity
* @Mapping\Table(name="string_ids")
*/
#[Mapping\Entity]
#[Mapping\Table(name: 'string_ids')]
class StringId
{
/**
* @Id
* @Column(type="string")
* @GeneratedValue
* @Mapping\Id
* @Mapping\Column(type="string")
* @Mapping\GeneratedValue
* @var ?string
*/
#[Mapping\Id]
Expand Down
10 changes: 5 additions & 5 deletions tests/Entities/TypedId.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
use Doctrine\ORM\Mapping;

/**
* @Entity
* @Table(name="typed_ids")
* @Mapping\Entity
* @Mapping\Table(name="typed_ids")
*/
#[Mapping\Entity]
#[Mapping\Table(name: 'typed_ids')]
class TypedId
{
/**
* @Id
* @Column
* @GeneratedValue
* @Mapping\Id
* @Mapping\Column
* @Mapping\GeneratedValue
*/
#[Mapping\Id]
#[Mapping\Column]
Expand Down
10 changes: 5 additions & 5 deletions tests/Entities/UnspecifiedId.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
use Doctrine\ORM\Mapping;

/**
* @Entity
* @Table(name="unspecified_ids")
* @Mapping\Entity
* @Mapping\Table(name="unspecified_ids")
*/
#[Mapping\Entity]
#[Mapping\Table(name: 'unspecified_ids')]
class UnspecifiedId
{
/**
* @Id
* @Column
* @GeneratedValue
* @Mapping\Id
* @Mapping\Column
* @Mapping\GeneratedValue
* @var ?string
*/
#[Mapping\Id]
Expand Down
16 changes: 8 additions & 8 deletions tests/Entities/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
use Doctrine\ORM\Mapping;

/**
* @Entity
* @Table(name="users")
* @Mapping\Entity
* @Mapping\Table(name="users")
*/
#[Mapping\Entity]
#[Mapping\Table(name: 'users')]
class User
{
/**
* @Id
* @Column(type="integer")
* @GeneratedValue
* @Mapping\Id
* @Mapping\Column(type="integer")
* @Mapping\GeneratedValue
* @var ?int
*/
#[Mapping\Id]
Expand All @@ -27,21 +27,21 @@ class User
private $id;

/**
* @Column
* @Mapping\Column
* @var string
*/
#[Mapping\Column]
private $email;

/**
* @Column(type="boolean")
* @Mapping\Column(type="boolean")
* @var bool
*/
#[Mapping\Column(type: Types::BOOLEAN)]
private $active = false;

/**
* @Column(name="last_name")
* @Mapping\Column(name="last_name")
* @var string
*/
#[Mapping\Column]
Expand Down
5 changes: 4 additions & 1 deletion tests/InMemoryEntityManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Firehed\Mocktrine;

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\Persistence\ObjectRepository;

/**
Expand All @@ -14,7 +16,8 @@ class InMemoryEntityManagerTest extends \PHPUnit\Framework\TestCase
{
protected function getEntityManager(): InMemoryEntityManager
{
return new InMemoryEntityManager();
$reader = new AnnotationReader();
return new InMemoryEntityManager(new AnnotationDriver($reader));
}

public function testFindWithNoEntity(): void
Expand Down
5 changes: 2 additions & 3 deletions tests/InMemoryRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Firehed\Mocktrine;

use Doctrine\Common\Annotations\SimpleAnnotationReader;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Persistence\ObjectRepository;
use Doctrine\ORM\ORMException;
Expand All @@ -24,8 +24,7 @@ class InMemoryRepositoryTest extends \PHPUnit\Framework\TestCase

public function setUp(): void
{
$reader = new SimpleAnnotationReader();
$reader->addNamespace('Doctrine\ORM\Mapping');
$reader = new AnnotationReader();
$this->driver = new AnnotationDriver($reader);
}

Expand Down

0 comments on commit 3aa43a1

Please sign in to comment.