1<?php
2
3namespace FINDOLOGIC\Export\Tests;
4
5use FINDOLOGIC\Export\Helpers\UsergroupAwareNumericValue;
6use FINDOLOGIC\Export\Helpers\ValueIsNotNumericException;
7use FINDOLOGIC\Export\Helpers\EmptyValueNotAllowedException;
8use FINDOLOGIC\Export\Helpers\DataHelper;
9use PHPUnit\Framework\TestCase;
10
11class DataHelperTest extends TestCase
12{
13    /**
14     * @dataProvider emptyValueProvider
15     *
16     * @param $value string|int value that should be checked.
17     * @param $shouldCauseException bool should an exception be caused by given parameter.
18     */
19    public function testEmptyValueDetectsEmptyStringsOnly($value, $shouldCauseException)
20    {
21        try {
22            $value = DataHelper::checkForEmptyValue($value);
23
24            if ($shouldCauseException) {
25                $this->fail('Should be detected as empty value.');
26            } else {
27                // The following assertion exists mostly to ensure that PHPUnit does not lament
28                // the lack of assertions in this successful test.
29                $this->assertNotNull($value);
30            }
31        } catch (EmptyValueNotAllowedException $e) {
32            if (!$shouldCauseException) {
33                $this->fail('Should not be detected as empty value.');
34            } else {
35                $this->assertEquals('Empty values are not allowed!', $e->getMessage());
36            }
37        }
38    }
39
40    /**
41     * Scenarios for empty value validation.
42     *
43     * @return array Cases with the value to check and whether it should cause a validation issue.
44     */
45    public function emptyValueProvider()
46    {
47        return [
48            'empty string' => ['', true],
49            'non-zero integer' => [123, false],
50            'zero as integer' => [0, false],
51            'non-zero float' => [12.3, false],
52            'zero as float' => [0.0, false],
53            'zero as string' => ['0', false],
54            'null' => ['', true],
55            'false' => [false, true]
56        ];
57    }
58
59    /**
60     * @dataProvider numericValueProvider
61     *
62     * @param $value string|int|bool value that should be checked.
63     * @param $shouldCauseException bool should an exception be caused by given parameter.
64     */
65    public function testNumericValuesAreValidated($value, $shouldCauseException)
66    {
67        try {
68            $numericValueElement = new UsergroupAwareNumericValue('dummies', 'dummy');
69            $numericValueElement->setValue($value);
70
71            if ($shouldCauseException) {
72                $this->fail('Should be detected as numeric value.');
73            } else {
74                // The following assertion exists mostly to ensure that PHPUnit does not lament
75                // the lack of assertions in this successful test.
76                $this->assertNotNull($numericValueElement);
77            }
78        } catch (ValueIsNotNumericException $e) {
79            if (!$shouldCauseException) {
80                $this->fail('Should not be detected as numeric value.');
81            } else {
82                $this->assertEquals('Value is not a valid number!', $e->getMessage());
83            }
84        }
85    }
86
87    /**
88     * Scenarios for numeric value validation.
89     *
90     * @return array Cases with the value to check and whether it should cause a validation issue.
91     */
92    public function numericValueProvider()
93    {
94        return [
95            'string' => ['blubbergurke', true],
96            'non-zero integer' => [123, false],
97            'zero as integer' => [0, false],
98            'non-zero float' => [12.3, false],
99            'zero as float' => [0.0, false],
100            'zero as string' => ['0', false]
101        ];
102    }
103}
104