Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Commit 818f22f

Browse files
committed
Created tests per use-case for removeEmptyData - split recurring functionality for creatoin of listener and event into own functions, re-used for all 5 test functions
1 parent 50a4143 commit 818f22f

File tree

1 file changed

+190
-19
lines changed

1 file changed

+190
-19
lines changed

test/ContentValidationListenerTest.php

Lines changed: 190 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,9 +2096,38 @@ public function testSaveUnknownDataWhenEmptyInputFilter()
20962096
$this->assertEquals($params, $dataParams->getBodyParams());
20972097
}
20982098

2099-
public function testFilterEmptyEntriesFromDataByOptionWithNestedData()
2099+
/**
2100+
* @group 40 removeEmptyData
2101+
*
2102+
* @param array $eventParams
2103+
*
2104+
* @return MvcEvent
2105+
*/
2106+
public function createGroup40Event(array $eventParams)
2107+
{
2108+
$request = new HttpRequest();
2109+
$request->setMethod('POST');
2110+
2111+
$matches = $this->createRouteMatch(['controller' => 'Foo']);
2112+
2113+
$dataParams = new ParameterDataContainer();
2114+
$dataParams->setBodyParams($eventParams);
2115+
2116+
$event = new MvcEvent();
2117+
$event->setRequest($request);
2118+
$event->setRouteMatch($matches);
2119+
$event->setParam('ZFContentNegotiationParameterData', $dataParams);
2120+
2121+
return $event;
2122+
}
2123+
2124+
/**
2125+
* @group 40 removeEmptyData
2126+
*
2127+
* @return ContentValidationListener
2128+
*/
2129+
public function createGroup40Listener()
21002130
{
2101-
$services = new ServiceManager();
21022131
$factory = new InputFilterFactory();
21032132

21042133
$inputFilterA = $factory->createInputFilter(
@@ -2141,9 +2170,10 @@ public function testFilterEmptyEntriesFromDataByOptionWithNestedData()
21412170

21422171
$inputFilterA->add($inputFilterB, 'empty_array');
21432172

2173+
$services = new ServiceManager();
21442174
$services->setService('FooFilter', $inputFilterA);
21452175

2146-
$listener = new ContentValidationListener(
2176+
return new ContentValidationListener(
21472177
[
21482178
'Foo' => [
21492179
'input_filter' => 'FooFilter',
@@ -2157,27 +2187,168 @@ public function testFilterEmptyEntriesFromDataByOptionWithNestedData()
21572187
'Foo' => 'foo_id',
21582188
]
21592189
);
2190+
}
21602191

2161-
$request = new HttpRequest();
2162-
$request->setMethod('POST');
2192+
/**
2193+
* @group 40 removeEmptyData
2194+
*
2195+
* Flows:
2196+
* 1 - data is empty, return immediately
2197+
* 2 - loop key/value - value (is not an array and (not empty or (is a boolean & not in comparison array)))
2198+
* 3 - loop key/value - value is not an array
2199+
* 4 - after filtering value, value is empty
2200+
* 5 - value is an array containing recursive data (subject to 1 through 4)
2201+
*
2202+
* This test does #1
2203+
*/
2204+
public function testFilterEmptyEntriesFromDataByOptionWhenDataEmpty()
2205+
{
2206+
// empty array
2207+
$event = $this->createGroup40Event([]);
21632208

2164-
$matches = $this->createRouteMatch(['controller' => 'Foo']);
2209+
$listener = $this->createGroup40Listener();
2210+
$listener->onRoute($event);
21652211

2166-
$params = [
2167-
'foo' => ' abc ',
2168-
'empty' => null,
2169-
'empty_array' => [
2170-
'empty_field' => null,
2212+
$this->assertEquals(
2213+
[],
2214+
$event->getParam('ZFContentNegotiationParameterData')->getBodyParams()
2215+
);
2216+
}
2217+
2218+
/**
2219+
* @group 40 removeEmptyData
2220+
*
2221+
* Flows:
2222+
* 1 - data is empty, return immediately
2223+
* 2 - loop key/value - value (is not an array and (not empty or (is a boolean & not in comparison array)))
2224+
* 3 - loop key/value - value is not an array
2225+
* 4 - after filtering value, value is empty
2226+
* 5 - value is an array containing recursive data (subject to 1 through 4)
2227+
*
2228+
* This test does #2 (twice, once for 'true', once for 'false')
2229+
*/
2230+
public function testFilterEmptyEntriesFromDataByOptionWhenValueBooleanNotInComparison()
2231+
{
2232+
$event = $this->createGroup40Event(
2233+
[
2234+
'foo' => true,
2235+
]
2236+
);
2237+
2238+
$listener = $this->createGroup40Listener();
2239+
$listener->onRoute($event);
2240+
2241+
$this->assertEquals(
2242+
[
2243+
'foo' => true,
21712244
],
2172-
];
2245+
$event->getParam('ZFContentNegotiationParameterData')->getBodyParams()
2246+
);
21732247

2174-
$dataParams = new ParameterDataContainer();
2175-
$dataParams->setBodyParams($params);
2248+
$event2 = $this->createGroup40Event(
2249+
[
2250+
'foo' => false,
2251+
]
2252+
);
21762253

2177-
$event = new MvcEvent();
2178-
$event->setRequest($request);
2179-
$event->setRouteMatch($matches);
2180-
$event->setParam('ZFContentNegotiationParameterData', $dataParams);
2254+
$listener2 = $this->createGroup40Listener();
2255+
$listener2->onRoute($event2);
2256+
2257+
$this->assertEquals(
2258+
[
2259+
'foo' => false,
2260+
],
2261+
$event2->getParam('ZFContentNegotiationParameterData')->getBodyParams()
2262+
);
2263+
}
2264+
2265+
/**
2266+
* @group 40 removeEmptyData
2267+
*
2268+
* Flows:
2269+
* 1 - data is empty, return immediately
2270+
* 2 - loop key/value - value (is not an array and (not empty or (is a boolean & not in comparison array)))
2271+
* 3 - loop key/value - value is not an array
2272+
* 4 - after filtering value, value is empty
2273+
* 5 - value is an array containing recursive data (subject to 1 through 4)
2274+
*
2275+
* This test does #3
2276+
*/
2277+
public function testFilterEmptyEntriesFromDataByOptionWhenValueNotAnArray()
2278+
{
2279+
$event = $this->createGroup40Event(
2280+
[
2281+
'foo' => ' string ',
2282+
]
2283+
);
2284+
2285+
$listener = $this->createGroup40Listener();
2286+
$listener->onRoute($event);
2287+
2288+
$this->assertEquals(
2289+
[
2290+
'foo' => 'string',
2291+
],
2292+
$event->getParam('ZFContentNegotiationParameterData')->getBodyParams()
2293+
);
2294+
}
2295+
2296+
/**
2297+
* @group 40 removeEmptyData
2298+
*
2299+
* Flows:
2300+
* 1 - data is empty, return immediately
2301+
* 2 - loop key/value - value (is not an array and (not empty or (is a boolean & not in comparison array)))
2302+
* 3 - loop key/value - value is not an array
2303+
* 4 - after filtering value, value is empty
2304+
* 5 - value is an array containing recursive data (subject to 1 through 4)
2305+
*
2306+
* This test does #4
2307+
*/
2308+
public function testFilterEmptyEntriesFromDataByOptionWhenValueEmptyAfterFilter()
2309+
{
2310+
$event = $this->createGroup40Event(
2311+
[
2312+
'foo' => [
2313+
'test' => []
2314+
],
2315+
]
2316+
);
2317+
2318+
$listener = $this->createGroup40Listener();
2319+
$listener->onRoute($event);
2320+
2321+
$this->assertEquals(
2322+
[],
2323+
$event->getParam('ZFContentNegotiationParameterData')->getBodyParams()
2324+
);
2325+
}
2326+
2327+
/**
2328+
* @group 40 removeEmptyData
2329+
*
2330+
* Flows:
2331+
* 1 - data is empty, return immediately
2332+
* 2 - loop key/value - value (is not an array and (not empty or (is a boolean & not in comparison array)))
2333+
* 3 - loop key/value - value is not an array
2334+
* 4 - after filtering value, value is empty
2335+
* 5 - value is an array containing recursive data (subject to 1 through 4)
2336+
*
2337+
* This test does #5
2338+
*/
2339+
public function testFilterEmptyEntriesFromDataByOptionWithNestedData()
2340+
{
2341+
$event = $this->createGroup40Event(
2342+
[
2343+
'foo' => ' abc ',
2344+
'empty' => null,
2345+
'empty_array' => [
2346+
'empty_field' => null,
2347+
],
2348+
]
2349+
);
2350+
2351+
$listener = $this->createGroup40Listener();
21812352

21822353
$listener->onRoute($event);
21832354

@@ -2190,7 +2361,7 @@ public function testFilterEmptyEntriesFromDataByOptionWithNestedData()
21902361
[
21912362
'foo' => 'abc',
21922363
],
2193-
$dataParams->getBodyParams()
2364+
$event->getParam('ZFContentNegotiationParameterData')->getBodyParams()
21942365
);
21952366
}
21962367

0 commit comments

Comments
 (0)