Verified Commit ec6a5fb5 authored by Dave Long's avatar Dave Long
Browse files

test: #3561844 Refactor tests to use stubs instead of mocks where mocks do not...

test: #3561844 Refactor tests to use stubs instead of mocks where mocks do not configure expectations - in component tests

By: mondrake
By: dcam
By: mstrelan
parent 126e7745
Loading
Loading
Loading
Loading
Loading
+7 −7
Original line number Diff line number Diff line
@@ -9,8 +9,10 @@
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\PreserveGlobalState;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
use PHPUnit\Framework\MockObject\Stub;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * Tests the Time class.
@@ -24,11 +26,9 @@
class TimeTest extends TestCase {

  /**
   * The mocked request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack|\PHPUnit\Framework\MockObject\MockObject
   * The request stack stub.
   */
  protected $requestStack;
  protected RequestStack&Stub $requestStack;

  /**
   * The mocked time class.
@@ -43,7 +43,7 @@ class TimeTest extends TestCase {
  protected function setUp(): void {
    parent::setUp();

    $this->requestStack = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')->getMock();
    $this->requestStack = $this->createStub(RequestStack::class);
    $this->time = new Time($this->requestStack);
  }

@@ -57,7 +57,7 @@ public function testGetRequestTime(): void {
    $request->server->set('REQUEST_TIME', $expected);

    // Mocks a the request stack getting the current request.
    $this->requestStack->expects($this->any())
    $this->requestStack
      ->method('getCurrentRequest')
      ->willReturn($request);

@@ -74,7 +74,7 @@ public function testGetRequestMicroTime(): void {
    $request->server->set('REQUEST_TIME_FLOAT', $expected);

    // Mocks a the request stack getting the current request.
    $this->requestStack->expects($this->any())
    $this->requestStack
      ->method('getCurrentRequest')
      ->willReturn($request);

+3 −0
Original line number Diff line number Diff line
@@ -46,6 +46,9 @@ public function testGetContextValue($expected, $context_value, $is_required, $da
      $ref_context_value = new \ReflectionProperty($mock_context, 'contextValue');
      // Set contextValue to a testable state.
      $ref_context_value->setValue($mock_context, $context_value);
      // Set expectation for getContextDefinition().
      $mock_context->expects($this->never())
        ->method('getContextDefinition');
      // Exercise getContextValue().
      $this->assertEquals($context_value, $mock_context->getContextValue());
    }
+1 −3
Original line number Diff line number Diff line
@@ -58,11 +58,9 @@ public function testGetPluginClassWithMissingClassWithArrayPluginDefinition(): v
   * Tests getPluginClass() with a missing class definition.
   */
  public function testGetPluginClassWithMissingClassWithObjectPluginDefinition(): void {
    $plugin_definition = $this->getMockBuilder(PluginDefinitionInterface::class)
      ->getMock();
    $this->expectException(PluginException::class);
    $this->expectExceptionMessage('The plugin (corn) did not specify an instance class.');
    DefaultFactory::getPluginClass('corn', $plugin_definition);
    DefaultFactory::getPluginClass('corn', $this->createStub(PluginDefinitionInterface::class));
  }

  /**
+2 −2
Original line number Diff line number Diff line
@@ -34,8 +34,8 @@ public function testGetDefinitions(): void {
      'id' => 'bar',
      'class' => 'com\example\PluginNamespace\AttributeDiscoveryTest1',
    ];
    $discovery = $this->createMock(DiscoveryInterface::class);
    $discovery->expects($this->any())
    $discovery = $this->createStub(DiscoveryInterface::class);
    $discovery
      ->method('getDefinitions')
      ->willReturn($definitions);

+2 −0
Original line number Diff line number Diff line
@@ -61,6 +61,8 @@ public function testGetDefinition($expected, $cached_definitions, $get_definitio
        });
    }
    else {
      $trait->expects($this->never())
        ->method('getDefinitions');
      // Put $cached_definitions into our mocked ::$definitions.
      $reflection_definitions->setValue($trait, $cached_definitions);
    }
Loading