Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Im following the tutorial from symfony's website to make my phpunit's test for my controller.

Im trying to test an upload on a form and i have 3 fields on this form : title,description and file.

I use :

public function testScenarioAdd() {

    $client = static::createClient();

     $x_wsse = 'UsernameToken Username="[email protected]", PasswordDigest="aeirugbjcUbfmùJK", Nonce="OTMzOGMwYzFkYTk2MzJmYzBh", Created="2013-11-12T10:22:15+01:00"';
    //X_Wsse is for the connection systeme on my application.

    $image = new UploadedFile(
        '/past/to/your/images.jpg',
        'images.jpg',
        'image/jpeg',
        100000
    );

    $crawler = $client->request('POST', '/ad/create', array('ad_form' => array('title' => 'test', 'description' => 'Test description')),array(), array('CONTENT_TYPE' => 'application/x-www-form-urlencoded', 'HTTP_X-WSSE' => $x_wsse));

    $response = $client->getResponse()->getContent();
    $json_response = json_decode($response);
    print_r($response);
}

When i launch it I have an error saying : {"app_error":{"success":500,"result":[{"message":"Array to string conversion",...

Am i doing something wrong ? or missing something in my code ?

Thanks for your help :)

Edit :

Thanks but, what ive done there is working on another test without upload, i still tried it and its not working. I still have the error array to string... I think it come from the fonction UplaodedFile or maybe from my controller itself because when i try the other solution from symfony's web site with

$photo = array( 'tmp_name' => '/path/to/photo.jpg', 'name' => 'photo.jpg', 'type' => 'image/jpeg', 'size' => 123, 'error' => UPLOAD_ERR_OK );

With putting the

$crawler = $client->request('POST', '/ad/create', array('ad_form[title]' => 'test', ad_form[description] => 'test description'),array('file' => $image),array('CONTENT_TYPE' => 'application/x-www-form-urlencoded', 'HTTP_X-WSSE' => $x_wsse));

The important part of my controller is :

            if ('POST' === $request->getMethod()) {
            $form->bind($request);

            if ($form->isValid())
            {
                $Ad->preCreate();
                $odm->persist($Ad);
                $odm->flush();
                $odm->clear();

                $data = $this->listAction($request, 1, 5);
                Tags::setEvent($request, $this->container, 'member.create.ad', $user->getId(), $Ad->getId());

                return $data;

            } else {
                $data = Errors::formatJson($form, $this->container);
            }
        }
    }

    return new JsonResponse(array('ad_create' => $data));

Im kinda new in symfony, i really dont know where this error can come from...

share|improve this question

1 Answer 1

Client::Request method expects you to provide it with one-dimensional array of data. So try this code:

<?php
//...
$crawler = $client->request('POST', '/ad/create', 
    array('ad_form[title]' => 'test', 
          'ad_form[description]' => 'Test description'),
    array(), 
    array('CONTENT_TYPE' => 'application/x-www-form-urlencoded', 'HTTP_X-WSSE' => $x_wsse));
share|improve this answer
    
I found out where the problem came from : $form->bind($request); But i still dont know what's wrong... Any idea –  user2982709 Nov 13 '13 at 9:54
    
replace array('file' => $image) with array('ad_form[file]' => $image). And also UploadedFile object should be created with test=true $image = new UploadedFile( '/past/to/your/images.jpg', 'images.jpg', 'image/jpeg', 100000, null, true ); –  Ziumin Nov 13 '13 at 18:27
    
I forgot to answer ;) But yeah the problem came from the "array('file' => $image) with array('ad_form[file]' => $image)" thanks a lot ! –  user2982709 Nov 22 '13 at 15:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.