php - How to use a Yii fixture object that's not in the defaultScope? Is there a way of using resetScope() with Yii fixtures? -


here's test file:

class mytest extends cdbtestcase  {     public $fixtures = array(         'my_data'   => 'mydata',     );      public function testmyfunction()     {         $myobjectnotindefaultscope = $this->my_data('out_of_scope_object');          //can't $myobjectnotindefaultscope since returns null         // possible use resetscope?         // can set primary key object , use findbypk that's hack      } } 

and here's corresponding fixture:

<?php return array(     'out_of_scope_object' => array(         'title' => 'this 1 out of scope',         'status' => 'archived', // not in default scope     ),     'in_scope_object' => array(         'title' => 'this 1 in scope',         'status' => 'active',     ), ); 

both rows in fixture added db table, that's not problem. can access both rows via primary keys they're allocated. can't access out of scope object in way:

$myobjectnotindefaultscope = $this->my_data('out_of_scope_object'); 

which when you're testing how want access it, think.

i have less satisfactory solution in use of allocating object primary key value , using findbypk (edit: resetscope()) load object. prefer use normal way of working fixtures instead, if that's possible.

edit: clarify little in response posts:

it possible use fixtures method return object. work:

$myobjectindefaultscope = $this->my_data('in_scope_object'); 

but wouldn't work because it's not in default scope , there's seemingly no way of running resetscope() fixture method call:

$myobjectnotindefaultscope = $this->my_data('out_of_scope_object'); 

why need this? well, might want test unarchive method, example. seems reasonable me. (as mentioned before, can round little inelegantly using primary key load object corresponding fixture).

although can access fixture data array using:

$arraynotindefaultscope = $this->my_data['out_of_scope_object']; 

it's returning array not object, can't test object's methods on array.

you using fixtures method, whilst array of object.

so instead of:

$myobjectnotindefaultscope = $this->my_data('out_of_scope_object'); 

you should doing:

$myobjectnotindefaultscope = $this->my_data['out_of_scope_object']; 

check guide more info


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -