Skip to content

Commit 5b97c82

Browse files
authored
Merge pull request #216 from DigitalTimK/env_files
Implement usage of DOTENV-files
2 parents 736635c + d11107d commit 5b97c82

File tree

7 files changed

+71
-17
lines changed

7 files changed

+71
-17
lines changed

.env

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Specify the value of your BigBlueButton secret
2+
BBB_SERVER_BASE_URL="https://test-install.blindsidenetworks.com/bigbluebutton/"
3+
4+
# Specify the Server Base URL of your BigBlueButton
5+
BBB_SECRET="8cd8ef52e8e101574e400365b55e11a6"

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ composer.phar
33
composer.lock
44
.DS_Store
55

6+
## local environment variables
7+
.env.local
8+
69
## Directory-based project format:
710
.idea/
811
/nbproject

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,31 @@ $ ./vendor/bin/phpstan analyse
5656
For every implemented feature add unit tests and check all is green by running the command below.
5757

5858
```bash
59+
# using an alias
5960
$ composer test
61+
62+
# or the same w/o alias
63+
./vendor/bin/phpunit
6064
```
6165

6266
To run a single test
6367

6468
```bash
65-
./vendor/bin/phpunit --filter "BigBlueButtonTest::testApiVersion"
69+
# using an alias
70+
$ composer test -- --filter BigBlueButtonTest::testApiVersion
71+
72+
# or the same w/o alias
73+
./vendor/bin/phpunit --filter BigBlueButtonTest::testApiVersion
6674
```
6775

