Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // https://stackoverflow.com/questions/47224428/multiple-shared-lists-of-the-same-bean-redbean
- namespace pip\system;
- use pip\system\DatabaseAccess;
- class Q47224428RBSharedList extends \PHPUnit_Framework_TestCase {
- const DB_LOGIN_NAME = 'qtestdb';
- protected $dbAccess;
- /**
- * Bean Data
- */
- protected $imageData = array(
- array('name' => 'image1'),
- array('name' => 'image2'),
- array('name' => 'image3'),
- );
- protected $manualData = array(
- array('name' => 'manual1'),
- array('name' => 'manual2'),
- array('name' => 'manual3'),
- );
- protected $productData = array(
- array('name' => 'product1'),
- array('name' => 'product2'),
- array('name' => 'product3'),
- );
- /**
- * Beans
- */
- protected $imageBean = array();
- protected $manualBean = array();
- protected $productBean = array();
- public static function setUpBeforeClass()
- {
- if (! getIsOrmLoaded('redbeanphp')) {
- include APP_BOOTSTRAP_ROOT .'__application_orm_setup_redbeanphp__.php';
- }
- }
- /**
- * Sets up the fixture, for example, opens a network connection.
- * This method is called before a test is executed.
- */
- protected function setUp() {
- // $this->config = new KeyValueList;
- $this->config = appDIC('config');
- $this->appDIC = appDIC();
- $this->dbDIC = dbDIC();
- // $dbTypeSelect = 'sqlite';
- $dbTypeSelect = 'mysql';
- $this->dbConfig = $this->config['db'];
- if (getIsOrmLoaded('redbeanphp')) {
- $toolbox = \R::getToolBox();
- if (!is_object($toolbox)) {
- // redbeamphp loaded
- $toolbox = $this->dbDIC['setupDefaultRedbeanphp'];
- }
- $this->assertTrue(is_object($toolbox));
- }
- $this->dbAccess = DatabaseAccess::load();
- // $this->pdo = $this->dbAccess->getDbConnection(self::DB_LOGIN_NAME);
- $this->dbAccess->redbeanAddDatabase(self::DB_LOGIN_NAME, $this->pdo);
- $this->metadata = $this->dbAccess->makeMetaData($this->pdo);
- }
- /**
- * Tears down the fixture, for example, closes a network connection.
- * This method is called after a test is executed.
- */
- protected function tearDown() {
- /*
- $this->appDIC['getDBConnection'] = null;
- $dbFile = APP_DATA_DIR . $this->config['db.sqlite.filename'];
- @unlink($dbFile);
- $allOk = copy(APP_DATA_DIR . $this->config['db.sqlite.testSource'], $dbFile);
- $this->assertTrue($allOk);
- */
- }
- public function testClearBeans() {
- \R::freeze(false);
- \R::selectDatabase('default');
- $this->assertTrue(\R::hasDatabase(self::DB_LOGIN_NAME));
- $this->assertTrue($this->dbAccess->redbeanSelectDatabase(self::DB_LOGIN_NAME));
- \R::nuke();
- }
- public function testCreate01()
- {
- $images = [
- ["id" => 1,'name'=>"image"],
- ["id" => 2,'name'=>"test image2"],
- ];
- $manuals = [
- ["id" => 1,'name'=>"test1"],
- ["id" => 2,'name'=>"test manual2"],
- ];
- $this->assertTrue($this->dbAccess->redbeanSelectDatabase(self::DB_LOGIN_NAME));
- $product = \R::findOneOrDispense( 'product', ' id=? ', [1] );
- $product->sharedImageList = []; //empty / create new shared image list
- $product->sharedManualList = []; //empty / create new shared manual list
- $product->name = "im a product"; // give product a name for giggles
- foreach ($images as $object) {
- $upload = \R::findOneOrDispense( 'upload', " id = ? ", [$object['id']] );
- $upload->name = $object['name'];
- $product->sharedImageList[] = $upload;
- }
- foreach ($manuals as $object) {
- $upload = \R::findOneOrDispense( 'upload', " id = ? ", [$object['id']] );
- $upload->name = $object['name'];
- $product->sharedManualList[] = $upload;
- }
- $product = \R::load( 'product', \R::store( $product ) );
- echo '<pre>';
- echo "\n\n<b>// this should return 2 beans of images that were uploaded</b>\n";
- echo json_encode( $product->sharedImageList, JSON_PRETTY_PRINT );
- echo "\n\n<b>// this should return 2 beans of manuals that were uploaded</b>\n";
- echo json_encode( $product->sharedManualList, JSON_PRETTY_PRINT );
- echo "\n\n<b>// this is the only way i can access the shared lists, which is the name of the (upload) bean</b>\n";
- echo json_encode( $product->sharedUploadList, JSON_PRETTY_PRINT );
- echo "\n\n\n";
- echo json_encode($product,JSON_PRETTY_PRINT);
- echo '</pre>';
- }
- public function testCreateImages() {
- \R::freeze(false);
- $beanName = 'image';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement