Advertisement
rfv123

Q47224428 - PHP code as a PHPUnit file

Nov 16th, 2017
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.78 KB | None | 0 0
  1. <?php
  2.  
  3. // https://stackoverflow.com/questions/47224428/multiple-shared-lists-of-the-same-bean-redbean
  4.  
  5. namespace pip\system;
  6.  
  7. use pip\system\DatabaseAccess;
  8.  
  9. class Q47224428RBSharedList extends \PHPUnit_Framework_TestCase {
  10.  
  11.     const   DB_LOGIN_NAME   = 'qtestdb';
  12.    
  13.     protected $dbAccess;
  14.  
  15.  
  16.     /**
  17.     * Bean Data
  18.     */
  19.     protected $imageData = array(
  20.         array('name' => 'image1'),
  21.         array('name' => 'image2'),
  22.         array('name' => 'image3'),
  23.     );
  24.  
  25.     protected $manualData = array(
  26.         array('name' => 'manual1'),
  27.         array('name' => 'manual2'),
  28.         array('name' => 'manual3'),
  29.     );
  30.  
  31.     protected $productData = array(
  32.         array('name' => 'product1'),
  33.         array('name' => 'product2'),
  34.         array('name' => 'product3'),
  35.     );
  36.  
  37.     /**
  38.     * Beans
  39.     */
  40.     protected $imageBean = array();
  41.  
  42.     protected $manualBean = array();
  43.  
  44.     protected $productBean = array();
  45.  
  46.  
  47.     public static function setUpBeforeClass()
  48.     {
  49.         if (! getIsOrmLoaded('redbeanphp')) {
  50.             include APP_BOOTSTRAP_ROOT .'__application_orm_setup_redbeanphp__.php';      
  51.         }
  52.     }
  53.  
  54.     /**
  55.      * Sets up the fixture, for example, opens a network connection.
  56.      * This method is called before a test is executed.
  57.      */
  58.     protected function setUp() {
  59.         // $this->config = new KeyValueList;
  60.         $this->config = appDIC('config');
  61.         $this->appDIC = appDIC();
  62.         $this->dbDIC = dbDIC();
  63.  
  64. //        $dbTypeSelect = 'sqlite';
  65.         $dbTypeSelect = 'mysql';
  66.        
  67.         $this->dbConfig = $this->config['db'];
  68.        
  69.         if (getIsOrmLoaded('redbeanphp')) {
  70.             $toolbox = \R::getToolBox();
  71.  
  72.             if (!is_object($toolbox)) {
  73.                 // redbeamphp loaded
  74.                 $toolbox = $this->dbDIC['setupDefaultRedbeanphp']; 
  75.             }
  76.  
  77.             $this->assertTrue(is_object($toolbox));
  78.         }
  79.  
  80.         $this->dbAccess = DatabaseAccess::load();
  81.        
  82. //        $this->pdo = $this->dbAccess->getDbConnection(self::DB_LOGIN_NAME);        
  83.         $this->dbAccess->redbeanAddDatabase(self::DB_LOGIN_NAME, $this->pdo);
  84.         $this->metadata = $this->dbAccess->makeMetaData($this->pdo);
  85.     }
  86.  
  87.     /**
  88.      * Tears down the fixture, for example, closes a network connection.
  89.      * This method is called after a test is executed.
  90.      */
  91.     protected function tearDown() {
  92.         /*
  93.         $this->appDIC['getDBConnection'] = null;
  94.  
  95.         $dbFile = APP_DATA_DIR . $this->config['db.sqlite.filename'];
  96.         @unlink($dbFile);
  97.         $allOk = copy(APP_DATA_DIR . $this->config['db.sqlite.testSource'], $dbFile);
  98.         $this->assertTrue($allOk);
  99.         */
  100.     }
  101.  
  102.  
  103.     public function testClearBeans() {
  104.         \R::freeze(false);
  105.         \R::selectDatabase('default');
  106.         $this->assertTrue(\R::hasDatabase(self::DB_LOGIN_NAME));
  107.         $this->assertTrue($this->dbAccess->redbeanSelectDatabase(self::DB_LOGIN_NAME));        
  108.         \R::nuke();
  109.     }
  110.        
  111.  
  112.     public function testCreate01()
  113.     {
  114.         $images  = [
  115.                         ["id" => 1,'name'=>"image"],
  116.                         ["id" => 2,'name'=>"test image2"],
  117.                     ];
  118.  
  119.         $manuals = [
  120.             ["id" => 1,'name'=>"test1"],
  121.             ["id" => 2,'name'=>"test manual2"],
  122.         ];
  123.  
  124.         $this->assertTrue($this->dbAccess->redbeanSelectDatabase(self::DB_LOGIN_NAME));        
  125.         $product = \R::findOneOrDispense( 'product', ' id=? ', [1] );
  126.  
  127.         $product->sharedImageList  = []; //empty / create new shared image list
  128.         $product->sharedManualList = []; //empty / create new shared manual list
  129.         $product->name             = "im a product"; // give product a name for giggles
  130.  
  131.         foreach ($images as $object) {
  132.             $upload                     = \R::findOneOrDispense( 'upload', " id = ? ", [$object['id']] );
  133.             $upload->name               = $object['name'];
  134.             $product->sharedImageList[] = $upload;
  135.         }
  136.  
  137.         foreach ($manuals as $object) {
  138.             $upload                      = \R::findOneOrDispense( 'upload', " id = ? ", [$object['id']] );
  139.             $upload->name                = $object['name'];
  140.             $product->sharedManualList[] = $upload;
  141.         }
  142.  
  143.         $product = \R::load( 'product', \R::store( $product ) );
  144.  
  145.         echo '<pre>';
  146.         echo "\n\n<b>// this should return 2 beans of images that were uploaded</b>\n";
  147.         echo json_encode( $product->sharedImageList, JSON_PRETTY_PRINT );
  148.  
  149.  
  150.         echo "\n\n<b>// this should return 2 beans of manuals that were uploaded</b>\n";
  151.         echo json_encode(  $product->sharedManualList, JSON_PRETTY_PRINT );
  152.  
  153.  
  154.         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";
  155.         echo json_encode( $product->sharedUploadList, JSON_PRETTY_PRINT );
  156.         echo "\n\n\n";
  157.         echo json_encode($product,JSON_PRETTY_PRINT);
  158.  
  159.         echo '</pre>';     
  160.     }
  161.  
  162.     public function testCreateImages() {
  163.         \R::freeze(false);
  164.         $beanName = 'image';
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement