-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom elements support and test for it (#22)
* Add custom elements support and test for it AbstractOffer class received 4 new methods: - setCustomElements(array $customElements = []) - addCustomElement($elementType, $value) - getCustomElements() - getCustomElementByType($elementType) AbstractGeneratorTest was modified in order to extract generation code from runGeneratorTest() method into generateFile() method OfferCustomElementsGeneratorTest uses SimpleXML to load and check generated values, added SimpleXMl as an extension requirement to composer.json * Fix composer.json version requirement for ext-simplexml * Updated custom elements test to cover all methods Using AbstractOffer::getCustomElements() in getHeaderOptions() * Updated AbstractOffer::addCustomElement() to exclude null values Included AbstractOffer::getCustomElementByType() into test * Updated custom elements test to check empty results AbstractOffer::getCustomElementByType($elementType) should return [] when $elementType wan't added
- Loading branch information
1 parent
3df790d
commit 9b1caf8
Showing
4 changed files
with
229 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Bukashk0zzzYmlGenerator | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Bukashk0zzz\YmlGenerator\Tests; | ||
|
||
use Bukashk0zzz\YmlGenerator\Model\Offer\OfferSimple; | ||
use Bukashk0zzz\YmlGenerator\Cdata; | ||
|
||
/** | ||
* Generator test | ||
*/ | ||
class OfferCustomElementsGeneratorTest extends AbstractGeneratorTest | ||
{ | ||
const CDATA_TEST_STRING = '<p>Simple HTML</p></description></offer><![CDATA['; | ||
const OFFER_COUNT = 2; | ||
|
||
/** | ||
* Test generate | ||
*/ | ||
public function testGenerate() | ||
{ | ||
// Don't call $this->validateFileWithDtd() here because custom elements are not included into the default DTD | ||
$this->generateFile(); | ||
$this->checkCustomElements(); | ||
} | ||
|
||
/** | ||
* Need to override parent::createOffers() in order to avoid setting description | ||
* after calling self::createOffer() | ||
* | ||
* {@inheritDoc} | ||
* @see \Bukashk0zzz\YmlGenerator\Tests\AbstractGeneratorTest::createOffers() | ||
*/ | ||
protected function createOffers() | ||
{ | ||
$offers = []; | ||
foreach (\range(1, self::OFFER_COUNT) as $id) { | ||
$offers[] = | ||
$this->createOffer() | ||
->setId($id) | ||
->setCategoryId($id) | ||
; | ||
} | ||
|
||
return $offers; | ||
} | ||
|
||
/** | ||
* Set the test description with CDATA here | ||
* | ||
* {@inheritDoc} | ||
* @see \Bukashk0zzz\YmlGenerator\Tests\AbstractGeneratorTest::createOffer() | ||
*/ | ||
protected function createOffer() | ||
{ | ||
$offer = (new OfferSimple()) | ||
->setAvailable($this->faker->boolean) | ||
->setUrl($this->faker->url) | ||
->setPrice($this->faker->numberBetween(1, 9999)) | ||
->setOldPrice($this->faker->numberBetween(1, 9999)) | ||
->setWeight($this->faker->numberBetween(1, 9999)) | ||
->setCurrencyId('UAH') | ||
->setDelivery($this->faker->boolean) | ||
->setLocalDeliveryCost($this->faker->numberBetween(1, 9999)) | ||
->setSalesNotes($this->faker->text(45)) | ||
->setManufacturerWarranty($this->faker->boolean) | ||
->setCountryOfOrigin('Украина') | ||
->setDownloadable($this->faker->boolean) | ||
->setAdult($this->faker->boolean) | ||
->setMarketCategory($this->faker->word) | ||
->setCpa($this->faker->numberBetween(0, 1)) | ||
->setBarcodes([$this->faker->ean13, $this->faker->ean13]) | ||
|
||
->setName($this->faker->name) | ||
->setVendor($this->faker->company) | ||
->setDescription($this->faker->sentence) | ||
->setVendorCode(null) | ||
->setPickup(true) | ||
->setGroupId($this->faker->numberBetween()) | ||
->addPicture('http://example.com/example.jpeg') | ||
->addBarcode($this->faker->ean13) | ||
|
||
->setCustomElements(['custom_element' => [100500, 'string value']]) | ||
->addCustomElement('custom_element', true) | ||
->addCustomElement('custom_element', false) | ||
->addCustomElement('custom_element', null) // Should not be written | ||
->addCustomElement('custom_element', $cdata = new Cdata(self::CDATA_TEST_STRING)) | ||
->addCustomElement('stock_quantity', 100) // https://rozetka.com.ua/sellerinfo/pricelist/ | ||
; | ||
|
||
$this->assertSame([100500, 'string value', true, false, $cdata], $offer->getCustomElementByType('custom_element')); | ||
$this->assertSame([100], $offer->getCustomElementByType('stock_quantity')); | ||
$this->assertSame([], $offer->getCustomElementByType('non_existent_element')); | ||
|
||
return $offer; | ||
} | ||
|
||
/** | ||
* Load generated XML file and check custom elements | ||
*/ | ||
private function checkCustomElements() | ||
{ | ||
// Much easier to test with SimpleXML tahn with DOM | ||
$yml = \simplexml_load_file($this->settings->getOutputFile()); | ||
|
||
$offers = $yml->shop->offers->offer; | ||
$this->assertNotEmpty($offers); | ||
$this->assertEquals(self::OFFER_COUNT, count($offers)); | ||
|
||
foreach ($offers as $offer) { | ||
$prop = 'stock_quantity'; | ||
$this->assertSame(100, (int) $offer->$prop); // Can't use $offer->stock_quantity because of CS rules | ||
|
||
$prop = 'custom_element'; | ||
$multipleElements = $offer->$prop; // Can't use $offer->custom_element because of CS rules | ||
$this->assertNotEmpty($multipleElements); | ||
|
||
// Verity each added value | ||
$this->assertSame(100500, (int) $multipleElements[0]); | ||
$this->assertSame('string value', (string) $multipleElements[1]); | ||
$this->assertSame('true', (string) $multipleElements[2]); | ||
$this->assertSame('false', (string) $multipleElements[3]); | ||
|
||
// ->addCustomElement('custom_element', null) must not produce an element | ||
|
||
$this->assertSame(self::CDATA_TEST_STRING, (string) $multipleElements[4]); | ||
} | ||
} | ||
|
||
/** | ||
* Create instance of Cdata class with a predefined test string | ||
* | ||
* @return \Bukashk0zzz\YmlGenerator\Cdata | ||
*/ | ||
private function makeDescription() | ||
{ | ||
return new Cdata(self::CDATA_TEST_STRING); | ||
} | ||
} |