Exemple #1
0
<?php

require_once __DIR__ . '/../vendor/autoload.php';
date_default_timezone_set('America/Sao_Paulo');
use Sitemapper\Generator;
use Sitemapper\Url;
use Sitemapper\Image;
use Faker\Factory;
$faker = Factory::create();
$host = rtrim($faker->url, '/');
$generator = new \Sitemapper\Generator();
$generator->setOutputDir(sys_get_temp_dir());
$generator->setHostname($host);
$generator->setOverwrite(true);
$generator->setAutoSave(true);
$generator->setMaxAllowedUrls(10000);
$generator->setCompress(true);
for ($i = 0; $i < 100000; $i++) {
    $loc = $host . '/' . $faker->uuid;
    echo $i . '=> ' . $loc . PHP_EOL;
    $randPriority = rand(Url::PRIORITY_LOW, Url::PRIORITY_HIGH);
    $url = new Url($loc, $faker->iso8601, Url::CHANGE_FREQUENCY_DAILY, $randPriority);
    // Adding images
    for ($j = 1; $j < $faker->randomDigit; $j++) {
        $url->addImage(new Image($loc, $faker->catchPhrase));
    }
    $generator->addUrl($url);
}
$generator->generate();
 public function testShouldGenerateAutoSave()
 {
     $generator = new Generator();
     $generator->setOutputDir($this->outputDir);
     $generator->setHostname('http://www.example.com');
     $generator->setMaxAllowedUrls(2);
     $generator->setAutoSave(true);
     $generator->setOverwrite(true);
     $generator->boot();
     $firstSitemap = $generator->getCurrentSitemap();
     $generator->addUrl(new Url('/'));
     $generator->addUrl(new Url('/path1'));
     $generator->addUrl(new Url('/path2', '2014-01-01'));
     $this->assertFileExists($this->outputDir . '/sitemap.xml');
     $this->assertFileExists($this->outputDir . '/sitemap1.xml');
     $this->assertFileNotExists($this->outputDir . '/sitemap2.xml');
     $this->assertEquals($this->readFixture('sitemap.autosave.index.xml'), $generator->getIndexSitemap()->getOutput());
     $this->assertEquals($this->readFixture('sitemap.autosave.map1.xml'), $firstSitemap->getOutput());
     // $this->putFixture('sitemap.autosave.index.xml', $generator->getIndexSitemap()->getOutput());
     // $this->putFixture('sitemap.autosave.map1.xml',  $firstSitemap->getOutput());
     // $this->putFixture('sitemap.autosave.map2.xml', $generator->getCurrentSitemap()->getOutput());
     $generator->generate();
     $this->assertFileExists($this->outputDir . '/sitemap2.xml');
     $this->assertEquals($this->readFixture('sitemap.autosave.map2.xml'), $generator->getCurrentSitemap()->getOutput());
 }