Skip to content

Commit

Permalink
Unit tests with Drupal 11 (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
pookmish committed Aug 29, 2024
1 parent b5defd8 commit c526857
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 23 deletions.
47 changes: 41 additions & 6 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
name: PHPUnit and Acceptance Tests
on: [push]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
phpunit:
name: PHPUnit Coverage Tests
runs-on: ubuntu-latest
container:
image: pookmish/drupal8ci:latest
image: pookmish/drupal8ci:php8.3
services:
mysql:
image: mysql:5.7
image: mysql:8.0
env:
MYSQL_DATABASE: drupal
MYSQL_USER: drupal
Expand All @@ -18,7 +21,7 @@ jobs:
- 33306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
path: project
- name: Build project
Expand All @@ -33,6 +36,38 @@ jobs:
with:
name: unit-tests-results
path: /var/www/html/artifacts
phpunit_d11:
name: Drupal 11 PHPUnit Coverage Tests
runs-on: ubuntu-latest
container:
image: pookmish/drupal8ci:php8.3
services:
mysql:
image: mysql:8.0
env:
MYSQL_DATABASE: drupal
MYSQL_USER: drupal
MYSQL_PASSWORD: drupal
MYSQL_ROOT_PASSWORD: drupal
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
steps:
- uses: actions/checkout@v4
with:
path: project
- name: Build project
env:
CC_TEST_REPORTER_ID: ${{secrets.CC_TEST_REPORTER_ID}}
run: |
composer global require su-sws/stanford-caravan:11.x-dev
~/.config/composer/vendor/bin/sws-caravan phpunit /var/www/html --extension-dir=$GITHUB_WORKSPACE/project --with-coverage
- name: Save Test Results
uses: actions/upload-artifact@v4
if: failure()
with:
name: unit-tests-results
path: /var/www/html/artifacts
# acceptance:
# name: Codeception Acceptance Tests
# runs-on: ubuntu-latest
Expand All @@ -42,7 +77,7 @@ jobs:
# DRUPAL_DATABASE_PASSWORD: drupal
# DRUPAL_DATABASE_HOST: mysql
# container:
# image: pookmish/drupal8ci:latest
# image: pookmish/drupal8ci:php8.3
# options: '--network-alias drupal8ci'
# services:
# mysql:
Expand All @@ -64,7 +99,7 @@ jobs:
# composer global require su-sws/stanford-caravan:dev-8.x-2.x
# ~/.composer/vendor/bin/sws-caravan codeception /var/www/html --extension-dir=$GITHUB_WORKSPACE/project --suites=acceptance
# - name: Save Test Results
# uses: actions/upload-artifact@v3
# uses: actions/upload-artifact@v4
# if: always()
# with:
# name: acceptance-tests-results
Expand All @@ -78,7 +113,7 @@ jobs:
# DRUPAL_DATABASE_PASSWORD: drupal
# DRUPAL_DATABASE_HOST: mysql
# container:
# image: pookmish/drupal8ci:latest
# image: pookmish/drupal8ci:php8.3
# options: '--network-alias=drupal8ci'
# services:
# selenium:
Expand Down
6 changes: 4 additions & 2 deletions src/Controller/MediaAdd.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Link;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Url;
Expand Down Expand Up @@ -39,15 +40,16 @@ public static function create(ContainerInterface $container) {
$container->get('renderer'),
$container->get('string_translation'),
$container->get('url_generator'),
$container->get('current_route_match'),
$container->get('plugin.manager.bundle_suggestion_manager')
);
}

