2

我正在尝试为特定函数创建一个单元测试,该函数根据一组参数从开关返回给定的 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);
        }
    }
} 

谢谢。

4

1 回答 1

0

试试这个,我们这样做是为了在 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);
于 2015-06-18T18:49:28.223 回答