dimanche 30 septembre 2018

testing a method with events in a repository with phpunit laravel

Hello everyone I am looking for help as I am starting on unit testing Laravel.

Here is the repository method I am testing =>

   public function store(array $input){

            $input['billing_contact'] = (isset($input['billing_contact'])) ? 1 : 0;  
            \DB::beginTransaction();
            try {
                $new_contact = $this->model->create($input);

                //EVENT CREATE ACCOUNT (USER)
                event(new EventCreateUserWithContact($new_contact));
                \DB::commit();
            }
            catch(\Exception $e){
                \DB::rollback();
                return false;
            }


            return $new_contact;

        }

Here is the test that I am trying to make =>

class ContactTest extends TestCase
{
    use WithFaker;
    protected $contact;/**/
    protected $container;/**/

    public function setup()
    {
        parent::setup();
        $this->container = new \Illuminate\Container\Container();
        $this->contact = new Contact();
        DB::beginTransaction();
    }
    public function tearDown()
    {
        DB::rollback();
        parent::tearDown();
    }
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testContactRepo()
    {
//        Event::();
        $publisher = factory(\App\Models\Publisher::class)->create();
        $contact =  factory(\App\Models\Contact::class)->create();


//
        $data =  [

            'first_name' => $this->faker->word,
            'last_name'=>  $this->faker->word,
            'email' => $this->faker->unique()->safeEmail,
            'phone'=> 112321321,
            'job'=> $this->faker->word,
            'billing_contact'=> $this->faker->word,
            'approve_delete_at'=> $this->faker->date('Y-m-d',  '1461067200'),
            'publisher_id'=> $publisher->id,

        ];

        $rep = new ContactRepositoryEloquent($this->container);

        $contact = $rep->store($data);
    dd($contact);


    }
}

I do not understand how to get to => " return $new_contact;" I got an exception error when I run the test (I get false) Do I need to create a fake event to make it works?

Thank you again for your help[ it will be much appreciated

Aucun commentaire:

Enregistrer un commentaire