/** * @group Behaviour * @group EnabledOnBeforeBuildQuery * @covers FOF30\Model\DataModel\Behaviour\Enabled::onBeforeBuildQuery * @dataProvider EnabledDataprovider::getTestOnBeforeBuildQuery */ public function testOnBeforeBuildQuery($test, $check) { $msg = 'Own::onAfterBuildQuery %s - Case: ' . $check['case']; $config = array('idFieldName' => $test['tableid'], 'tableName' => $test['table']); $model = new DataModelStub(static::$container, $config); $query = \JFactory::getDbo()->getQuery(true)->select('*')->from('test'); $dispatcher = $model->getBehavioursDispatcher(); $behavior = new Enabled($dispatcher); $behavior->onBeforeBuildQuery($model, $query); $where = ReflectionHelper::getValue($model, 'whereClauses'); $this->assertCount($check['count'], $where, sprintf($msg, 'Failed to set the where')); }
/** * @group Behaviour * @group OwnOnAfterBuildQuery * @covers FOF30\Model\DataModel\Behaviour\Own::onAfterBuildQuery * @dataProvider OwnDataprovider::getTestOnAfterBuildQuery */ public function testOnAfterBuildQuery($test, $check) { $msg = 'Own::onAfterBuildQuery %s - Case: ' . $check['case']; $config = array('idFieldName' => $test['tableid'], 'tableName' => $test['table']); $platform = static::$container->platform; $platform::$user = (object) array('id' => 99); $model = new DataModelStub(static::$container, $config); $query = \JFactory::getDbo()->getQuery(true)->select('*')->from('test'); $dispatcher = $model->getBehavioursDispatcher(); $filter = new Own($dispatcher); $filter->onAfterBuildQuery($model, $query); $rawQuery = (string) $query; if ($check['contains']) { $this->assertNotFalse(stripos($rawQuery, $check['query']), sprintf($msg, 'Query should contain the query clause')); } else { $this->assertFalse(stripos($rawQuery, $check['query']), sprintf($msg, 'Query should not contain the query clause')); } }
/** * @group DataModel * @group DataModelAddBehaviour * @covers FOF30\Model\DataModel::addBehaviour * @dataProvider DataModelGenericDataprovider::getTestAddBehaviour */ public function testAddBehaviour($test, $check) { $msg = 'DataModel::addBehaviour %s - Case: ' . $check['case']; $config = array('idFieldName' => 'foftest_bare_id', 'tableName' => '#__foftest_bares'); $model = new DataModelStub(static::$container, $config); $result = $model->addBehaviour($test['class']); $dispatcher = $model->getBehavioursDispatcher(); $attached = $dispatcher->hasObserverClass($check['class']); $this->assertInstanceOf('\\FOF30\\Model\\DataModel', $result, sprintf($msg, 'Should return and instance of itself')); $this->assertEquals($check['attached'], $attached, sprintf($msg, 'Failed to properly attach the behaviour')); }
/** * @group Behaviour * @group AssetsOnBeforeDelete * @covers FOF30\Model\DataModel\Behaviour\Assets::onBeforeDelete * @dataProvider AssetsDataprovider::getTestOnBeforeDelete */ public function testOnBeforeDelete($test, $check) { $msg = 'Own::onBeforeDelete %s - Case: ' . $check['case']; $db = \JFactory::getDbo(); $config = array('idFieldName' => $test['tableid'], 'tableName' => $test['table']); $model = new DataModelStub(static::$container, $config); $dispatcher = $model->getBehavioursDispatcher(); $behavior = new Assets($dispatcher); $model->setAssetsTracked($test['track']); if ($test['load']) { $model->find($test['load']); } if ($check['exception']) { $this->setExpectedException('FOF30\\Model\\DataModel\\Exception\\NoAssetKey'); } $query = $db->getQuery(true)->select('COUNT(*)')->from('#__assets'); $beforeTotal = $db->setQuery($query)->loadResult(); $result = $behavior->onBeforeDelete($model, $test['id']); $this->assertTrue($result, sprintf($msg, 'Returned a wrong value')); $query = $db->getQuery(true)->select('COUNT(*)')->from('#__assets'); $afterTotal = $db->setQuery($query)->loadResult(); $this->assertEquals($check['count'], $beforeTotal - $afterTotal, sprintf($msg, 'Deleted a wrong number of assets')); }
/** * @group DataModel * @group DataModelMove * @covers FOF30\Model\DataModel::move * @dataProvider SpecialColumnsDataprovider::getTestMove */ public function testMove($test, $check) { // Please note that if you try to debug this test, you'll get a "Couldn't fetch mysqli_result" error // That's harmless and appears in debug only, you might want to suppress exception thowing //\PHPUnit_Framework_Error_Warning::$enabled = false; $before = 0; $beforeDisp = 0; $after = 0; $afterDisp = 0; $db = \JFactory::getDbo(); $msg = 'DataModel::move %s - Case: ' . $check['case']; $config = array('idFieldName' => 'foftest_foobar_id', 'tableName' => '#__foftest_foobars'); // I am passing those methods so I can double check if the method is really called $methods = array('onBeforeMove' => function () use(&$before) { $before++; }, 'onAfterMove' => function () use(&$after) { $after++; }); $model = new DataModelStub(static::$container, $config, $methods); $dispatcher = $model->getBehavioursDispatcher(); // Let's attach a custom observer, so I can mock and check all the calls performed by the dispatcher // P.A. The object is immediatly attached to the dispatcher, so I don't need to manually do that new ObserverClosure($dispatcher, array('onBeforeMove' => function (&$subject, &$delta, &$where) use($test, &$beforeDisp) { if (!is_null($test['mock']['find'])) { $subject->find($test['mock']['find']); } if (!is_null($test['mock']['delta'])) { $delta = $test['mock']['delta']; } if (!is_null($test['mock']['where'])) { $where = $test['mock']['where']; } $beforeDisp++; }, 'onAfterMove' => function () use(&$afterDisp) { $afterDisp++; })); if ($test['id']) { $model->find($test['id']); } $result = $model->move($test['delta'], $test['where']); // Now let's take a look at the updated records $query = $db->getQuery(true)->select('ordering')->from($db->qn('#__foftest_foobars'))->order($db->qn($model->getIdFieldName()) . ' ASC'); $ordering = $db->setQuery($query)->loadColumn(); $this->assertInstanceOf('\\FOF30\\Model\\DataModel', $result, sprintf($msg, 'Should return an instance of itself')); $this->assertEquals(1, $before, sprintf($msg, 'Failed to invoke the onBefore method')); $this->assertEquals(1, $beforeDisp, sprintf($msg, 'Failed to invoke the onBeforeMove event')); $this->assertEquals(1, $after, sprintf($msg, 'Failed to invoke the onAfter method')); $this->assertEquals(1, $afterDisp, sprintf($msg, 'Failed to invoke the onAfterMove event')); $this->assertEquals($check['order'], $ordering, sprintf($msg, 'Failed to save the correct order')); }