PHPUnit to test defaultScope

How can one test a custom defaultShope with unit test?

Got a Property model and it got a HAS_MANY relation to PropertyUrl model. What I would like to do is, when I view a property (/property/view?id=1) to only see those PropertyUrls that belongs to it. (I know that there are many other ways to do this apart from scope, but if it’s possible I would like to stick with it.)

So in order to only get the right PropertyUrls, when I call PropertyUrl::model()->findAll(); and only when i’m on the property/view… page I set the following defaultScope in the PropertyUrl model:


	public function defaultScope(){

		$controller=Yii::app()->controller->id;

		$action = Yii::app()->controller->action->id;

		$alias = $this->getTableAlias(false,false);


		if($controller=='property' && $action=='view') {

			$property_id = $_GET['id'];

			$criteria = new CDbCriteria;

			$criteria->compare("$alias.property_id", $property_id);

			return $criteria;

		}

		else {

			return array();

		}

	}

To test this I made the PropertyUrlTest::testDefaultScope class::method for it. But I have no clue how could I set the controller id and action id in the testDefaultScope method. I hope someone could help me in this. This is the last code I tried in my test method:


	public function testDefaultScope() {

		// test the scope on property controller's actionView

		Yii::app()->setController('property');

		$controller = new CController('property');

		$controller->setAction('view');

		$this->assertEquals(2, count(PropertyUrl::model()->findAll()));

	}

Of course this dies as I’m trying to set the controller and action in the wrong way. The test fails as the defaultScope is “Trying to get property of non-object”.

Hands up who think my logic is faulty and I should do a functional test instead of unit test, to test something that actually have and effect in the Controller/View?!

Thanks in advance for the help

PS. "moved" this topic from Installation and Setup section. (wonder why there is no move topic option in Topic Moderation?)

bump + info:

is it seriously a bad idea to only filter with defaultScope/scope, if the request comes from a specific controller/action? Found this:

Source: http://www.yiiframework.com/doc/guide/1.1/en/basics.best-practices

bump

looks like I’ll have to forget to use defaultScope for this task… so I’ll get cracking with named scopes, unless anyone else have some better idea?!