Skip to content

Commit

Permalink
Migrated ImageSitemap 100% tested
Browse files Browse the repository at this point in the history
  • Loading branch information
Nil Portugués committed Feb 28, 2014
1 parent 492acf3 commit 8364c70
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 51 deletions.
70 changes: 40 additions & 30 deletions src/Sonrisa/Component/Sitemap/ImageSitemap.php
Expand Up @@ -10,6 +10,7 @@
use Sonrisa\Component\Sitemap\Items\ImageItem;
use Sonrisa\Component\Sitemap\Validators\AbstractValidator;
use Sonrisa\Component\Sitemap\Validators\ImageValidator;
use \Sonrisa\Component\Sitemap\Exceptions\SitemapException;

/**
* Class ImageSitemap
Expand Down Expand Up @@ -63,46 +64,55 @@ public function add(ImageItem $item,$url='')

$loc = $item->getLoc();

if (!empty($url) && !empty($loc) && !in_array($loc,$this->used_images[$url],true)) {
//Mark URL as used.
$this->used_urls[] = $url;
$this->used_images[$url][] = $loc;
if (!empty($url) && !empty($loc)) {

$this->items[$url] = array();
if(!in_array($loc,$this->used_images[$url],true))
{

//Mark URL as used.
$this->used_urls[] = $url;
$this->used_images[$url][] = $loc;

$item = new ImageItem($this->validator);
$this->items[$url] = array();

//Check constrains
$current = $this->current_file_byte_size + $item->getHeaderSize() + $item->getFooterSize() +
(count($this->items[$url])*( mb_strlen($this->urlHeader,'UTF-8')+mb_strlen($this->urlFooter,'UTF-8')));
//Check constrains
$current = $this->current_file_byte_size + $item->getHeaderSize() + $item->getFooterSize() +
(count($this->items[$url])*( mb_strlen($this->urlHeader,'UTF-8')+mb_strlen($this->urlFooter,'UTF-8')));

//Check if new file is needed or not. ONLY create a new file if the constrains are met.
if ( ($current <= $this->max_filesize) && ( $this->total_items <= $this->max_items_per_sitemap)) {
//add bytes to total
$this->current_file_byte_size = $item->getItemSize();
//Check if new file is needed or not. ONLY create a new file if the constrains are met.
if ( ($current <= $this->max_filesize) && ( $this->total_items <= $this->max_items_per_sitemap)) {
//add bytes to total
$this->current_file_byte_size = $item->getItemSize();

//add item to the item array
$built = $item->build();
if (!empty($built)) {
$this->items[$url][] = $built;

$this->files[$this->total_files][$url][] = implode("\n",$this->items[$url]);
//add item to the item array
$built = $item->build();
if (!empty($built)) {
$this->items[$url][] = $built;

$this->total_items++;
}
} else {
//reset count
$this->current_file_byte_size = 0;
$this->files[$this->total_files][$url][] = implode("\n",$this->items[$url]);

$this->total_items++;
}
} else {
//reset count
$this->current_file_byte_size = 0;

//copy items to the files array.
$this->total_files=$this->total_files+1;
$this->files[$this->total_files][$url][] = implode("\n",$this->items[$url]);
//copy items to the files array.
$this->total_files=$this->total_files+1;
$this->files[$this->total_files][$url][] = implode("\n",$this->items[$url]);

//reset the item count by inserting the first new item
$this->items = array($item);
$this->total_items=1;
//reset the item count by inserting the first new item
$this->items = array($item);
$this->total_items=1;
}
$this->lastItem = $item;
}
$this->lastItem = $item;

}
else
{
throw new SitemapException("A valid URL value for <loc> must be given.");
}


Expand Down
1 change: 1 addition & 0 deletions src/Sonrisa/Component/Sitemap/Items/ImageItem.php
Expand Up @@ -94,6 +94,7 @@ public function setLicense($license)
public function build()
{
$data = '';

//Create item ONLY if all mandatory data is present.
if (!empty($this->data['loc'])) {
$xml = array();
Expand Down
54 changes: 33 additions & 21 deletions tests/Sonrisa/Component/Sitemap/ImageSitemapTest.php
Expand Up @@ -143,7 +143,7 @@ public function testAddUrlWithImagesAbovetheSitemapMaxUrlElementLimit()

public function testAddUrlAndImagesWithValidUrlForImages()
{
$this->setExpectedException("Sonrisa\\Component\\Sitemap\\Exception\\SitemapException");
$this->setExpectedException("Sonrisa\\Component\\Sitemap\\Exceptions\\SitemapException");

$item = new \Sonrisa\Component\Sitemap\Items\ImageItem($this->validator);
$item->setLoc('no/a/proper/url');
Expand All @@ -156,7 +156,7 @@ public function testAddUrlAndImagesWithValidUrlForImages()

public function testAddUrlAndImagesWithNoUrlForImages()
{
$this->setExpectedException("Sonrisa\\Component\\Sitemap\\Exception\\SitemapException");
$this->setExpectedException("Sonrisa\\Component\\Sitemap\\Exceptions\\SitemapException");

$item = new \Sonrisa\Component\Sitemap\Items\ImageItem($this->validator);
$item->setTitle('Example.com logo');
Expand All @@ -166,21 +166,6 @@ public function testAddUrlAndImagesWithNoUrlForImages()
$this->assertEmpty($files);
}

















public function testAddUrlAndImagesWithValidUrlForImagesAndOtherImageDataPassedIsEmpty()
{
Expand All @@ -195,11 +180,21 @@ public function testAddUrlAndImagesWithValidUrlForImagesAndOtherImageDataPassedI
\t</url>
</urlset>
XML;
$this->sitemap->add(array('loc' => 'http://www.example.com/logo.png', 'title' => '', 'geolocation' => '', 'license' => '', 'caption' =>'' ),'http://www.example.com/');

$this->setExpectedException("Sonrisa\\Component\\Sitemap\\Exceptions\\SitemapException");
$item = new \Sonrisa\Component\Sitemap\Items\ImageItem($this->validator);
$item->setLoc('http://www.example.com/logo.png');
$item->setTitle('');
$item->setGeolocation('');
$item->setLicense('');
$item->setCaption('');
$this->sitemap->add($item,'http://www.example.com/');

$files = $this->sitemap->build();
$this->assertEquals($expected,$files[0]);
}


public function testAddUrlAndImagesWithValidUrlAndGeolocationForImages()
{
$expected=<<<XML
Expand All @@ -214,11 +209,17 @@ public function testAddUrlAndImagesWithValidUrlAndGeolocationForImages()
\t</url>
</urlset>
XML;
$this->sitemap->add(array('loc' => 'http://www.example.com/logo.png', 'geolocation' => 'Limerick, Ireland' ),'http://www.example.com/');

$item = new \Sonrisa\Component\Sitemap\Items\ImageItem($this->validator);
$item->setLoc('http://www.example.com/logo.png');
$item->setGeolocation('Limerick, Ireland');
$this->sitemap->add($item,'http://www.example.com/');

$files = $this->sitemap->build();
$this->assertEquals($expected,$files[0]);
}


public function testAddUrlAndImagesWithValidUrlAndLicenseForImages()
{
$expected=<<<XML
Expand All @@ -233,10 +234,17 @@ public function testAddUrlAndImagesWithValidUrlAndLicenseForImages()
\t</url>
</urlset>
XML;
$this->sitemap->add(array('loc' => 'http://www.example.com/logo.png', 'license' => 'MIT' ),'http://www.example.com/');
$item = new \Sonrisa\Component\Sitemap\Items\ImageItem($this->validator);
$item->setLoc('http://www.example.com/logo.png');
$item->setLicense('MIT');
$this->sitemap->add($item,'http://www.example.com/');


$files = $this->sitemap->build();
$this->assertEquals($expected,$files[0]);
}


public function testAddUrlAndImagesWithValidUrlAndCaptionForImages()
{
$expected=<<<XML
Expand All @@ -251,7 +259,11 @@ public function testAddUrlAndImagesWithValidUrlAndCaptionForImages()
\t</url>
</urlset>
XML;
$this->sitemap->add(array('loc' => 'http://www.example.com/logo.png', 'caption' => 'This place is called Limerick, Ireland' ),'http://www.example.com/');
$item = new \Sonrisa\Component\Sitemap\Items\ImageItem($this->validator);
$item->setLoc('http://www.example.com/logo.png');
$item->setCaption('This place is called Limerick, Ireland');
$this->sitemap->add($item,'http://www.example.com/');

$files = $this->sitemap->build();
$this->assertEquals($expected,$files[0]);
}
Expand Down

0 comments on commit 8364c70

Please sign in to comment.