survos/crawler-bundle

提供了一种创建爬取网站测试的方法

资助包维护!
kbond

安装数: 2,381

依赖: 0

建议者: 0

安全: 0

星标: 1

关注者: 3

分支: 0

开放问题: 0

类型:symfony-bundle

1.5.340 2024-07-23 22:01 UTC

This package is auto-updated.

Last update: 2024-09-24 13:45:05 UTC


README

测试网站最基本的方法是直接访问起始页面,点击每个链接以确保没有损坏的页面。

然后以不同的用户身份(例如管理员)登录,重复此过程。

这正是这个包所做的事情。结合代码覆盖率,这是一个快速简单的测试方法。

composer req survos/crawler-bundle

工作示例(不含API Platform)

symfony new crawler-7 --webapp --version=next --php=8.2 && cd crawler-7
composer config extra.symfony.allow-contrib true
echo "DATABASE_URL=sqlite:///%kernel.project_dir%/var/data.db" > .env.local
composer update
composer require form validator security-csrf      
composer require orm-fixtures doctrine/doctrine-fixtures-bundle --dev
echo "firstName,string,16,yes," | sed "s/,/\n/g"  | bin/console make:entity Official
echo "lastName,string,32,no," | sed "s/,/\n/g"  | bin/console make:entity Official
echo "officialName,string,48,no," | sed "s/,/\n/g"  | bin/console make:entity Official
echo "birthday,date_immutable,yes," | sed "s/,/\n/g"  | bin/console make:entity Official
echo "gender,string,1,yes," | sed "s/,/\n/g"  | bin/console make:entity Official
bin/console doctrine:schema:update --force --complete

# was bin/console make:crud Official -q
echo ",," | sed "s/,/\n/g"  | bin/console make:crud Official
sed -i "s|'app_app'|'app_homepage'|" src/Controller/OfficialController.php

bin/console make:controller AppController
sed -i "s|Route('/app'|Route('/'|" src/Controller/AppController.php
sed -i "s|'app_app'|'app_homepage'|" src/Controller/AppController.php
cat > templates/app/index.html.twig <<END
{% extends 'base.html.twig' %}
{% block body %}
    <h1>A simple CRUD</h1>
    <a href="{{ path('app_official_index') }}">Listing</a>
{% endblock %}
END

cat > src/DataFixtures/AppFixtures.php <<'END'
<?php

namespace App\DataFixtures;

use App\Entity\Official;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;

class AppFixtures extends Fixture
{
    public function load(ObjectManager $manager): void
    {
        $url = 'https://theunitedstates.io/congress-legislators/legislators-current.json';
        $json = file_get_contents($url);
        foreach (json_decode($json) as $record) {
            $name = $record->name;
            $bio = $record->bio;
            $official = (new Official())
                ->setBirthday(new \DateTimeImmutable($bio->birthday))
                ->setGender($bio->gender)
                ->setFirstName($name->first)
                ->setLastName($name->last)
                ->setOfficialName($name->official_full ?? "$name->first $name->last");
            $manager->persist($official);
        }
    $manager->flush();
    }
}
END

bin/console d:fixtures:load -n

composer require stenope/stenope
bin/console -e prod cache:clear
bin/console -e prod stenope:build ./public/static/ --base-url=/static

启动服务器。在代理工作之前(@todo)如果您使用Symfony CLI,则需要使用服务器的IP地址。

设置默认值(@todo:安装配方)

# config/packages/survos_crawler.yaml
survos_crawler:
  base_url: 'https://127.0.0.1:8000'

过程

bin/console survos:crawl

请注意,第一次运行此命令时,它将在测试目录中创建一个VisitLinksTest.php文件,以便phpunit工作。

该命令访问每个链接并将结果存储在crawldata.json中。然后测试使用这些结果来确保它们正确无误。

当不同用户对不同路由有权限时,这尤其有用,例如,如果您已经保护了/admin路由,但意外地留下了链接,您将得到一个错误。

symfony new --demo crawler_bundle_demo