diff --git a/tests/LaravelFacebookFeedTest.php b/tests/LaravelFacebookFeedTest.php new file mode 100644 index 0000000..2e18da4 --- /dev/null +++ b/tests/LaravelFacebookFeedTest.php @@ -0,0 +1,157 @@ + 'prod-1', + 'link' => 'https://example.com/product', + 'title' => 'Test Product', + 'price' => '29.99 AUD', + 'image_link' => 'https://example.com/image.jpg', + ], $overrides); +} + +describe('init', function () { + it('stores title, description, and link', function () { + $feed = LaravelFacebookFeed::init('My Store', 'My Desc', 'https://example.com'); + expect($feed->title)->toBe('My Store') + ->and($feed->description)->toBe('My Desc') + ->and($feed->link)->toBe('https://example.com'); + }); + + it('defaults to empty strings', function () { + $feed = LaravelFacebookFeed::init(); + expect($feed->title)->toBe('') + ->and($feed->description)->toBe('') + ->and($feed->link)->toBe(''); + }); +}); + +describe('addItem', function () { + it('accepts a valid item without throwing', function () { + $feed = LaravelFacebookFeed::init('', '', ''); + $feed->addItem(validProduct()); + expect($feed->toXml())->toContain('prod-1'); + }); + + it('throws for missing required field', function (string $field) { + $product = validProduct(); + unset($product[$field]); + expect(fn () => LaravelFacebookFeed::init()->addItem($product)) + ->toThrow(MissingRequiredFieldException::class); + })->with(['id', 'link', 'title', 'price', 'image_link']); + + it('throws with a message containing the field name', function () { + $product = validProduct(); + unset($product['price']); + expect(fn () => LaravelFacebookFeed::init()->addItem($product)) + ->toThrow(MissingRequiredFieldException::class, 'price'); + }); +}); + +describe('toXml', function () { + it('returns a string', function () { + $feed = LaravelFacebookFeed::init('', '', ''); + expect($feed->toXml())->toBeString(); + }); + + it('contains the RSS xmlns:g attribute', function () { + $feed = LaravelFacebookFeed::init('', '', ''); + expect($feed->toXml())->toContain('xmlns:g="http://base.google.com/ns/1.0"'); + }); + + it('contains the RSS version attribute', function () { + $feed = LaravelFacebookFeed::init('', '', ''); + expect($feed->toXml())->toContain('version="2.0"'); + }); + + it('includes the channel title, description and link', function () { + $feed = LaravelFacebookFeed::init('My Store', 'My Desc', 'https://example.com'); + $xml = $feed->toXml(); + expect($xml) + ->toContain('