PhpUnit Extras

Using mockObjectsMap in Tests

<?php

namespace AKlump\PHPUnit\Test\EasyMock;

use AKlump\PHPUnit\EasyMockTrait;
use PHPUnit\Framework\TestCase;

Here is the Test

The mockObjectsMap portion of getSchema provides a means of having one or more mocked objects automatically created before the start of each test method.

class MockObjectsMapUnitTest extends TestCase {

  use EasyMockTrait;

  /**
   * {@inheritdoc}
   *
   * Demonstrate the use of mockObjectsMap, which creates mocked instances of a
   * set of classes and adds them to the test class as properties.
   */
  protected function getSchema() {
    return [
      'classToBeTested' => "AKlump\PHPUnit\Test\EasyMock\Charlie",
      'mockObjectsMap' => [
        'delta' => '\AKlump\PHPUnit\Test\EasyMock\Delta',
      ],
    ];
  }

  public function testCanSetDelta() {
    $this->delta->allows('getName')->andReturns('Juliette');

    $this->obj->setDelta($this->delta);
    $this->assertSame('Juliette', $this->obj->getDelta()->getName());
  }

}

Class Being Tested

class Charlie {

  private $delta;

  public function setDelta(Delta $delta) {
    $this->delta = $delta;

    return $this;
  }

  public function getDelta() {
    return $this->delta;
  }

}

Class Being Mocked

This class gets added in the mockObjectsMap array and then automatically mocked to $this->delta.

class Delta {

  public function getName() {
    return 'Frank';
  }

}