-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add MockTrait::emCreateMockEntity and tests for MockTrait
- Loading branch information
Showing
8 changed files
with
337 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace ORM\Test\MockTrait; | ||
|
||
use ORM\MockTrait; | ||
use ORM\Test\Entity\Examples\Article; | ||
use ORM\Test\TestCase; | ||
|
||
class CreateMockedEntityTest extends TestCase | ||
{ | ||
use MockTrait; | ||
|
||
protected $em; | ||
|
||
protected function setUp() | ||
{ | ||
$this->em = $this->emInitMock(); | ||
} | ||
|
||
public function testReturnsTheEntity() | ||
{ | ||
$article = $this->emCreateMockedEntity(Article::class); | ||
|
||
self::assertInstanceOf(Article::class, $article); | ||
} | ||
|
||
public function testSetsTheData() | ||
{ | ||
$article = $this->emCreateMockedEntity(Article::class, ['id' => 42, 'title' => 'Hello World!']); | ||
|
||
self::assertSame(42, $article->id); | ||
self::assertSame('Hello World!', $article->title); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace ORM\Test\MockTrait; | ||
|
||
use Mockery\Adapter\Phpunit\MockeryTestCase; | ||
use Mockery\MockInterface; | ||
use ORM\EntityFetcher; | ||
use ORM\EntityManager; | ||
use ORM\MockTrait; | ||
use ORM\Test\Entity\Examples\Article; | ||
|
||
class ExpectFetchTest extends MockeryTestCase | ||
{ | ||
use MockTrait; | ||
|
||
/** @var EntityManager|MockInterface */ | ||
protected $em; | ||
|
||
protected function setUp() | ||
{ | ||
$this->em = $this->emInitMock(); | ||
} | ||
|
||
protected function tearDown() | ||
{ | ||
\Mockery::close(); | ||
} | ||
|
||
public function testReturnsFetcher() | ||
{ | ||
$fetcher = $this->emExpectFetch(Article::class); | ||
|
||
self::assertInstanceOf(EntityFetcher::class, $fetcher); | ||
|
||
\Mockery::resetContainer(); | ||
} | ||
|
||
public function testMocksFetch() | ||
{ | ||
$fetcher = $this->emExpectFetch(Article::class); | ||
|
||
self::assertSame($fetcher, $this->em->fetch(Article::class)); | ||
} | ||
|
||
public function testReturnsNull() | ||
{ | ||
$this->emExpectFetch(Article::class); | ||
|
||
$fetcher = $this->em->fetch(Article::class); | ||
$result = $fetcher->one(); | ||
|
||
self::assertNull($result); | ||
} | ||
|
||
public function testReturnsEntities() | ||
{ | ||
$articles = [new Article(), new Article()]; | ||
$this->emExpectFetch(Article::class, $articles); | ||
|
||
$fetcher = $this->em->fetch(Article::class); | ||
$result = $fetcher->all(); | ||
|
||
self::assertSame($articles, $result); | ||
} | ||
|
||
public function testReturnsCount() | ||
{ | ||
$articles = [new Article(), new Article()]; | ||
$this->emExpectFetch(Article::class, $articles); | ||
|
||
$fetcher = $this->em->fetch(Article::class); | ||
$result = $fetcher->count(); | ||
|
||
self::assertSame(2, $result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
namespace ORM\Test\MockTrait; | ||
|
||
use Mockery\Adapter\Phpunit\MockeryTestCase; | ||
use Mockery\MockInterface; | ||
use ORM\EntityManager; | ||
use ORM\MockTrait; | ||
use ORM\Test\Entity\Examples\Article; | ||
use ORM\Test\Entity\Examples\Category; | ||
|
||
class ExpectInsertTest extends MockeryTestCase | ||
{ | ||
use MockTrait; | ||
|
||
/** @var EntityManager|MockInterface */ | ||
protected $em; | ||
|
||
protected function setUp() | ||
{ | ||
$this->em = $this->emInitMock(); | ||
} | ||
|
||
protected function tearDown() | ||
{ | ||
\Mockery::close(); | ||
} | ||
|
||
public function testAllowsInsertOfSpecifiedClass() | ||
{ | ||
$this->emExpectInsert(Article::class, ['id' => 42]); | ||
$article = new Article(); | ||
|
||
$article->save(); | ||
} | ||
|
||
public function testDoesNotAllowInsertsOfOtherClasses() | ||
{ | ||
$this->emExpectInsert(Category::class); | ||
$article = new Article(); | ||
|
||
self::expectException(\BadMethodCallException::class); | ||
self::expectExceptionMessage('PDO::query() does not exist on this mock object'); | ||
|
||
try { | ||
$article->save(); | ||
} catch (\Exception $e) { | ||
\Mockery::resetContainer(); | ||
throw $e; | ||
} | ||
} | ||
|
||
public function testSetsDefaultData() | ||
{ | ||
$defaults = ['id' => 42, 'created' => date('c')]; | ||
$this->emExpectInsert(Article::class, $defaults); | ||
$article = new Article(); | ||
|
||
$article->save(); | ||
|
||
self::assertSame($defaults, $article->getData()); | ||
} | ||
|
||
public function testDoesNotOverwriteCurrentData() | ||
{ | ||
$defaults = ['id' => 42, 'created' => date('c')]; | ||
$this->emExpectInsert(Article::class, $defaults); | ||
$article = new Article(); | ||
|
||
$article->id = 1337; | ||
$article->created = date('c', strtotime('-1 Hour')); | ||
$article->save(); | ||
|
||
self::assertNotEquals($defaults, $article->getData()); | ||
} | ||
|
||
public function testEmulatesAutoIncrementWithRandomValue() | ||
{ | ||
$this->emExpectInsert(Article::class); | ||
$article = new Article(); | ||
|
||
$article->save(); | ||
|
||
self::assertGreaterThan(0, $article->id); | ||
} | ||
} |
Oops, something went wrong.