3 namespace RedUNIT\Mysql;
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;
13 * Tests whether database schema remains unmodified in frozen
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
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 Freeze extends Mysql
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.
34 public function testFreezer()
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" );
58 $page->name = "half a page";
60 $id = $redbean->store( $page );
65 $post = $redbean->dispense( "post" );
66 $post->title = "existing table";
68 $id = $redbean->store( $post );
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;
78 $id = $redbean->store( $newtype );
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 );