Skip to content

Commit

Permalink
tests: added STI tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Jul 3, 2015
1 parent d641868 commit 108f20d
Show file tree
Hide file tree
Showing 11 changed files with 183 additions and 3 deletions.
45 changes: 45 additions & 0 deletions tests/cases/integration/Repository/repository.sti.phpt
@@ -0,0 +1,45 @@
<?php

/**
* @testCase
* @dataProvider ../../../sections.ini
*/

namespace NextrasTests\Orm\Integration\Repository;

use Mockery;
use NextrasTests\Orm\DataTestCase;
use Tester\Assert;

$dic = require_once __DIR__ . '/../../../bootstrap.php';


class RepositorySTITest extends DataTestCase
{

public function testSelect()
{
\Tester\Environment::lock("integration-{$this->section}", TEMP_DIR);
$thread = $this->orm->contents->findBy(['NextrasTests\Orm\Thread->id' => 1])->fetch();
Assert::type('NextrasTests\Orm\Thread', $thread);
//Asset::fail('a');
}


public function testRead()
{
\Tester\Environment::lock("integration-{$this->section}", TEMP_DIR);
$all = $this->orm->contents->findAll()->orderBy('id');

$thread = $all->fetch();
Assert::type('NextrasTests\Orm\Thread', $thread);

$comment = $all->fetch();
Assert::type('NextrasTests\Orm\Comment', $comment);
}

}


$test = new RepositorySTITest($dic);
$test->run();
7 changes: 7 additions & 0 deletions tests/db/array-data.php
Expand Up @@ -82,4 +82,11 @@
$tagFollower3->createdAt = '2014-01-01 00:10:00';
$orm->tagFollowers->persist($tagFollower3);

$thread = new Thread();
$orm->contents->persist($thread);

$comment = new Comment();
$comment->thread = $thread;
$orm->contents->persist($comment);

$orm->flush();
4 changes: 4 additions & 0 deletions tests/db/mysql-data.sql
Expand Up @@ -5,6 +5,7 @@ TRUNCATE tags;
TRUNCATE authors;
TRUNCATE publishers;
TRUNCATE tag_followers;
TRUNCATE contents;
SET FOREIGN_KEY_CHECKS = 1;

INSERT INTO authors (id, name, web, born) VALUES (1, 'Writer 1', 'http://example.com/1', NULL);
Expand All @@ -30,3 +31,6 @@ INSERT INTO books_x_tags (book_id, tag_id) VALUES (3, 3);
INSERT INTO tag_followers (tag_id, author_id, created_at) VALUES (1, 1, '2014-01-01 00:10:00');
INSERT INTO tag_followers (tag_id, author_id, created_at) VALUES (3, 1, '2014-01-01 00:10:00');
INSERT INTO tag_followers (tag_id, author_id, created_at) VALUES (2, 2, '2014-01-01 00:10:00');

INSERT INTO contents (id, type, parent_id) VALUES (1, 'thread', NULL);
INSERT INTO contents (id, type, parent_id) VALUES (2, 'comment', 1);
13 changes: 11 additions & 2 deletions tests/db/mysql-init.sql
Expand Up @@ -54,11 +54,20 @@ CREATE TABLE books_x_tags (
);


