UnitTestBase.php in Select (or other) 4.x        
                          
                  
                        
  
  
  
  
File
  tests/src/Unit/UnitTestBase.php
  
    View source  
  <?php
namespace Drupal\Tests\select_or_other\Unit {
  use Drupal\Tests\UnitTestCase;
  use PHPUnit_Framework_MockObject_MockBuilder;
  use PHPUnit_Framework_MockObject_MockObject;
  use ReflectionProperty;
  use Symfony\Component\DependencyInjection\ContainerInterface;
  abstract class UnitTestBase extends UnitTestCase {
    
    protected $mockBuilder;
    
    private $containerMock;
    
    private $services;
    
    protected abstract function getTestedClassName();
    
    protected function setUp() {
      parent::setUp();
      $this
        ->prepareContainer();
      $this
        ->addMockServicesToContainer();
      $this->mockBuilder = $this
        ->getMockBuilder($this
        ->getTestedClassName());
    }
    
    private function prepareContainer() {
      $container_class = 'Drupal\\Core\\DependencyInjection\\Container';
      $methods = get_class_methods($container_class);
      
      $this->containerMock = $container = $this
        ->getMockBuilder($container_class)
        ->disableOriginalConstructor()
        ->setMethods($methods)
        ->getMock();
      \Drupal::setContainer($container);
      $this->containerMock
        ->method('get')
        ->willReturnCallback([
        $this,
        'containerMockGetServiceCallback',
      ]);
    }
    
    private function addMockServicesToContainer() {
      $this
        ->registerServiceWithContainerMock('current_user', $this
        ->getNewUserMock());
      $this
        ->registerServiceWithContainerMock('entity_type.manager', $this
        ->getNewEntityTypeManagerMock());
    }
    
    private function getNewUserMock() {
      $user_mock = $this
        ->getMockForAbstractClass('\\Drupal\\Core\\Session\\AccountProxyInterface', [
        'id',
      ]);
      $user_mock
        ->method('id')
        ->willReturn(1);
      return $user_mock;
    }
    private function getNewEntityTypeManagerMock() {
      $field_storage_config = $this
        ->getMockForAbstractClass('\\Drupal\\field\\FieldStorageConfigInterface');
      $field_storage_config
        ->method('setSetting')
        ->willReturnSelf();
      $entity_storage_methods = [
        'load' => $field_storage_config,
      ];
      $entity_type_manager_methods = [
        'getStorage' => $this
          ->getMockForAbstractClassWithMethods('\\Drupal\\Core\\Entity\\EntityStorageInterface', $entity_storage_methods),
      ];
      return $this
        ->getMockForAbstractClassWithMethods('\\Drupal\\Core\\Entity\\EntityTypeManagerInterface', $entity_type_manager_methods);
    }
    
    protected function getBasicMocks() {
      $methods = [
        'getSetting',
        'getSelectedOptions',
        'getColumn',
      ];
      $parent = $this
        ->getMockBuilder('Drupal\\select_or_other\\Plugin\\Field\\FieldWidget\\WidgetBase')
        ->setMethods($methods)
        ->disableOriginalConstructor()
        ->getMockForAbstractClass();
      $mock = $this
        ->getMockBuilder($this
        ->getTestedClassName())
        ->setMethods($methods)
        ->disableOriginalConstructor()
        ->getMock();
      $mocks = [
        $parent,
        $mock,
      ];
      $reflected_field_definition = new ReflectionProperty('Drupal\\select_or_other\\Plugin\\Field\\FieldWidget\\WidgetBase', 'fieldDefinition');
      $reflected_field_definition
        ->setAccessible(TRUE);
      
      foreach ($mocks as $current_mock) {
        $field_definition_methods = [
          'getFieldStorageDefinition' => $this
            ->getMockForAbstractClass('Drupal\\Core\\Field\\FieldStorageDefinitionInterface'),
        ];
        $field_definition = $this
          ->getMockForAbstractClassWithMethods('\\Drupal\\Core\\Field\\FieldDefinitionInterface', $field_definition_methods);
        $reflected_field_definition
          ->setValue($current_mock, $field_definition);
        $current_mock
          ->method('getSelectedOptions')
          ->willReturn([]);
        $current_mock
          ->method('getColumn')
          ->willReturn('');
      }
      return $mocks;
    }
    
    protected function getMockForAbstractClassWithMethods($abstractClassName, array $methods) {
      $mock = $this
        ->getMockForAbstractClass($abstractClassName);
      foreach ($methods as $method => $return_value) {
        $mock
          ->method($method)
          ->willReturn($return_value);
      }
      return $mock;
    }
    
    protected function registerServiceWithContainerMock($service_id, $service) {
      $this->services[$service_id] = $service;
    }
    
    public function containerMockGetServiceCallback($service_id) {
      if (isset($this->services[$service_id])) {
        return $this->services[$service_id];
      }
      return NULL;
    }
  }
}
namespace {
  if (!function_exists('t')) {
    function t($string, array $args = []) {
      return strtr($string, $args);
    }
  }
  if (!function_exists('drupal_static_reset')) {
    function drupal_static_reset() {
    }
  }
}