Skip to content

Commit eba6094

Browse files
authored
Merge pull request #9 from digitalutsc/refactor/dependency-injection
refactor: implement dependency injection
2 parents 2f4ade3 + d8d98f3 commit eba6094

6 files changed

Lines changed: 128 additions & 7 deletions

File tree

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ on:
99

1010
jobs:
1111
islandora-module-ci:
12-
uses: digitalutsc/reusable_workflows/.github/workflows/islandora-module-ci.yml@main
12+
uses: digitalutsc/reusable_workflows/.github/workflows/islandora-module-ci-d10only.yml@main
1313
with:
1414
module_name: assign_calc
15-
install_chromedriver: false
15+
install_chromedriver: true

assign_calc.info.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,11 @@ dependencies:
88
- paragraphs
99
- entity_reference_revisions
1010
- contact
11+
- node
12+
- menu_ui
13+
- views
14+
- ds
15+
- text
16+
- datetime
17+
- field
18+
- path

config/install/field.field.paragraph.step.field_step_name.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,3 @@ default_value: { }
1919
default_value_callback: ''
2020
settings: { }
2121
field_type: string
22-
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
langcode: en
2+
status: true
3+
dependencies:
4+
module:
5+
- node
6+
- text
7+
id: node.body
8+
field_name: body
9+
entity_type: node
10+
type: text_with_summary
11+
settings: { }
12+
module: text
13+
locked: false
14+
cardinality: 1
15+
translatable: true
16+
indexes: { }
17+
persist_with_no_fields: true
18+
custom_storage: false

src/Form/AssignmentDatesForm.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,40 @@
66
use Drupal\Core\Form\FormBase;
77
use Drupal\Core\Form\FormStateInterface;
88
use Drupal\Core\Url;
9+
use Symfony\Component\DependencyInjection\ContainerInterface;
10+
use Drupal\Core\Database\Connection;
911

1012
/**
1113
* Implements an assignment form.
1214
*/
1315
class AssignmentDatesForm extends FormBase {
1416

17+
/**
18+
* The database connection.
19+
*
20+
* @var \Drupal\Core\Database\Connection
21+
*/
22+
protected $database;
23+
24+
/**
25+
* Constructs a assignment form object.
26+
*
27+
* @param \Drupal\Core\Database\Connection $database
28+
* The database connection.
29+
*/
30+
public function __construct(Connection $database) {
31+
$this->database = $database;
32+
}
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
public static function create(ContainerInterface $container) {
38+
return new static(
39+
$container->get('database')
40+
);
41+
}
42+
1543
/**
1644
* {@inheritdoc}
1745
*/
@@ -36,13 +64,11 @@ public function buildForm(array $form, FormStateInterface $form_state) {
3664
];
3765

3866
// For php >8.1.
39-
// phpcs:ignore -- \Drupal calls should be avoided in classes, use dependency injection instead
40-
if (!isset(\Drupal::database()->getConnectionOptions()['prefix'])) {
67+
if (!isset($this->database->getConnectionOptions()['prefix'])) {
4168
return $form;
4269
}
4370

44-
// phpcs:ignore -- \Drupal calls should be avoided in classes, use dependency injection instead
45-
$prf = \Drupal::database()->getConnectionOptions()['prefix'];
71+
$prf = $this->database->getConnectionOptions()['prefix'];
4672
$node = $prf . 'node';
4773
$node_field_data = $prf . 'node_field_data';
4874
$conn = Database::getConnection();

tests/src/Functional/LoadTest.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Drupal\Tests\assign_calc\Functional;
4+
5+
use Drupal\Core\Url;
6+
use Drupal\Tests\BrowserTestBase;
7+
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
8+
9+
/**
10+
* Simple test to ensure that main page loads with module enabled.
11+
*
12+
* @group assign_calc
13+
*/
14+
#[RunTestsInSeparateProcesses]
15+
class LoadTest extends BrowserTestBase {
16+
17+
/**
18+
* Modules to enable.
19+
*
20+
* @var array
21+
*/
22+
protected static $modules = [
23+
'assign_calc',
24+
'paragraphs',
25+
'entity_reference_revisions',
26+
'node',
27+
'menu_ui',
28+
'views',
29+
'ds',
30+
'contact',
31+
'text',
32+
'datetime',
33+
'field',
34+
'path',
35+
];
36+
37+
/**
38+
* The theme to install as the default for testing.
39+
*
40+
* Defaults to the install profile's default theme, if it specifies any.
41+
*
42+
* @var string
43+
*/
44+
protected $defaultTheme = 'stark';
45+
46+
/**
47+
* A user with permission to administer site configuration.
48+
*
49+
* @var \Drupal\user\UserInterface
50+
*/
51+
protected $user;
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
protected function setUp(): void {
57+
parent::setUp();
58+
$this->user = $this->drupalCreateUser(['administer site configuration']);
59+
$this->drupalLogin($this->user);
60+
}
61+
62+
/**
63+
* Tests that the home page loads with a 200 response.
64+
*/
65+
public function testLoad() {
66+
$this->drupalGet(Url::fromRoute('<front>'));
67+
$this->assertSession()->statusCodeEquals(200);
68+
}
69+
70+
}

0 commit comments

Comments
 (0)