PHPunit: Form Ancestor Not Found

Asked
Active3 hr before
Viewed126 times

5 Answers

ancestor
90%

possible duplicate of The selected node does not have a form ancestor - Mink error – Limon Monte Jun 4 '15 at 9:20 , Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers , The shape and extension of the fundamental particles , By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

I have a simple form:

      <!--NEW FORM-->
      <form method="POST" action="http://192.168.114.139/family/search_results" accept-charset="UTF-8" id="admin-form"><input name="_token" type="hidden" value="7v2IhbTkiMRVyojJ8Vuv0dDdRqySzO18Z6gPWKP2">

         <input class="gui-input" id="search_term" placeholder="" name="search_term" type="text">

         <div class="panel-footer text-right">
            <input class="button btn-primary" name="submit" id="search" type="submit" value="Search">
            <button class="button btn-danger" type="reset">Reset</button>
         </div>
      </form>
load more v
88%

TL;DR: use different driver or make sure form elements are actually inside the form.,How to get this work? Why does Mink actually need a form to click a button element? Or is that the problem connected with Goutte not being able to fetch JS?,These actions helped:,In order to create that form object it searches for the closest parent form to get the basic form details. So, when you try to set a value on the input that is not part of the form, or do a submit without a form – it can't find the form and throws an exception.

Code:

$page = $this - > getSession() - > getPage();
$checkout = $page - > find('css', '.btn-proceed-checkout');
$checkout - > click();
72%

I am using sonata admin bundle and I am creating the test with behat to be sure that everything is working fine. But the problem is that when I try to add a new element using behat I get this error:,The problem with sonata was that I have this element in the form and it seems that create an incorrect form :,The problem is because this button is out of the form but this is managed by sonata admin. So is there any way to click this button without get this error?,I change for this one and right now is working:

- > add('image', 'sonata_type_model_autocomplete', ['required' => false, 'property' => 'name']);
load more v
65%

Depending on the way you installed your Symfony or Symfony Form component the tests may not be downloaded. Use the --prefer-source option with Composer if this is the case.,By default only the CoreExtension is registered in tests. You can find other extensions from the Form component in the Symfony\Component\Form\Extension namespace.,To solve this, you have to mock the injected dependencies, instantiate your own form type and use the PreloadedExtension to make sure the FormRegistry uses the created instance:,The Form component consists of 3 core objects: a form type (implementing FormTypeInterface), the Form and the FormView.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// tests/Form/Type/TestedTypeTest.php
namespace App\ Tests\ Form\ Type;

use App\ Form\ Type\ TestedType;
use App\ Model\ TestObject;
use Symfony\ Component\ Form\ Test\ TypeTestCase;

class TestedTypeTest extends TypeTestCase {
   public
   function testSubmitValidData() {
      $formData = [
         'test' => 'test',
         'test2' => 'test2',
      ];

      $model = new TestObject();
      // $model will retrieve data from the form submission; pass it as the second argument
      $form = $this - > factory - > create(TestedType::class, $model);

      $expected = new TestObject();
      // ...populate $object properties with the data stored in $formData

      // submit the data to the form directly
      $form - > submit($formData);

      // This check ensures there are no transformation failures
      $this - > assertTrue($form - > isSynchronized());

      // check that $model was modified as expected when the form was submitted
      $this - > assertEquals($expected, $model);
   }

   public
   function testCustomFormView() {
      $formData = new TestObject();
      // ... prepare the data as you need

      // The initial data may be used to compute custom view variables
      $view = $this - > factory - > create(TestedType::class, $formData) -
         > createView();

      $this - > assertArrayHasKey('custom_var', $view - > vars);
      $this - > assertSame('expected value', $view - > vars['custom_var']);
   }
}
load more v
75%

To make it work I wrote a class DbTestCase that extends CDbTestCase and overrides the PHP magic methods get and call. Please find the result and how to use it here:Related forum post,Because the Yii testing framework is built on top of PHPUnit, it is recommended that you go through the PHPUnit documentation first to get the basic understanding on how to write a unit test. We summarize in the following the basic principles of writing a unit test in Yii:,In the following, we mainly describe how to write unit tests for active record model classes. We will extend our test classes from CDbTestCase because it provides the database fixture support that we introduced in the previous section.,A unit test is written in terms of a class XyzTest which extends from CTestCase or CDbTestCase, where Xyz stands for the class being tested. For example, to test the Post class, we would name the corresponding unit test as PostTest by convention. The base class CTestCase is meant for generic unit tests, while CDbTestCase is suitable for testing active record model classes. Because PHPUnit_Framework_TestCase is the ancestor class for both classes, we can use all methods inherited from this class.

class CommentTest extends CDbTestCase {
   protected $fixtures = array(
      'posts' => 'Post',
      'comments' => 'Comment',
   );

   ......
}
load more v

Other "ancestor-undefined" queries related to "PHPunit: Form Ancestor Not Found"