1<?php 2 3namespace FINDOLOGIC\Export\Tests; 4 5use FINDOLOGIC\Export\Data\Attribute; 6use FINDOLOGIC\Export\Data\Bonus; 7use FINDOLOGIC\Export\Data\DateAdded; 8use FINDOLOGIC\Export\Data\Description; 9use FINDOLOGIC\Export\Data\Keyword; 10use FINDOLOGIC\Export\Data\Name; 11use FINDOLOGIC\Export\Data\Ordernumber; 12use FINDOLOGIC\Export\Data\Price; 13use FINDOLOGIC\Export\Data\Property; 14use FINDOLOGIC\Export\Data\SalesFrequency; 15use FINDOLOGIC\Export\Data\Sort; 16use FINDOLOGIC\Export\Data\Summary; 17use FINDOLOGIC\Export\Data\Url; 18use FINDOLOGIC\Export\Helpers\EmptyValueNotAllowedException; 19use FINDOLOGIC\Export\Helpers\ValueIsNotNumericException; 20use PHPUnit\Framework\TestCase; 21 22class DataElementsTest extends TestCase 23{ 24 /** 25 * Provides a data set for testing if initializing elements of type UsergroupAwareMultiValueItem 26 * with an empty value fails. 27 * 28 * @return array Scenarios with a value, the element class and whether this input should cause an exception. 29 */ 30 public function multiValueItemProvider() 31 { 32 return [ 33 'Keyword with empty value' => ['', Keyword::class, true], 34 'Keyword with value' => ['value', Keyword::class, false], 35 'Ordernumber with empty value' => ['', Ordernumber::class, true], 36 'Ordernumber with value' => ['value', Ordernumber::class, false] 37 ]; 38 } 39 40 /** 41 * @dataProvider multiValueItemProvider 42 * @param string $value 43 * @param string $elementType 44 * @param bool $shouldCauseException 45 */ 46 public function testAddingEmptyValuesToMultiValueItemCausesException( 47 $value = '', 48 $elementType = '', 49 $shouldCauseException = true 50 ) { 51 try { 52 $element = new $elementType($value); 53 if ($shouldCauseException) { 54 $this->fail('Adding empty values should cause exception!'); 55 } else { 56 // The following assertion exists mostly to ensure that PHPUnit does not lament 57 // the lack of assertions in this successful test. 58 $this->assertNotNull($element); 59 } 60 } catch (\Exception $exception) { 61 $this->assertEquals(EmptyValueNotAllowedException::class, get_class($exception)); 62 } 63 } 64 65 /** 66 * Provides a data set for testing if adding empty values to elements of type UsergroupAwareSimpleValue fails. 67 * 68 * @return array Scenarios with a value, the element class and the expected exception, or null if none is supposed 69 * to be thrown. 70 */ 71 public function simpleValueItemProvider() 72 { 73 return [ 74 'Bonus with empty value' => ['', Bonus::class, EmptyValueNotAllowedException::class], 75 'Bonus with value' => [1337, Bonus::class, null], 76 'Bonus with non-numeric value' => ['test', Bonus::class, ValueIsNotNumericException::class], 77 'Description with empty value' => ['', Description::class, EmptyValueNotAllowedException::class], 78 'Description with value' => ['value', Description::class, null], 79 'Name with empty value' => ['', Name::class, EmptyValueNotAllowedException::class], 80 'Name with value' => ['value', Name::class, null], 81 'Price with empty value' => ['', Price::class, EmptyValueNotAllowedException::class], 82 'Price with numeric value' => [1337, Price::class, null], 83 'Price zero' => [0, Price::class, null], 84 'Price with non-numeric value' => ['test', Price::class, ValueIsNotNumericException::class], 85 'SalesFrequency with empty value' => ['', SalesFrequency::class, 86 EmptyValueNotAllowedException::class], 87 'SalesFrequency with value' => [1337, SalesFrequency::class, null], 88 'SalesFrequency with non-numeric value' => ['test', SalesFrequency::class, 89 ValueIsNotNumericException::class], 90 'Sort with empty value' => ['', Sort::class, EmptyValueNotAllowedException::class], 91 'Sort with value' => [1337, Sort::class, null], 92 'Sort with non-numeric value' => ['test', Sort::class, ValueIsNotNumericException::class], 93 'Summary with empty value' => ['', Summary::class, EmptyValueNotAllowedException::class], 94 'Summary with value' => ['value', Summary::class, null], 95 'Url with empty value' => ['', Url::class, EmptyValueNotAllowedException::class], 96 'Url with value' => ['value', Url::class, null] 97 ]; 98 } 99 100 /** 101 * @dataProvider simpleValueItemProvider 102 * @param string $value 103 * @param string $elementType 104 * @param \Exception|null $expectedException 105 */ 106 public function testAddingEmptyValuesToSimpleItemsCausesException( 107 $value = '', 108 $elementType = '', 109 $expectedException = null 110 ) { 111 try { 112 $element = new $elementType(); 113 $element->setValue($value); 114 if ($expectedException !== null) { 115 $this->fail('Adding empty values should cause exception!'); 116 } else { 117 // The following assertion exists mostly to ensure that PHPUnit does not lament 118 // the lack of assertions in this successful test. 119 $this->assertNotNull($element); 120 } 121 } catch (\Exception $e) { 122 $this->assertEquals($expectedException, get_class($e)); 123 } 124 } 125 126 /** 127 * @expectedException \BadMethodCallException 128 */ 129 public function testCallingSetValueMethodOfDateAddedClassCausesException() 130 { 131 $element = new DateAdded(); 132 $element->setValue(""); 133 } 134 135 /** 136 * Provides a data set for testing if adding empty keys or values to attribute and property elements fails. 137 * 138 * @return array Scenarios with key, one or more values, the element class and whether this input should cause 139 * an exception. 140 */ 141 public function emptyValueProvider() 142 { 143 return [ 144 'Attribute with empty key' => ['', ['value'], Attribute::class, true], 145 'Attribute with empty value' => ['key', [''], Attribute::class, true], 146 'Attribute with valid key and value' => ['key', ['value'], Attribute::class, false], 147 'Property with empty key' => ['',['value'], Property::class, true], 148 'Property with empty value' => ['key', [''], Property::class, true], 149 'Property with valid key and value' => ['key', ['value'], Property::class, false] 150 ]; 151 } 152 153 /** 154 * @dataProvider emptyValueProvider 155 * @param string $key 156 * @param string $value 157 * @param string $elementType 158 * @param bool $shouldCauseException 159 */ 160 public function testAddingEmptyValueCausesException( 161 $key = '', 162 $value = '', 163 $elementType = '', 164 $shouldCauseException = true 165 ) { 166 try { 167 $element = new $elementType($key, $value); 168 if ($shouldCauseException) { 169 $this->fail('Adding empty values should cause exception!'); 170 } else { 171 // The following assertion exists mostly to ensure that PHPUnit does not lament 172 // the lack of assertions in this successful test. 173 $this->assertNotNull($element); 174 } 175 } catch (\Exception $exception) { 176 $this->assertEquals(EmptyValueNotAllowedException::class, get_class($exception)); 177 } 178 } 179} 180