otherjohn's avatar

How would you write a phpspec test for this?

Hi, I have the following code in a Repository I would like to learn how to unit test for the create() method. I want to test the firing of the event and the saving of the account. I beleive this is more of a functional test but I want to learn how to write this properly as a unit test. Can some give me some direction?

class AccountRepository implements AccountInterface
{
...
...
 public function __construct(\Account $account, Dispatcher $event)
 {
        $this->model = $account;
        $this->event = $event;
 }

 public function create($input,$user = null)
{
    $this->event->fire('account.creating');//, $input);
    $account = new \Account;

    $account->owner_id = ($user!=null) ? $user->id : 0;
    $account->name     = array_get($input, 'company');
    ($user==null)? : $account->users()->associate($user);
    $account->save();
    $this->event->fire('account.created', [$input, $account]);
    return $account;
}

and this is my spec test

class AccountRepositorySpec extends ObjectBehavior
{
    function let(\Account $account,Dispatcher $event)
    {
        $this->beConstructedWith($account, $event);
    }
 function it_fires_event_when_creating_account(Dispatcher $event, Account $account)
    {
 $event->fire(Argument::any())->shoulRecieve('fire');
$data = [
            'name'        => 'Acme',
            'owner_id' => '999',

        ];

        $this->create($data);
}
}
0 likes
0 replies

Please or to participate in this conversation.