Yaffs site version 1.1
[yaffs-website] / vendor / gabordemooij / redbean / testing / RedUNIT / Base / Threeway.php
1 <?php
2
3 namespace RedUNIT\Base;
4
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;
9
10 /**
11  * Threeway
12  *
13  * Tests link/via relations, i.e. N-M tables with additional
14  * columns.
15  *
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
20  *
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.
24  */
25 class Threeway extends Base
26 {
27         /**
28          * Test whether we can use threeway tables without being
29          * bothered by unique constraints.
30          *
31          * @return void
32          */
33         public function testUniqueConstraintOnThreeways()
34         {
35                 AQueryWriter::clearRenames();
36                 R::nuke();
37                 $person = R::dispense( 'person' );
38                 $role = R::dispense( 'role' );
39                 $person->sharedRole[] = $role;
40                 R::store( $person );
41                 $person->link( 'person_role', array(
42                          'unit' => R::dispense('unit')
43                 ))->role = $role;
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 );
47                 R::nuke();
48                 $person = R::dispense( 'person' );
49                 $role = R::dispense( 'role' );
50                 $person->via('participant')->sharedRole[] = $role;
51                 R::store( $person );
52                 $person->link( 'participant', array(
53                          'unit' => R::dispense('unit')
54                 ))->role = $role;
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 );
58                 R::nuke();
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')
69                 ))->role = $role;
70                 R::store( $person );
71                 //Can we add a duplicate role now?
72                 asrt( R::count( 'participant' ), 2 );
73                 AQueryWriter::clearRenames();
74         }
75
76         /**
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.
82          *
83          * @return void
84          */
85         public function testIssueWithDriverReturnID()
86         {
87                 AQueryWriter::clearRenames();
88                 R::nuke();
89                 $book = R::dispense( 'book' );
90                 $page = R::dispense( 'page' );
91                 $book->sharedPageList[] = $page;
92                 R::store( $book );
93                 asrt( R::count( 'page' ), 1 );
94                 $book = $book->fresh();
95                 $book->sharedPageList[] = $page;
96                 R::store( $book );
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;
102                 R::store( $book );
103                 foreach( $book->sharedPageList as $listItem) {
104                         asrt( is_string( $listItem->id ), TRUE );
105                 }
106                 //same test but for own-list
107                 R::nuke();
108                 $book = R::dispense( 'book' );
109                 $page = R::dispense( 'page' );
110                 $book->ownPageList[] = $page;
111                 R::store( $book );
112                 asrt( R::count( 'page' ), 1 );
113                 $book = $book->fresh();
114                 $book->ownPageList[] = $page;
115                 R::store( $book );
116                 //don't save the duplicate bean!
117                 asrt( R::count( 'page' ), 1 );
118                 $book = $book->fresh();
119                 $book->ownPageList[] = $page;
120                 $page->item = 3;
121                 R::store( $book );
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 );
126                 }
127                 AQueryWriter::clearRenames();
128         }
129 }