1<?php
2
3namespace FINDOLOGIC\Export\Tests;
4
5use FINDOLOGIC\Export\CSV\CSVExporter;
6use FINDOLOGIC\Export\Data\Bonus;
7use FINDOLOGIC\Export\Data\DateAdded;
8use FINDOLOGIC\Export\Data\Description;
9use FINDOLOGIC\Export\Data\Name;
10use FINDOLOGIC\Export\Data\Price;
11use FINDOLOGIC\Export\Data\SalesFrequency;
12use FINDOLOGIC\Export\Data\Sort;
13use FINDOLOGIC\Export\Data\Summary;
14use FINDOLOGIC\Export\Data\Url;
15use FINDOLOGIC\Export\Exporter;
16use PHPUnit\Framework\TestCase;
17
18class CSVSerializationTest extends TestCase
19{
20    /** @var CSVExporter */
21    private $exporter;
22
23    /**
24     * @SuppressWarnings(PHPMD.StaticAccess)
25     */
26    public function setUp()
27    {
28        $this->exporter = Exporter::create(Exporter::TYPE_CSV);
29    }
30
31    public function tearDown()
32    {
33        try {
34            unlink('/tmp/findologic.csv');
35        } catch (\Exception $e) {
36            // No need to delete a written file if the test didn't write it.
37        }
38    }
39
40    private function getMinimalItem()
41    {
42        $item = $this->exporter->createItem('123');
43
44        $name = new Name();
45        $name->setValue('Foobar &quot;</>]]>');
46        $item->setName($name);
47
48        $summary = new Summary();
49        $summary->setValue('This is a summary. &quot;</>]]>');
50        $item->setSummary($summary);
51
52        $description = new Description();
53        $description->setValue('This is a more verbose description. &quot;</>]]>');
54        $item->setDescription($description);
55
56        $price = new Price();
57        $price->setValue('13.37');
58        $item->setPrice($price);
59
60        $url = new Url();
61        $url->setValue('http://example.org/my-awesome-product.html');
62        $item->setUrl($url);
63
64        $bonus = new Bonus();
65        $bonus->setValue(3);
66        $item->setBonus($bonus);
67
68        $salesFrequency = new SalesFrequency();
69        $salesFrequency->setValue(42);
70        $item->setSalesFrequency($salesFrequency);
71
72        $dateAdded = new DateAdded();
73        $dateAdded->setDateValue(new \DateTime());
74        $item->setDateAdded($dateAdded);
75
76        $sort = new Sort();
77        $sort->setValue(2);
78        $item->setSort($sort);
79
80        return $item;
81    }
82
83    public function testMinimalItemIsExported()
84    {
85        $item = $this->getMinimalItem();
86        $export = $this->exporter->serializeItems([$item]);
87
88        $this->assertInternalType('string', $export);
89    }
90
91    public function testCsvCanBeWrittenDirectlyToFile()
92    {
93        $item = $this->getMinimalItem();
94
95        $expectedCsvContent = $this->exporter->serializeItems([$item], 0, 1, 1);
96        $this->exporter->serializeItemsToFile('/tmp', [$item], 0, 1, 1);
97
98        self::assertEquals($expectedCsvContent, file_get_contents('/tmp/findologic.csv'));
99    }
100}
101