Я пытаюсь создать модульный тест для конкретной функции, которая возвращает заданный URL-адрес из переключателя на основе набора параметров. Все хорошо, пока не вернется url_for.
Вот возвращенный результат:
http:///opt/ipc/bin/phpunit/phpunit/nikon-d7200-review
http:///opt/ipc/bin/phpunit/phpunit/nikon-d7200-review
http:///opt/ipc/bin/phpunit/phpunit/benq-gw2765-specs
http:///opt/ipc/bin/phpunit/phpunit/benq-gw2765-photos-2
http:///opt/ipc/bin/phpunit/phpunit/benq-gw2765-photos-2
http:///opt/ipc/bin/phpunit/phpunit/benq-gw2765-best-deals
http:///opt/ipc/bin/phpunit/phpunit/nikon-d7200-user-reviews
http:///opt/ipc/bin/phpunit/phpunit/canon-eos-7d-mkii-video-review
http:///opt/ipc/bin/phpunit/phpunit/best-mobile-puzzle-games_round-up
http:///opt/ipc/bin/phpunit/phpunit/best-mobile-puzzle-games_round-up
http:///opt/ipc/bin/phpunit/phpunit/best-mobile-puzzle-games_round-up
http:///opt/ipc/bin/phpunit/phpunit/best-mobile-puzzle-games_round-up
http:///opt/ipc/bin/phpunit/phpunit/round-ups/laptops
http:///opt/ipc/bin/phpunit/phpunit/news/wearables-fitness
http:///opt/ipc/bin/phpunit/phpunit/opinions/5-ways-the-humble-light-bulb-is-being-reinvented
http:///opt/ipc/bin/phpunit/phpunit/opinions/5-ways-the-humble-light-bulb-is-being-reinvented
http:///opt/ipc/bin/phpunit/phpunit/digital-cameras
http:///opt/ipc/bin/phpunit/phpunit/cameras
http:///opt/ipc/bin/phpunit/phpunit/info/evan-kypreos
http:///opt/ipc/bin/phpunit/phpunit/default/vitamix-s30
Как видите, возвращаемый URL заменил URL сайта версией PHUnit. Я хотел бы получить возвращенный URL-адрес, похожий на этот:
http://www.somesite.com/nikon-d7200-review
http://www.somesite.com/benq-gw2765-specs
http://www.somesite.com/benq-gw2765-photos-2
http://www.somesite.com/benq-gw2765-best-deals
http://www.somesite.com/nikon-d7200-user-reviews
http://www.somesite.com/canon-eos-7d-mkii-video-review
http://www.somesite.com/best-mobile-puzzle-games_round-up
Я использую командную строку для запуска моих тестов.
Если вам нужна дополнительная информация, пожалуйста, скажите, что я сделаю все возможное, чтобы объяснить дальше.
Функция, которая вызывает проблему, заключается в следующем …
url_for('@round_up?slug=digital_cameras', true);
И мой тестовый скрипт
class Api2CompositionsHelperTest extends PHPUnit_Framework_TestCase
{
public function setUp(){}
public function tearDown() {}
public function testUrlForItemType()
{
if(function_exists('url_for_itemtype')) {
/**
* Create stubs for function. itemType => params
*
* Params avaliable
* - itemSlug
* - absolute
* - page
* - pageTile
* - itemIndex
* - itemCategoryIdentifier
*/
$stub = array(
'itemType' => array(
// TODO: Find out if Preview is even being used.
// 'preview' => array(
// 'itemSlug' => '',
// 'absolute' => '',
// 'page' => '',
// 'pageTile' => '',
// 'itemIndex' => ''
//),
'review' => array(
'itemSlug' => 'nikon-d7200',
'pageTitle' => 'image-quality-performance-and-verdict',
),
'reviews' => array(
'itemSlug' => 'nikon-d7200',
'pageTitle' => 'image-quality-performance-and-verdict',
'page' => 2
),
'review_specs' => array(
'itemSlug' => 'benq-gw2765'
),
'review_photo' => array(
'itemSlug' => 'benq-gw2765',
),
'review_photos' => array(
'itemSlug' => 'benq-gw2765',
'itemIndex' => 2
),
'review_compareprices' => array(
'itemSlug' => 'benq-gw2765',
),
// TODO: Check recombu deals are still being used.
//'review_recombudeals' => array(
// 'itemSlug' => null,
// 'absolute' => null,
// 'page' => null,
// 'pageTile' => null,
// 'itemIndex' => null,
// 'itemCategoryIdentifier' => null
//),
'review_userreviews' => array(
'itemSlug' => 'nikon-d7200'
),
'review_video' => array(
'itemSlug' => 'canon-eos-7d-mkii'
),
'roundup|roundups|round_up|round_ups' => array(
'itemSlug' => 'best-mobile-puzzle-games'
),
'round_up_index' => array(
'itemSlug' => 'laptops'
),
'news' => array(
'itemSlug' => 'wearables-fitness'
),
'opinion|opinions' => array(
'itemSlug' => '5-ways-the-humble-light-bulb-is-being-reinvented'
),
'category' => array(
'itemSlug' => 'digital-cameras'
),
'root_category' => array(
'itemSlug' => 'cameras'
),
'author' => array(
'itemSlug' => 'evan-kypreos'
),
'default' => array(
'itemSlug' => 'vitamix-s30'
)
)
);
// Loop through
foreach($stub['itemType'] as $itemType => $params) {
$itemType = explode('|', $itemType);
foreach($itemType as $itemType) {
// Test all params
$route = url_for_itemtype(
array_merge(
array(
'itemType' => $itemType
),
$params
)
);
// Check URL.
switch($itemType) {
case 'preview':
// See above TODO
break;
case 'review':
case 'reviews':
if(isset($params['page'])) {
$pattern = '^www.trustedreviews.com\/([a-zA-Z0-9-]+)review([a-zA-Z-]+)page([-0-9]+)^';
} else {
$pattern = '^http:\/\/www.trustedreviews.com\/([a-zA-Z0-9-]+)-review^';
}
break;
case 'review_specs':
$pattern = '^http:\/\/www.trustedreviews.com\/([a-zA-Z0-9-]+)specs^';
break;
case 'review_photo':
case 'review_photos':
if(isset($params['itemIndex'])) {
$pattern = '^http:\/\/www.trustedreviews.com\/([a-zA-Z0-9-]+)photos([0-9-]+)^';
} else {
$pattern = '^http:\/\/www.trustedreviews.com\/([a-zA-Z0-9-]+)photos^';
}
break;
case 'review_compareprices':
$pattern = '^http:\/\/www.trustedreviews.com\/([a-zA-Z0-9-]+)best-deals^';
break;
case 'review_recombudeals':
// See above TODO
break;
case 'review_userreviews':
$pattern = '^http:\/\/www.trustedreviews.com\/([a-zA-Z0-9-]+)user-reviews^';
break;
case 'review_video':
$pattern = '^http:\/\/www.trustedreviews.com\/([a-zA-Z0-9-]+)video-review^';
break;
case 'roundup':
case 'roundups':
case 'round_up':
case 'round_ups':
$pattern = '^http:\/\/www.trustedreviews.com\/([a-zA-Z0-9-]+)_round-up^';
break;
case 'round_up_index':
$pattern = '^http:\/\/www.trustedreviews.com\/round-ups\/([a-zA-Z0-9-]+)^';
break;
case 'news':
$pattern = '^http:\/\/www.trustedreviews.com\/news\/([a-zA-Z0-9-]+)^';
break;
case 'opinion':
case 'opinions':
$pattern = '^http:\/\/www.trustedreviews.com\/opinions\/([a-zA-Z0-9-]+)^';
break;
case 'category':
$pattern = '^http:\/\/www.trustedreviews.com\/([a-zA-Z0-9-]+)^';
break;
case 'root_category':
$pattern = '^http:\/\/www.trustedreviews.com\/([a-zA-Z0-9-]+)^';
break;
case 'author':
$pattern = '^http:\/\/www.trustedreviews.com\/info\/([a-zA-Z0-9-]+)^';
break;
default:
// TODO: research pattern for default
$pattern = '^http:\/\/www.trustedreviews.com\/([a-zA-Z0-9-]+)^';
break;
}
// Check pattern matches URL structure
$this->assertRegExp($pattern, $route);
}
}
} else {
$this->assertFalse(true);
}
}
}
Благодарю.
Попробуйте это, мы сделали это для генерации ссылок в файлах Sitemap в задаче CLI, похоже на то, что вам нужно:
$context = sfContext::createInstance($this->configuration);
$context->getConfiguration()->loadHelpers('Partial');
$routing = $context->getRouting();
$routingOptions = $routing->getOptions();
$routingOptions['context']['prefix'] = '';
$routingOptions['context']['host'] = sfConfig::get('sf_host');
$routing->initialize($this->dispatcher, $routing->getCache(), $routingOptions);
Других решений пока нет …