/**
* {@inheritdoc}
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $bundle_info, EntityRepositoryInterface $entity_repository, RendererInterface $renderer, TranslationInterface $string_translation, UrlGeneratorInterface $url_generator, BundleSuggestionManagerInterface $bundle_suggestion) {
parent::__construct($entity_type_manager, $bundle_info, $entity_repository, $renderer, $string_translation, $url_generator);
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $bundle_info, EntityRepositoryInterface $entity_repository, RendererInterface $renderer, TranslationInterface $string_translation, UrlGeneratorInterface $url_generator, RouteMatchInterface $route_match, BundleSuggestionManagerInterface $bundle_suggestion) {
parent::__construct($entity_type_manager, $bundle_info, $entity_repository, $renderer, $string_translation, $url_generator, $route_match);
$this->bundleSuggestion = $bundle_suggestion;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function __construct(UrlResolverInterface $url_resolver, ResourceFetcherI
/**
* {@inheritDoc}
*/
public function validate($value, Constraint $constraint) {
public function validate($value, Constraint $constraint): void {
/** @var \Drupal\media\MediaInterface $media */
$media = $value->getEntity();
$source = $media->getSource();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ public function setup(): void {
$this->client = $this->createMock(Client::class);
$this->client
->method('request')
->will($this->returnCallback([$this, 'getOembedCallback']));
->willReturnCallback([$this, 'getOembedCallback']);
$this->client
->method('__call')
->will($this->returnCallback([$this, 'getMagicMethodCallback']));
->willReturnCallback([$this, 'getMagicMethodCallback']);

\Drupal::getContainer()->set('http_client', $this->client);

Expand Down
2 changes: 1 addition & 1 deletion tests/src/Unit/Controller/MediaAddTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function setup(): void {

$bundle_suggestion = $this->createMock(BundleSuggestionManagerInterface::class);
$bundle_suggestion->method('getUploadBundles')
->will($this->returnCallback([$this, 'getUploadBundlesCallback']));
->willReturnCallback([$this, 'getUploadBundlesCallback']);

$url_generator = $this->createMock(UrlGeneratorInterface::class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ public function setup(): void {
$media_type->method('getSource')->willReturn($media_source);

$field_type = $this->createMock(FieldConfigInterface::class);
$field_type->method('getSetting')->will($this->returnCallback([
$field_type->method('getSetting')->willReturnCallback([
$this,
'fieldGetSetting',
]));
$field_type->method('getType')->will($this->returnCallback([
]);
$field_type->method('getType')->willReturnCallback([
$this,
'fieldGetTypeCallback',
]));
]);

$entity_storage = $this->createMock(EntityStorageInterface::class);
$entity_storage->method('loadMultiple')
Expand Down
4 changes: 2 additions & 2 deletions tests/src/Unit/Plugin/BundleSuggestionManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ public function setup(): void {
$media_type->method('getSource')->willReturn($source);

$field_config = $this->createMock(FieldConfigInterface::class);
$field_config->method('getSetting')->will($this->returnCallback([
$field_config->method('getSetting')->willReturnCallback([
$this,
'getFieldSettingCallback',
]));
]);

$entity_storage = $this->createMock(EntityStorageInterface::class);
$entity_storage->method('loadMultiple')->willReturn(['foo' => $media_type]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function setup(): void {

$field_storage = $this->createMock(FieldStorageConfigInterface::class);
$field_storage->method('getSetting')
->will($this->returnCallback([$this, 'fieldStorageGetSettingCallback']));
->willReturnCallback([$this, 'fieldStorageGetSettingCallback']);

$this->fieldDefinition = $this->createMock(FieldDefinitionInterface::class);
$this->fieldDefinition->method('getFieldStorageDefinition')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public function setup(): void {

$entity_storage = $this->createMock(EntityStorageInterface::class);
$entity_storage->method('load')
->will($this->returnCallback([$this, 'loadCallback']));
->willReturnCallback([$this, 'loadCallback']);
$entity_storage->method('loadMultiple')
->will($this->returnCallback([$this, 'loadMultipleCallback']));
->willReturnCallback([$this, 'loadMultipleCallback']);

$entity_type_manager = $this->createMock(EntityTypeManagerInterface::class);
$entity_type_manager->method('getStorage')->willReturn($entity_storage);
Expand All @@ -77,12 +77,23 @@ public function setup(): void {

$field_list = $this->createMock(FieldItemListInterface::class);
$field_list->method('getString')
->will($this->returnCallback([$this, 'fieldGetStringCallback']));
->willReturnCallback([$this, 'fieldGetStringCallback']);

$this->mediaEntity = $this->createMock(MediaInterface::class);
$this->mediaEntity->method('bundle')->willReturnReference($this->mediaBundle);
$this->mediaEntity->method('getSource')->willReturnReference($this->mediaSource);
$this->mediaEntity->method('get')->willReturn($field_list);
}

public function loadCallback($display_id) {

}

public function loadMultipleCallback(){

}

public function fieldGetStringCallback(){
}

}
2 changes: 1 addition & 1 deletion tests/src/Unit/StanfordMediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function setup(): void {
parent::setUp();
$url_assembler = $this->createMock(UnroutedUrlAssemblerInterface::class);
$link_generator = $this->createMock(LinkGeneratorInterface::class);
$link_generator->method('generateFromLink')->willReturn('<a href="http://foobar.com">Foobar</a>');
$link_generator->method('generate')->willReturn('<a href="http://foobar.com">Foobar</a>');
$container = new ContainerBuilder();
$container->set('unrouted_url_assembler', $url_assembler);
$container->set('link_generator', $link_generator);
Expand Down

0 comments on commit c526857

Please sign in to comment.