Skip to content

Commit

Permalink
Provide failing test for readonly properties
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed Jan 9, 2022
1 parent 0d911b9 commit 20d0298
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 0 deletions.
31 changes: 31 additions & 0 deletions tests/Doctrine/Tests/Models/ReadonlyProperties/Author.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\ReadonlyProperties;

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\Table;

#[Entity, Table(name: 'author')]
class Author
{
#[Column, Id, GeneratedValue]
private readonly int $id;

#[Column]
private readonly string $name;

public function getId(): int
{
return $this->id;
}

public function getName(): string
{
return $this->name;
}
}
51 changes: 51 additions & 0 deletions tests/Doctrine/Tests/Models/ReadonlyProperties/Book.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\ReadonlyProperties;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\JoinTable;
use Doctrine\ORM\Mapping\ManyToMany;
use Doctrine\ORM\Mapping\Table;

#[Entity, Table(name: 'book')]
class Book
{
#[Column, Id, GeneratedValue]
private readonly int $id;

#[Column]
private readonly string $title;

#[ManyToMany(targetEntity: Author::class), JoinTable(name: 'book_author')]
private readonly Collection $authors;

public function __construct()
{
$this->authors = new ArrayCollection();
}

public function getId(): int
{
return $this->id;
}

public function getTitle(): string
{
return $this->title;
}

/**
* @return list<Author>
*/
public function getAuthors(): array
{
return $this->authors->getValues();
}
}
41 changes: 41 additions & 0 deletions tests/Doctrine/Tests/Models/ReadonlyProperties/SimpleBook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Models\ReadonlyProperties;

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\Table;

#[Entity, Table(name: 'simple_book')]
class SimpleBook
{
#[Column, Id, GeneratedValue]
private readonly int $id;

#[Column]
private readonly string $title;

#[ManyToOne, JoinColumn(nullable: false)]
private readonly Author $author;

public function getId(): int
{
return $this->id;
}

public function getTitle(): string
{
return $this->title;
}

public function getAuthor(): Author
{
return $this->author;
}
}
111 changes: 111 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/ReadonlyPropertiesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional;

use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\Tests\Models\ReadonlyProperties\Author;
use Doctrine\Tests\Models\ReadonlyProperties\Book;
use Doctrine\Tests\Models\ReadonlyProperties\SimpleBook;
use Doctrine\Tests\OrmFunctionalTestCase;
use Doctrine\Tests\TestUtil;

use function dirname;

/**
* @requires PHP 8.1
*/
class ReadonlyPropertiesTest extends OrmFunctionalTestCase
{
protected function setUp(): void
{
if (! isset(static::$sharedConn)) {
static::$sharedConn = TestUtil::getConnection();
}

$this->_em = $this->getEntityManager(null, new AttributeDriver(
[dirname(__DIR__, 2) . '/Models/ReadonlyProperties']
));
$this->_schemaTool = new SchemaTool($this->_em);

parent::setUp();

$this->setUpEntitySchema([Author::class, Book::class, SimpleBook::class]);
}

public function testSimpleEntity(): void
{
$connection = $this->_em->getConnection();

$connection->insert('author', ['name' => 'Jane Austen']);
$authorId = $connection->lastInsertId();

$author = $this->_em->find(Author::class, $authorId);

self::assertSame('Jane Austen', $author->getName());
self::assertEquals($authorId, $author->getId());
}

public function testEntityWithLazyManyToOne(): void
{
$connection = $this->_em->getConnection();

$connection->insert('author', ['name' => 'Jane Austen']);
$authorId = $connection->lastInsertId();

$connection->insert('simple_book', ['title' => 'Pride and Prejudice', 'author_id' => $authorId]);
$bookId = $connection->lastInsertId();

$book = $this->_em->find(SimpleBook::class, $bookId);

self::assertSame('Pride and Prejudice', $book->getTitle());
self::assertEquals($bookId, $book->getId());
self::assertSame('Jane Austen', $book->getAuthor()->getName());
}

public function testEntityWithEagerManyToOne(): void
{
$connection = $this->_em->getConnection();

$connection->insert('author', ['name' => 'Jane Austen']);
$authorId = $connection->lastInsertId();

$connection->insert('simple_book', ['title' => 'Pride and Prejudice', 'author_id' => $authorId]);
$bookId = $connection->lastInsertId();

[$book] = $this->_em->createQueryBuilder()
->from(SimpleBook::class, 'b')
->join('b.author', 'a')
->select(['b', 'a'])
->where('b.id = :id')
->setParameter('id', $bookId)
->getQuery()
->execute();

self::assertInstanceOf(SimpleBook::class, $book);
self::assertSame('Pride and Prejudice', $book->getTitle());
self::assertEquals($bookId, $book->getId());
self::assertSame('Jane Austen', $book->getAuthor()->getName());
}

public function testEntityWithManyToMany(): void
{
$connection = $this->_em->getConnection();

$connection->insert('author', ['name' => 'Jane Austen']);
$authorId = $connection->lastInsertId();

$connection->insert('book', ['title' => 'Pride and Prejudice']);
$bookId = $connection->lastInsertId();

$connection->insert('book_author', ['book_id' => $bookId, 'author_id' => $authorId]);

$book = $this->_em->find(Book::class, $bookId);

self::assertSame('Pride and Prejudice', $book->getTitle());
self::assertEquals($bookId, $book->getId());
self::assertSame('Jane Austen', $book->getAuthors()[0]->getName());
}
}

0 comments on commit 20d0298

Please sign in to comment.