Perhaps I'm doing something wrong, but here's the situation: I have an
entity with a file constraint to accept only .csv files:

<?php
namespace MN\ImportBundle\Entity;

use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;

class Import {

    /**
     * @Assert\NotBlank()
     * @Assert\MinLength(5)
     */
    private $name;

    /**
     * @Assert\NotBlank()
     * @Assert\File(mimeTypes = {"text/comma-separated-values"})
     */
    private $file;

    /**
     * @Assert\NotBlank()
     * @Assert\Choice(choices = {"insight","stratech"})
     */
    private $source;

    public function setName($name) {
        $this->name = $name;
        return $this;
    }

    public function setFile(File $file) {
        $this->file = $file;
        return $this;
    }

    public function setSource($source) {
        $this->source = $source;
        return $this;
    }

    public function getName() {
        return $this->name;
    }

    public function getFile() {
        return $this->file;
    }

    public function getSource() {
        return $this->source;
    }
}
?>

I wrote a test to see if the constraint actually works, but it
doesn't:

<?php

namespace MN\ImportBundle\Tests\Entity;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use MN\ImportBundle\Entity\Import;
use Symfony\Component\HttpFoundation\File\File;

class ImportTest extends WebTestCase {

    private $entity;
    private $validator;

    public function setUp() {
        $this->entity = new Import();

        $this->entity->setName('123456');
        $this->entity->setSource('insight');

        $client = $this->createClient();
        $this->validator = $client->getContainer()->get('validator');
    }

    public function testValidatorDoesntAcceptTxt() {

        $file = new File(__DIR__.'/../Resources/BogusFile.txt');

        $this->entity->setFile($file);

        $errorList = $this->validator->validate($this->entity);

        $this->assertEquals('The mime type of the file is invalid
("text/plain"). Allowed mime types are "text/comma-separated-values"',
$errorList[0]->getMessage());
    }

    public function testValidatorDoesAcceptCsv() {

        $file = new File(__DIR__.'/../Resources/
CsvParserWithHeaderDataTest.csv');

        $this->entity->setFile($file);

        $errorList = $this->validator->validate($this->entity);

        $this->assertEquals(0, count($errorList));
    }

}
?>

When I run PHPUnit, I get the following output:


bart@burgov:~/websites/[...]$ phpunit -c app src/MN/ImportBundle/Tests/
Entity/ImportTest.php
PHPUnit 3.5.13 by Sebastian Bergmann.

.F

Time: 0 seconds, Memory: 8.25Mb

There was 1 failure:

1) MN\ImportBundle\Tests\Entity\ImportTest::testValidatorDoesAcceptCsv
Failed asserting that <integer:1> matches expected <integer:0>.

/home/bart/websites/[...]/src/MN/ImportBundle/Tests/Entity/
ImportTest.php:43

FAILURES!
Tests: 2, Assertions: 2, Failures: 1.

When I do some debugging, the error in the errorlist appears to be:
The mime type of the file is invalid ("text/plain"). Allowed mime
types are "text/comma-separated-values"
Exactly equal to the (expected) error in the first test.

Why is my file not recognized as a .csv file?

-- 
If you want to report a vulnerability issue on symfony, please send it to 
security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "symfony users" group.
To post to this group, send email to symfony-users@googlegroups.com
To unsubscribe from this group, send email to
symfony-users+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/symfony-users?hl=en

Reply via email to