Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 12 | class JsonExtensionTest extends \PHPUnit_Framework_TestCase |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var Form |
||
| 16 | */ |
||
| 17 | private $form; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var Form |
||
| 21 | */ |
||
| 22 | private $formWithoutJson; |
||
| 23 | |||
| 24 | public function setUp() |
||
| 25 | { |
||
| 26 | $formFactory = Forms::createFormFactoryBuilder() |
||
| 27 | ->addExtension(new JsonExtension()) |
||
| 28 | ->getFormFactory(); |
||
| 29 | $this->form = $formFactory |
||
| 30 | ->createBuilder( |
||
| 31 | FormType::class, |
||
| 32 | null, |
||
| 33 | ['json_format' => true] |
||
| 34 | ) |
||
| 35 | ->add('name', TextType::class) |
||
| 36 | ->add('lastname', TextType::class) |
||
| 37 | ->getForm(); |
||
| 38 | $this->formWithoutJson = $formFactory |
||
| 39 | ->createBuilder(FormType::class) |
||
| 40 | ->add('name', TextType::class) |
||
| 41 | ->add('lastname', TextType::class) |
||
| 42 | ->getForm(); |
||
| 43 | } |
||
| 44 | |||
| 45 | public function testSubmitValidJsonShouldPopulateForm() |
||
| 46 | { |
||
| 47 | $this->form->submit('{ "name": "test1" }'); |
||
| 48 | $this->assertEquals(['name' => 'test1', 'lastname' => null], $this->form->getData()); |
||
| 49 | $this->assertEquals(['name' => 'test1', 'lastname' => null], $this->form->getNormData()); |
||
| 50 | $this->assertEquals(['name' => 'test1', 'lastname' => null], $this->form->getViewData()); |
||
| 51 | } |
||
| 52 | |||
| 53 | public function testSubmitInvalidJsonShouldThrowException() |
||
| 54 | { |
||
| 55 | $this->setExpectedExceptionRegExp( |
||
| 56 | 'InvalidArgumentException', |
||
| 57 | '/^Invalid submitted json data, error (.*) : (.*), json : invalid json$/' |
||
| 58 | ); |
||
| 59 | $this->form->submit('invalid json'); |
||
| 60 | } |
||
| 61 | |||
| 62 | public function testFormWithoutJsonShouldWorkNormally() |
||
| 63 | { |
||
| 64 | $this->formWithoutJson->submit(['name' => 'test1']); |
||
| 65 | $this->assertEquals(['name' => 'test1', 'lastname' => null], $this->formWithoutJson->getData()); |
||
| 66 | $this->assertEquals(['name' => 'test1', 'lastname' => null], $this->formWithoutJson->getNormData()); |
||
| 67 | $this->assertEquals(['name' => 'test1', 'lastname' => null], $this->formWithoutJson->getViewData()); |
||
| 68 | } |
||
| 69 | |||
| 70 | public function testSubmitWithoutStringShouldThrowException() |
||
| 71 | { |
||
| 72 | $this->setExpectedExceptionRegExp( |
||
| 73 | 'InvalidArgumentException', |
||
| 74 | '/^Invalid argument: the submitted variable must be a string when you enable the json_format option.$/' |
||
| 75 | ); |
||
| 76 | $this->form->submit(['invalid json']); |
||
| 77 | } |
||
| 78 | |||
| 79 | public function testRequestWithValidJsonShouldPopulateForm() |
||
| 80 | { |
||
| 81 | $request = $this->getRequest('{ "name": "test1" }'); |
||
| 82 | $this->form->handleRequest($request); |
||
| 83 | $this->assertEquals(['name' => 'test1', 'lastname' => null], $this->form->getData()); |
||
| 84 | $this->assertEquals(['name' => 'test1', 'lastname' => null], $this->form->getNormData()); |
||
| 85 | $this->assertEquals(['name' => 'test1', 'lastname' => null], $this->form->getViewData()); |
||
| 86 | } |
||
| 87 | |||
| 88 | public function testRequestWithInvalidJsonShouldTHrowException() |
||
| 89 | { |
||
| 90 | $this->setExpectedExceptionRegExp( |
||
| 91 | 'InvalidArgumentException', |
||
| 92 | '/^Invalid submitted json data, error (.*) : (.*), json : invalid json$/' |
||
| 93 | ); |
||
| 94 | $request = $this->getRequest('invalid json'); |
||
| 95 | $this->form->handleRequest($request); |
||
| 96 | } |
||
| 97 | |||
| 98 | public function testRequestWithoutStringShouldThrowException() |
||
| 99 | { |
||
| 100 | $this->setExpectedExceptionRegExp( |
||
| 101 | 'InvalidArgumentException', |
||
| 102 | '/^Invalid argument: the submitted variable must be a string when you enable the json_format option.$/' |
||
| 103 | ); |
||
| 104 | $request = $this->getRequest(['test']); |
||
| 105 | $this->form->handleRequest($request); |
||
| 106 | } |
||
| 107 | |||
| 108 | protected function getRequest($content) |
||
| 109 | { |
||
| 110 | return new Request( |
||
| 111 | [], |
||
| 112 | [], |
||
| 113 | [], |
||
| 114 | [], |
||
| 115 | [], |
||
| 116 | ['CONTENT_TYPE' => 'application/json'], |
||
| 117 | $content |
||
| 118 | ); |
||
| 119 | } |
||
| 120 | } |
||
| 121 |