CREATE TABLE `tag_followers` (
CREATE TABLE tag_followers (
tag_id int NOT NULL,
author_id int NOT NULL,
created_at datetime NOT NULL,
PRIMARY KEY (tag_id, author_id),
CONSTRAINT tag_followers_tag FOREIGN KEY (tag_id) REFERENCES tags (id) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT tag_followers_tag FOREIGN KEY (tag_id) REFERENCES tags (id) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT tag_followers_author FOREIGN KEY (author_id) REFERENCES authors (id) ON DELETE CASCADE ON UPDATE CASCADE
);


CREATE TABLE contents (
id int NOT NULL,
type varchar(10) NOT NULL,
parent_id int,
PRIMARY KEY (id),
CONSTRAINT contents_parent_id FOREIGN KEY (parent_id) REFERENCES contents (id) ON DELETE CASCADE ON UPDATE CASCADE
);
4 changes: 4 additions & 0 deletions tests/db/pgsql-data.sql
Expand Up @@ -4,6 +4,7 @@ TRUNCATE tags CASCADE;
TRUNCATE authors CASCADE;
TRUNCATE publishers CASCADE;
TRUNCATE tag_followers CASCADE;
TRUNCATE contents CASCADE;


INSERT INTO "authors" ("id", "name", "web", "born") VALUES (1, 'Writer 1', 'http://example.com/1', NULL);
Expand Down Expand Up @@ -41,3 +42,6 @@ INSERT INTO "books_x_tags" ("book_id", "tag_id") VALUES (3, 3);
INSERT INTO "tag_followers" ("tag_id", "author_id", "created_at") VALUES (1, 1, '2014-01-01 00:10:00');
INSERT INTO "tag_followers" ("tag_id", "author_id", "created_at") VALUES (3, 1, '2014-01-01 00:10:00');
INSERT INTO "tag_followers" ("tag_id", "author_id", "created_at") VALUES (2, 2, '2014-01-01 00:10:00');

INSERT INTO "contents" ("id", "type", "parent_id") VALUES (1, 'thread', NULL);
INSERT INTO "contents" ("id", "type", "parent_id") VALUES (2, 'comment', 1);
11 changes: 10 additions & 1 deletion tests/db/pgsql-init.sql
Expand Up @@ -59,6 +59,15 @@ CREATE TABLE "tag_followers" (
"author_id" int NOT NULL,
"created_at" timestamp NOT NULL,
PRIMARY KEY ("tag_id", "author_id"),
CONSTRAINT "tag_followers_tag" FOREIGN KEY ("tag_id") REFERENCES "tags" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT "tag_followers_tag" FOREIGN KEY ("tag_id") REFERENCES "tags" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT "tag_followers_author" FOREIGN KEY ("author_id") REFERENCES "authors" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);


CREATE TABLE "contents" (
"id" SERIAL4 NOT NULL,
"type" varchar(10) NOT NULL,
"parent_id" int,
PRIMARY KEY ("id"),
CONSTRAINT "contents_parent_id" FOREIGN KEY ("parent_id") REFERENCES "contents" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);
1 change: 1 addition & 0 deletions tests/inc/model/Model.php
Expand Up @@ -9,6 +9,7 @@
* Testing model
* @property-read AuthorsRepository $authors
* @property-read BooksRepository $books
* @property-read ContentsRepository $contents
* @property-read EansRepository $eans
* @property-read PublishersRepository $publishers
* @property-read TagsRepository $tags
Expand Down
22 changes: 22 additions & 0 deletions tests/inc/model/contents/Comment.php
@@ -0,0 +1,22 @@
<?php

/**
* This file is part of the Nextras\ORM library.
*
* @license MIT
* @link https://github.com/nextras/orm
* @author Jan Skrasek
*/

namespace NextrasTests\Orm;

use Nextras\Orm\Entity\Entity;


/**
* @property-read string $type {default comment}
* @property Thread|NULL $thread {m:1 Thread::$comments}
*/
class Comment extends Entity
{
}
18 changes: 18 additions & 0 deletions tests/inc/model/contents/ContentsMapper.php
@@ -0,0 +1,18 @@
<?php

/**
* This file is part of the Nextras\ORM library.
*
* @license MIT
* @link https://github.com/nextras/orm
* @author Jan Skrasek
*/

namespace NextrasTests\Orm;

use Nextras\Orm\Mapper\Mapper;


class ContentsMapper extends Mapper
{
}
38 changes: 38 additions & 0 deletions tests/inc/model/contents/ContentsRepository.php
@@ -0,0 +1,38 @@
<?php

/**
* This file is part of the Nextras\ORM library.
*
* @license MIT
* @link https://github.com/nextras/orm
* @author Jan Skrasek
*/

namespace NextrasTests\Orm;

use Nextras\Orm\Repository\Repository;


/**
* @method Thread|Comment getById($id)
*/
class ContentsRepository extends Repository
{

public static function getEntityClassNames()
{
return [
'NextrasTests\Orm\Comment',
'NextrasTests\Orm\Thread',
];
}


public function getEntityClassName(array $data)
{
return $data['type'] === 'comment'
? 'NextrasTests\Orm\Comment'
: 'NextrasTests\Orm\Thread';
}

}
23 changes: 23 additions & 0 deletions tests/inc/model/contents/Thread.php
@@ -0,0 +1,23 @@
<?php

/**
* This file is part of the Nextras\ORM library.
*
* @license MIT
* @link https://github.com/nextras/orm
* @author Jan Skrasek
*/

namespace NextrasTests\Orm;

use Nextras\Orm\Entity\Entity;
use Nextras\Orm\Relationships\ManyHasOne;


/**
* @property-read string $type {default thread}
* @property ManyHasOne|Comment[] $comments {1:m Comment::$thread}
*/
class Thread extends Entity
{
}

0 comments on commit 108f20d

Please sign in to comment.