Outils pour utilisateurs

Outils du site


pele_mele:stack_exchange

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
pele_mele:stack_exchange [2020/11/18 03:39] – [Seasoned Advice] alexispele_mele:stack_exchange [2024/11/23 03:59] (Version actuelle) alexis
Ligne 1: Ligne 1:
 ====== Stack Exchange ====== ====== Stack Exchange ======
-<WRAP todo> 
-Ajouter toutes les questions 
-</WRAP> 
- 
 Cette page contient les réponses à toutes les questions que j'ai posé sur l'ensemble des sites de [[https://stackexchange.com/|Stack Exchange]]. Cette page contient les réponses à toutes les questions que j'ai posé sur l'ensemble des sites de [[https://stackexchange.com/|Stack Exchange]].
 En tout cas, toutes celles des questions qui méritent d'être conservées si le site venait à disparaître. En tout cas, toutes celles des questions qui méritent d'être conservées si le site venait à disparaître.
  
-===== Seasoned Advice ===== +<catlist pele_mele:stack_exchange -sortByTitle -noHead>
-  - [[pele_mele:stack_exchange:cooking-78840|How to modify a bread recipe to accomodate longer proofing times?]] +
-  [[pele_mele:stack_exchange:cooking-79458|Why is eating pretzels safe when they are bathed in lye?]] +
-  - [[pele_mele:stack_exchange:cooking-79461|How to bake croissant?]] +
-  - [[pele_mele:stack_exchange:cooking-82283|Does beating hot oil into eggs before cooking an omelette make a difference?]] +
-  - [[pele_mele:stack_exchange:cooking-84099|Looking for a Finnish bread name and recipe]] +
-  - [[pele_mele:stack_exchange:cooking-91741|What pepper is used in Sriracha sauce?]] +
-  - [[pele_mele:stack_exchange:cooking-102288|Can I ferment blanched cabbage?]] +
-  - [[pele_mele:stack_exchange:cooking-110527|Are dried elderberries safe to eat?]] +
- +
-===== Stack Overflow ===== +
-  - [[pele_mele:stack_exchange:stackoverflow-27801219|How to unit test a Symfony2 form when it uses a transformer linked to a database]] +
-  - [[pele_mele:stack_exchange:stackoverflow-27972741|Is there a better way to handle the Doctrine proxy object]] +
-  - [[pele_mele:stack_exchange:stackoverflow-28071372|How to test a concrete method calling an abstract method with phpunit]] +
-  - [[pele_mele:stack_exchange:stackoverflow-29131903|How to revert a chunk of a file in the staging area?]] +
-  - [[pele_mele:stack_exchange:stackoverflow-29661578|How to handle a POST method that generate multiple resource with REST?]] +
-  - [[pele_mele:stack_exchange:stackoverflow-29779716|Symfony form validation of integer field]] +
-  - [[pele_mele:stack_exchange:stackoverflow-29987139|Symfony2 - Collection type is displayed twice in my view]] +
-  - [[pele_mele:stack_exchange:stackoverflow-35478874|How to choose the driver to serialize/deserialize with jmsserializer]] +
-  - [[pele_mele:stack_exchange:stackoverflow-44944603|Talend warning and error messages are black]] +
-  - [[pele_mele:stack_exchange:stackoverflow-47312136|How to use a variable as user name in a SF memory provider?]] +
-  - [[pele_mele:stack_exchange:stackoverflow-50909507|List available workflows in Symfony]] +
-  - [[pele_mele:stack_exchange:stackoverflow-62270358|How to define a macro to interpret a unicode character with Mathjax]] +
- +
-<WRAP group> +
-<WRAP half column> +
-==== How to unit test a Symfony2 form when it uses a transformer linked to a database ==== +
- +
-TLDR: I am new to unit tests and I have few questions: +
- +
-  - Are my transformer tests well written? +
-  - Is there a way to decoupled my transformer tests from the database? +
-  - How to test my form with the transformer using the database? +
-  - Should I decouple my form from my transformer? +
- +
-------- +
-I don't know if my classes are too coupled, if my design is flawed or if my understanding of the unit tests is bad. +
- +
-Here is some background.   +
-I have a form object with different widgets. One of them is used within a model transformer.   +
-This model transformer uses a connection to the database to retrieve the proper object. +
- +
-Here is my code: +
- +
-<code php> +
-class BookToStringTransformer implements DataTransformerInterface { +
-    private $om; +
- +
-    public function __construct(ObjectManager $om) { +
-        $this->om = $om; +
-    } +
- +
-    public function transform($book) { +
-        if (!$book instanceof Book) { +
-            return ""; +
-        } +
- +
-        return $book->getName(); +
-    } +
- +
-    public function reverseTransform($string) { +
-        if (!is_string($string) || !$string) { +
-            return null; +
-        } +
- +
-        $book = $this->om +
-                ->getRepository('MyBundle:Book'+
-                ->findOneBy(array('name' => $string)) +
-        ; +
- +
-        if (null === $book) { +
-            throw new TransformationFailedException(sprintf( +
-                    'The book "%s" does not exist!', $string +
-            )); +
-        } +
- +
-        return $book; +
-    } +
-+
- +
-class ItemType extends AbstractType { +
-    private $om; +
- +
-    public function __construct(ObjectManager $om) { +
-        $this->om = $om; +
-    } +
- +
-    public function buildForm(FormBuilderInterface $builder, array $options) { +
-        $bookTransformer = new BookToStringTransformer($this->om); +
-        $builder->add($builder->create('book', 'text', array( +
-                    'required' => false, +
-                ))->addModelTransformer($bookTransformer)); +
-    } +
- +
-    public function setDefaultOptions(OptionsResolverInterface $resolver) { +
-        $resolver->setDefaults(array( +
-            'data_class' => 'MyBundle\Entity\Item', +
-        )); +
-    } +
- +
-    public function getName() { +
-        return 'mybundle_item'; +
-    } +
-+
-</code> +
- +
-I wrote unit tests for the transformer using the KernelTestCase +
- +
-<code php> +
-class BookToStringTransformerTest extends KernelTestCase { +
-    private $name = 'existing name'; +
-    private $em; +
- +
-    public function setUp() { +
-        static::$kernel = static::createKernel(); +
-        static::$kernel->boot(); +
-        $this->em = static::$kernel->getContainer() +
-                ->get('doctrine'+
-                ->getManager(); +
-    } +
- +
-    public function testReverseTransform_whenNameExists_returnsBookObject() { +
-        $transformer = new BookToStringTransformer($this->em); +
-        $book = $transformer->reverseTransform($this->name); +
-        $this->assertInstanceOf('MyBundle\Entity\Book', $book, 'Should return a Book object'); +
-        $this->assertEquals($this->name, $book->getName(), 'Should return a Book object with the selected name'); +
-    } +
- +
-    /** +
-     * @expectedException Symfony\Component\Form\Exception\TransformationFailedException +
-     */ +
-    public function testReverseTransform_whenNameDoesNotExist_throwsException() { +
-        $transformer = new BookToStringTransformer($this->em); +
-        $transformer->reverseTransform('unknown name'); +
-    } +
- +
-    /** +
-     * @param mixed $invalid_parameter +
-     * @dataProvider provideInvalidParameter +
-     */ +
-    public function testReverseTransform_whenParameterIsInvalid_returnsNull($invalid_parameter) { +
-        $om = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')->getMock(); +
-        $transformer = new BookToStringTransformer($om); +
-        $this->assertNull($transformer->reverseTransform($invalid_parameter), 'Should return a NULL value'); +
-    } +
- +
-    /** +
-     * @return array +
-     */ +
-    public function provideInvalidParameter() { +
-        return [ +
-            [null], +
-            [false], +
-            [true], +
-            [''], +
-            [[]], +
-            [new \stdClass()], +
-        ]; +
-    } +
- +
-    public function testTransform_whenParameterIsBookObject_returnsName() { +
-        $book = $this->em->getRepository('MyBundle:Book'+
-                ->findOneBy(array('name' => $this->name)); +
-        $om = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')->getMock(); +
-        $transformer = new BookToStringTransformer($om); +
-        $this->assertEquals($this->name, $transformer->transform($book), 'Should return a string containing the name'); +
-    } +
- +
-    /** +
-     * @param mixed $not_book +
-     * @dataProvider provideInvalidBookObject +
-     */ +
-    public function testTransform_whenParameterIsNotBookObject_returnsEmptyString($not_book) { +
-        $om = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')->getMock(); +
-        $transformer = new BookToStringTransformer($om); +
-        $this->assertEquals("", $transformer->transform($not_book), 'Should return an empty string to be chained'); +
-    } +
- +
-    /** +
-     * @return array +
-     */ +
-    public function provideInvalidBookObject() { +
-        return [ +
-            [null], +
-            [123], +
-            ['123'], +
-            [[]], +
-            [true], +
-            [new \stdClass()], +
-        ]; +
-    } +
-+
-</code> +
- +
-As I am new to unit tests, I don't even know if it is the proper way to test that transformer.   +
-I start writing tests for the form object. I am using the TypeTestCase but there is no simple way to get the connection to the database and I can't use the KernelTestCase. +
- +
-<code php> +
-class ItemTypeTest extends TypeTestCase { +
-    /** +
-     * @expectedException \PHPUnit_Framework_Error +
-     */ +
-    public function test_whenCreatedWithNoParameters_raiseException() { +
-        new ItemType(); +
-    } +
- +
-    /** +
-     * @expectedException \PHPUnit_Framework_Error +
-     */ +
-    public function test_whenCreatedWithBadParameters_raiseException() { +
-        new ItemType(123); +
-    } +
- +
-    public function test_whenCreatedWithGoodParameters_createsFormObject() { +
-        $om = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')->getMock(); +
-        $type = new ItemType($om); +
-        $form = $this->factory->create($type); +
-        $this->assertInstanceOf('Symfony\Component\Form\Form', $form); +
-    } +
- +
-    public function test_whenSubmittedWithGoodData() { +
-        $formData = array( +
-            'name' => 'existing name', +
-        ); +
- +
-        $om = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')->getMock(); +
-        $type = new ItemType($om); +
-        $form = $this->factory->create($type); +
- +
-        $form->submit($formData); +
-    } +
-+
-</code> +
- +
-The last test fails because the transformer does get access to the database since I am passing a mock to the form. So should I get a real object (meaning classes are too coupled) or should I find an other way. +
- +
-Thank you +
-<WRAP help> +
-The approach is good, in the last method you must mock the repo object and the repo response. In example try this code: +
-<code php> +
-public function test_whenSubmittedWithGoodData() { +
-    $formData = array( +
-        'name' => 'existing name', +
-    ); +
- +
-    $om = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')->getMock(); +
- +
-    $repoMock= $this->getMock('Doctrine\ORM\EntityRepository', array(), array(), '', false); +
- +
-    $om +
-        ->expects($this->atLeastOnce()) +
-        ->method('getRepository'+
-        ->withAnyParameters() +
-        ->will($this->returnValue($repoMock)); +
- +
- +
-    $repoMock +
-        ->expects($this->atLeastOnce()) +
-        ->method('findOneBy'+
-        ->withAnyParameters() +
-        ->will($this->returnValue($mockedBook)); +
- +
-    $type = new ItemType($om); +
-    $form = $this->factory->create($type); +
- +
-    $form->submit($formData); +
-+
-</code> +
-</WRAP> +
-<WRAP info> +
-[[https://stackoverflow.com/questions/27801219/how-to-unit-test-a-symfony2-form-when-it-uses-a-transformer-linked-to-a-database|How to unit test a Symfony2 form when it uses a transformer linked to a database - Stack Overflow]] +
-</WRAP> +
-</WRAP> +
- +
-<WRAP half column> +
-==== Is there a better way to handle the Doctrine proxy object ==== +
-</WRAP> +
-</WRAP> +
-===== Super User ===== +
-  - [[pele_mele:stack_exchange:superuser-1225022-|Send notification from systemd]] +
- +
-===== TeX - LaTeX ===== +
-  - [[pele_mele:stack_exchange:tex-367891|Is there a better way for drawing my table]] +
-  - [[pele_mele:stack_exchange:tex-370684|Is there a better way to repeat the content of a page?]] +
-  - [[pele_mele:stack_exchange:tex-374734|Why is there a padding only on the first row of a repeating pattern?]] +
-  - [[pele_mele:stack_exchange:tex-377681|How to keep cell border with a column background color?]] +
-  - [[pele_mele:stack_exchange:tex-391286|Apostrophe is not showing as an apostrophe [closed]]] +
-  - [[pele_mele:stack_exchange:tex-391569|Why the first tikz element is a bit off?]] +
-  - [[pele_mele:stack_exchange:tex-392900|How positioning works with tikz]] +
- +
  
pele_mele/stack_exchange.1605699541.txt.gz · Dernière modification : 2020/11/18 03:39 de alexis