76+
**Remark:**
77+
78+
Some test will connect to an existing BBB-server, which is specified in the `.env`-file. You
79+
can specify your own BBB-server by copy that file into the same folder and name it `.env.local`.
80+
Exchange the credentials `BBB_SERVER_BASE_URL` and `BBB_SECRET` to your server's values.
81+
Since this new file (`.env.local`) takes precedence over the main file (`.env`), you will now test
82+
with your own server.
83+
6884
[bbb]: http://bigbluebutton.org
6985
[composer]: https://getcomposer.org
7086
[INSTALL]: samples/README.md

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
"phpmetrics/phpmetrics": "^v2.8.2",
4242
"wapmorgan/php-deprecation-detector": "^2.0.33",
4343
"phpstan/phpstan": "^1.10",
44-
"tracy/tracy": "^2.10"
44+
"tracy/tracy": "^2.10",
45+
"vlucas/phpdotenv": "^5.6"
4546
},
4647
"scripts": {
4748
"code-check": "./vendor/bin/phpstan analyse",

phpunit.xml.dist

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
</coverage>
88
<php>
99
<env name="XDEBUG_MODE" value="coverage"/>
10-
<!-- Specify the value of your BigBlueButton secret -->
11-
<env name="BBB_SECRET" value="8cd8ef52e8e101574e400365b55e11a6"/>
12-
<!-- Specify the Server Base URL of your BigBlueButton -->
13-
<env name="BBB_SERVER_BASE_URL" value="https://test-install.blindsidenetworks.com/bigbluebutton/"/>
1410
</php>
1511
<testsuites>
1612
<testsuite name="BigBlueButton test suite">

src/BigBlueButton.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@
6060
*/
6161
class BigBlueButton
6262
{
63-
protected string $securitySecret;
64-
protected string $bbbServerBaseUrl;
63+
protected string $bbbSecret;
64+
protected string $bbbBaseUrl;
6565
protected UrlBuilder $urlBuilder;
6666
protected string $jSessionId;
6767

@@ -78,12 +78,27 @@ class BigBlueButton
7878
*/
7979
public function __construct(?string $baseUrl = null, ?string $secret = null, ?array $opts = [])
8080
{
81+
// Provide an early error message if configuration is wrong
82+
if (is_null($secret) && false === getenv('BBB_SERVER_BASE_URL')) {
83+
throw new \RuntimeException('No BBB-Server-Url found! Please provide it either in constructor ' .
84+
"(1st argument) or by environment variable 'BBB_SERVER_BASE_URL'!");
85+
}
86+
87+
if (is_null($secret) && false === getenv('BBB_SECRET') && false === getenv('BBB_SECURITY_SALT')) {
88+
throw new \RuntimeException('No BBB-Secret (or BBB-Salt) found! Please provide it either in constructor ' .
89+
"(2nd argument) or by environment variable 'BBB_SECRET' (or 'BBB_SECURITY_SALT')!");
90+
}
91+
8192
// Keeping backward compatibility with older deployed versions
8293
// BBB_SECRET is the new variable name and have higher priority against the old named BBB_SECURITY_SALT
83-
$this->securitySecret = $secret ?: getenv('BBB_SECRET') ?: getenv('BBB_SECURITY_SALT') ?: '';
84-
$this->bbbServerBaseUrl = $baseUrl ?: getenv('BBB_SERVER_BASE_URL') ?: '';
94+
// Reminder: getenv() will return FALSE if not set. But bool is not accepted by $this->bbbSecret
95+
// nor $this->bbbBaseUrl (only strings), thus FALSE will be converted automatically to an empty
96+
// string (''). Having a bool should be not possible due to the checks above and the automated
97+
// conversion, but PHPStan is still unhappy, so it's covered explicit by adding `?: ''`.
98+
$this->bbbBaseUrl = $baseUrl ?: getenv('BBB_SERVER_BASE_URL') ?: '';
99+
$this->bbbSecret = $secret ?: getenv('BBB_SECRET') ?: getenv('BBB_SECURITY_SALT') ?: '';
85100
$this->hashingAlgorithm = HashingAlgorithm::SHA_256;
86-
$this->urlBuilder = new UrlBuilder($this->securitySecret, $this->bbbServerBaseUrl, $this->hashingAlgorithm);
101+
$this->urlBuilder = new UrlBuilder($this->bbbSecret, $this->bbbBaseUrl, $this->hashingAlgorithm);
87102
$this->curlOpts = $opts['curl'] ?? [];
88103
}
89104

tests/BigBlueButtonTest.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use BigBlueButton\Parameters\IsMeetingRunningParameters;
3030
use BigBlueButton\Parameters\PublishRecordingsParameters;
3131
use BigBlueButton\Util\ParamsIterator;
32+
use Dotenv\Dotenv;
3233

3334
/**
3435
* Class BigBlueButtonTest.
@@ -48,12 +49,7 @@ public function setUp(): void
4849
{
4950
parent::setUp();
5051

51-
foreach (['BBB_SECRET', 'BBB_SERVER_BASE_URL'] as $key) {
52-
if (!getenv($key)) {
53-
$this->fail('$_SERVER[\'' . $key . '\'] not set in '
54-
. 'phpunit.xml');
55-
}
56-
}
52+
$this->loadEnvironmentVariables();
5753

5854
$this->bbb = new BigBlueButton();
5955
}
@@ -359,4 +355,26 @@ public function testUpdateRecordings(): void
359355
$this->assertEquals('FAILED', $result->getReturnCode());
360356
$this->assertTrue($result->failed());
361357
}
358+
359+
/**
360+
* @see https://github.com/vlucas/phpdotenv
361+
*/
362+
private function loadEnvironmentVariables(): void
363+
{
364+
$envPath = __DIR__ . '/..';
365+
$envFileMain = '.env';
366+
$envFileLocal = '.env.local';
367+
368+
if (file_exists("{$envPath}/{$envFileLocal}")) {
369+
$envFile = $envFileLocal;
370+
} elseif (file_exists("{$envPath}/{$envFileMain}")) {
371+
$envFile = $envFileMain;
372+
} else {
373+
throw new \RuntimeException("Environment file ('{$envFileMain}' nor '{$envFileLocal}') not found!");
374+
}
375+
376+
$dotenv = Dotenv::createUnsafeImmutable($envPath, $envFile);
377+
$dotenv->load();
378+
$dotenv->required(['BBB_SECRET', 'BBB_SERVER_BASE_URL']);
379+
}
362380
}

0 commit comments

Comments
 (0)