3 namespace RedUNIT\Base;
5 use RedUNIT\Base as Base;
6 use RedBeanPHP\Facade as R;
7 use RedBeanPHP\RedException as RedException;
8 use RedBeanPHP\QueryWriter\AQueryWriter as AQueryWriter;
13 * Tests link/via relations, i.e. N-M tables with additional
16 * @file RedUNIT/Base/Threeway.php
17 * @desc Various tests for 3-way tables or X-way tables.
18 * @author Gabor de Mooij and the RedBeanPHP Community
19 * @license New BSD/GPLv2
21 * (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
22 * This source file is subject to the New BSD/GPLv2 License that is bundled
23 * with this source code in the file license.txt.
25 class Threeway extends Base
28 * Test whether we can use threeway tables without being
29 * bothered by unique constraints.
33 public function testUniqueConstraintOnThreeways()
35 AQueryWriter::clearRenames();
37 $person = R::dispense( 'person' );
38 $role = R::dispense( 'role' );
39 $person->sharedRole[] = $role;
41 $person->link( 'person_role', array(
42 'unit' => R::dispense('unit')
44 //Can we add a duplicate role now? - No because we started with a simple N-M table
45 //and unique constraint has been applied accordingly, manually change database.
46 asrt( R::count( 'person_role' ), 1 );
48 $person = R::dispense( 'person' );
49 $role = R::dispense( 'role' );
50 $person->via('participant')->sharedRole[] = $role;
52 $person->link( 'participant', array(
53 'unit' => R::dispense('unit')
55 //Can we add a duplicate role now? - No because we started with a simple N-M table
56 //and unique constraint has been applied accordingly, manually change database.
57 asrt( R::count( 'participant' ), 1 );
59 $participant = R::dispense( 'participant' );
60 $person = R::dispense( 'person' );
61 $role = R::dispense( 'role' );
62 $unit = R::dispense( 'unit' );
63 $participant->person = $person;
64 $participant->role = $role;
65 $participant->unit = $unit;
66 R::store( $participant );
67 $person->link( 'participant', array(
68 'unit' => R::dispense('unit')
71 //Can we add a duplicate role now?
72 asrt( R::count( 'participant' ), 2 );
73 AQueryWriter::clearRenames();
77 * Test whether a duplicate bean in the list isnt saved.
78 * This was an issue with Postgres while testing the threeway tables.
79 * Postgres returned the ID as a string while other drivers returned
80 * a numeric value causing different outcome in array_diff when
81 * calculating the shared additions.
85 public function testIssueWithDriverReturnID()
87 AQueryWriter::clearRenames();
89 $book = R::dispense( 'book' );
90 $page = R::dispense( 'page' );
91 $book->sharedPageList[] = $page;
93 asrt( R::count( 'page' ), 1 );
94 $book = $book->fresh();
95 $book->sharedPageList[] = $page;
97 //don't save the duplicate bean!
98 asrt( R::count( 'page' ), 1 );
99 $book = $book->fresh();
100 $page->item = 2; //even if we change a property ?
101 $book->sharedPageList[] = $page;
103 foreach( $book->sharedPageList as $listItem) {
104 asrt( is_string( $listItem->id ), TRUE );
106 //same test but for own-list
108 $book = R::dispense( 'book' );
109 $page = R::dispense( 'page' );
110 $book->ownPageList[] = $page;
112 asrt( R::count( 'page' ), 1 );
113 $book = $book->fresh();
114 $book->ownPageList[] = $page;
116 //don't save the duplicate bean!
117 asrt( R::count( 'page' ), 1 );
118 $book = $book->fresh();
119 $book->ownPageList[] = $page;
122 //don't save the duplicate bean!
123 asrt( R::count( 'page' ), 1 );
124 foreach( $book->ownPageList as $listItem) {
125 asrt( is_string( $listItem->id ), TRUE );
127 AQueryWriter::clearRenames();