Skip to content

Commit

Permalink
tests: added Tag::isGlobal property with mapping callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Oct 10, 2015
1 parent b3146b0 commit ca9ef7b
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 6 deletions.
1 change: 1 addition & 0 deletions tests/db/array-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
$tag1 = new Tag('Tag 1');
$tag2 = new Tag('Tag 2');
$tag3 = new Tag('Tag 3');
$tag3->isGlobal = FALSE;
$orm->tags->persist($tag1);
$orm->tags->persist($tag2);
$orm->tags->persist($tag3);
Expand Down
6 changes: 3 additions & 3 deletions tests/db/mysql-data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ INSERT INTO authors (id, name, web, born) VALUES (2, 'Writer 2', 'http://example

INSERT INTO publishers (id, name) VALUES (1, 'Nextras publisher');

INSERT INTO tags (id, name) VALUES (1, 'Tag 1');
INSERT INTO tags (id, name) VALUES (2, 'Tag 2');
INSERT INTO tags (id, name) VALUES (3, 'Tag 3');
INSERT INTO tags (id, name, is_global) VALUES (1, 'Tag 1', 'y');
INSERT INTO tags (id, name, is_global) VALUES (2, 'Tag 2', 'y');
INSERT INTO tags (id, name, is_global) VALUES (3, 'Tag 3', 'n');

INSERT INTO books (id, author_id, translator_id, title, next_part, publisher_id, published_at) VALUES (1, 1, 1, 'Book 1', NULL, 1, NOW());
INSERT INTO books (id, author_id, translator_id, title, next_part, publisher_id, published_at) VALUES (2, 1, NULL, 'Book 2', NULL, 1, NOW());
Expand Down
1 change: 1 addition & 0 deletions tests/db/mysql-init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ CREATE TABLE publishers (
CREATE TABLE tags (
id int NOT NULL AUTO_INCREMENT,
name varchar(50) NOT NULL,
is_global char(1) NOT NULL,
PRIMARY KEY (id)
) AUTO_INCREMENT=4;

Expand Down
6 changes: 3 additions & 3 deletions tests/db/pgsql-data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ INSERT INTO "publishers" ("id", "name") VALUES (1, 'Nextras publisher');
SELECT setval('publishers_id_seq', 1, TRUE);


INSERT INTO "tags" ("id", "name") VALUES (1, 'Tag 1');
INSERT INTO "tags" ("id", "name") VALUES (2, 'Tag 2');
INSERT INTO "tags" ("id", "name") VALUES (3, 'Tag 3');
INSERT INTO "tags" ("id", "name", "is_global") VALUES (1, 'Tag 1', 'y');
INSERT INTO "tags" ("id", "name", "is_global") VALUES (2, 'Tag 2', 'y');
INSERT INTO "tags" ("id", "name", "is_global") VALUES (3, 'Tag 3', 'n');

SELECT setval('tags_id_seq', 3, TRUE);

Expand Down
1 change: 1 addition & 0 deletions tests/db/pgsql-init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ CREATE TABLE "publishers" (
CREATE TABLE "tags" (
"id" SERIAL4 NOT NULL,
"name" varchar(50) NOT NULL,
"is_global" char(1) NOT NULL,
PRIMARY KEY ("id")
);

Expand Down
1 change: 1 addition & 0 deletions tests/inc/model/tag/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* @property string $name
* @property MHM|Book[] $books {m:n Book::$tags}
* @property OHM|TagFollower[] $tagFollowers {1:m TagFollower::$tag}
* @property bool $isGlobal {default true}
*/
final class Tag extends Entity
{
Expand Down
10 changes: 10 additions & 0 deletions tests/inc/model/tag/TagsMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,14 @@

final class TagsMapper extends Mapper
{
protected function createStorageReflection()
{
$reflection = parent::createStorageReflection();
$reflection->addMapping('isGlobal', 'is_global', function ($val) {
return $val === 'y';
}, function ($val) {
return $val ? 'y' : 'n';
});
return $reflection;
}
}

0 comments on commit ca9ef7b

Please sign in to comment.