Yaffs site version 1.1
[yaffs-website] / vendor / gabordemooij / redbean / testing / RedUNIT / Mysql / Freeze.php
1 <?php
2
3 namespace RedUNIT\Mysql;
4
5 use RedUNIT\Mysql as Mysql;
6 use RedBeanPHP\Facade as R;
7 use RedBeanPHP\AssociationManager as AssociationManager;
8 use RedBeanPHP\RedException\SQL as SQL;
9
10 /**
11  * Freeze
12  *
13  * Tests whether database schema remains unmodified in frozen
14  * mode.
15  *
16  * @file    RedUNIT/Mysql/Freeze.php
17  * @desc    Tests freezing of databases for production environments.
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 Freeze extends Mysql
26 {
27         /**
28          * Tests freezing the database.
29          * After freezing the database, schema modifications are no longer
30          * allowed and referring to missing columns will now cause exceptions.
31          *
32          * @return void
33          */
34         public function testFreezer()
35         {
36                 $toolbox = R::getToolBox();
37                 $adapter = $toolbox->getDatabaseAdapter();
38                 $writer  = $toolbox->getWriter();
39                 $redbean = $toolbox->getRedBean();
40                 $pdo     = $adapter->getDatabase();
41                 $a = new AssociationManager( $toolbox );
42                 $post = $redbean->dispense( 'post' );
43                 $post->title = 'title';
44                 $redbean->store( $post );
45                 $page = $redbean->dispense( 'page' );
46                 $page->name = 'title';
47                 $redbean->store( $page );
48                 $page = $redbean->dispense( "page" );
49                 $page->name = "John's page";
50                 $idpage = $redbean->store( $page );
51                 $page2 = $redbean->dispense( "page" );
52                 $page2->name = "John's second page";
53                 $idpage2 = $redbean->store( $page2 );
54                 $a->associate( $page, $page2 );
55                 $redbean->freeze( TRUE );
56                 $page = $redbean->dispense( "page" );
57                 $page->sections = 10;
58                 $page->name = "half a page";
59                 try {
60                         $id = $redbean->store( $page );
61                         fail();
62                 } catch ( SQL $e ) {
63                         pass();
64                 }
65                 $post = $redbean->dispense( "post" );
66                 $post->title = "existing table";
67                 try {
68                         $id = $redbean->store( $post );
69                         pass();
70                 } catch ( SQL $e ) {
71                         fail();
72                 }
73                 asrt( in_array( "name", array_keys( $writer->getColumns( "page" ) ) ), TRUE );
74                 asrt( in_array( "sections", array_keys( $writer->getColumns( "page" ) ) ), FALSE );
75                 $newtype = $redbean->dispense( "newtype" );
76                 $newtype->property = 1;
77                 try {
78                         $id = $redbean->store( $newtype );
79                         fail();
80                 } catch ( SQL $e ) {
81                         pass();
82                 }
83                 $logger = R::debug( true, 1 );
84                 // Now log and make sure no 'describe SQL' happens
85                 $page = $redbean->dispense( "page" );
86                 $page->name = "just another page that has been frozen...";
87                 $id = $redbean->store( $page );
88                 $page = $redbean->load( "page", $id );
89                 $page->name = "just a frozen page...";
90                 $redbean->store( $page );
91                 $page2 = $redbean->dispense( "page" );
92                 $page2->name = "an associated frozen page";
93                 $a->associate( $page, $page2 );
94                 $a->related( $page, "page" );
95                 $a->unassociate( $page, $page2 );
96                 $a->clearRelations( $page, "page" );
97                 $items = $redbean->find( "page", array(), array( "1" ) );
98                 $redbean->trash( $page );
99                 $redbean->freeze( FALSE );
100                 asrt( count( $logger->grep( "SELECT" ) ) > 0, TRUE );
101                 asrt( count( $logger->grep( "describe" ) ) < 1, TRUE );
102                 asrt( is_array( $logger->getLogs() ), TRUE );
103                 R::debug( FALSE );
104         }
105 }