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\OODBBean as OODBBean;
13 * Tests whether RedBeanPHP handles type casting correctly.
15 * @file RedUNIT/Base/Typechecking.php
16 * @desc Tests basic bean validation rules; invalid bean handling.
17 * @author Gabor de Mooij and the RedBeanPHP Community
18 * @license New BSD/GPLv2
20 * (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
21 * This source file is subject to the New BSD/GPLv2 License that is bundled
22 * with this source code in the file license.txt.
24 class Typechecking extends Base
28 * Test how RedBeanPHP OODB and OODBBean handle type and type casts.
32 * 1. before storing a bean all types are preserved except booleans (they are converted to STRINGS '0' or '1')
33 * 2. after store-reload all bean property values are STRINGS or NULL
34 * (or ARRAYS but that's only from a user perspective because these are lazy loaded)
35 * 3. the ID returned by store() is an INTEGER (if possible; on 32 bit systems overflowing values will be cast to STRINGS!)
38 * ALL VALUES EXCEPT NULL -> STRING
41 * @note Why not simply return bean->id in store()? Because not every driver returns the same type:
42 * databases without insert_id support require a separate query or a suffix returning STRINGS, not INTEGERS.
44 * @note Why not preserve types? I.e. I store integer, why do I get back a string?
45 * Answer: types are handled different across database platforms, would cause overhead to inspect every value for type,
46 * also PHP is a dynamically typed language so types should not matter that much. Another reason: due to the nature
47 * of RB columns in the database might change (INT -> STRING) this would cause return types to change as well which would
48 * cause 'cascading errors', i.e. a column gets widened and suddenly your code would break.
50 * @note Unfortunately the 32/64-bit issue cannot be tested fully. Return-strategy store() is probably the safest
55 public function testTypes()
57 testpack( 'Beans can only contain STRING and NULL after reload' );
59 $bean = R::dispense( 'bean' );
66 $bean->datetime = new\DateTime( 'NOW', new\DateTimeZone( 'Europe/Amsterdam' ) );
67 $id = R::store( $bean );
68 asrt( is_int( $id ), TRUE );
69 asrt( is_float( $bean->float ), TRUE );
70 asrt( is_integer( $bean->number ), TRUE );
71 asrt( is_string( $bean->bool ), TRUE );
72 asrt( is_string( $bean->bool2 ), TRUE );
73 asrt( is_string( $bean->datetime ), TRUE );
74 asrt( is_string( $bean->text ), TRUE );
75 asrt( is_null( $bean->null ), TRUE );
76 $bean = R::load('bean', $id );
77 asrt( is_string( $bean->id ), TRUE );
78 asrt( is_string( $bean->float ), TRUE );
79 asrt( is_string( $bean->number ), TRUE );
80 asrt( is_string( $bean->bool ), TRUE );
81 asrt( is_string( $bean->bool2 ), TRUE );
82 asrt( is_string( $bean->datetime ), TRUE );
83 asrt( is_string( $bean->text ), TRUE );
84 asrt( is_null( $bean->null ), TRUE );
85 asrt( $bean->bool, '0' );
86 asrt( $bean->bool2, '1' );
90 * Test bean type checking.
94 public function testBeanTypeChecking()
96 $redbean = R::getRedBean();
97 $bean = $redbean->dispense( "page" );
98 // Set some illegal values in the bean; this should trigger Security exceptions.
99 // Arrays are not allowed.
100 $bean->name = array( "1" );
102 $redbean->store( $bean );
104 } catch ( RedException $e ) {
108 $redbean->check( $bean );
110 } catch ( RedException $e ) {
113 $bean->name = new OODBBean;
115 $redbean->check( $bean );
117 } catch ( RedException $e ) {
120 // Property names should be alphanumeric
124 $redbean->store( $bean );
126 } catch ( RedException $e ) {
130 $redbean->check( $bean );
132 } catch ( RedException $e ) {
139 $redbean->store( $bean );
141 } catch ( RedException $e ) {
145 $redbean->check( $bean );
147 } catch ( RedException $e ) {