<blockquote>
  <p>I read that you cannot use bootstrap.php in the craft/app folder because you're essentially trying to bootstrap a webapp (and this server isn't a webserver).</p>
</blockquote>

<p>Not sure where you read that, but that doesn't sound right.</p>

<p>I'm assuming you've got a multi-environment config setup?  If so, then manually set the <a href="https://craftcms.com/docs/php-constants#craft-environment" rel="nofollow"><code>CRAFT_ENVIRONMENT</code></a> PHP constant before you load the Craft <code>bootstrap.php</code> file to the environment you want Craft to load in the config files.</p>

<p>That will get rid of the <code>Undefined index: SERVER_NAME</code> error you're getting as well as should fixed your database connection issue.</p>

CC BY-SA 3.0
2016-05-17T13:29:00.963Z
So what about at the top of my phpunit bootstrap file before I call craft's bootstrap file? 

Just literally define a PHP constant? Like `define("CRAFT_ENVIRONMENT", "dev")` ??
Exziled
CC BY-SA 3.0
2016-05-17T17:08:41.970Z
Sounds about right!
Brad Bell
CC BY-SA 3.0
2016-05-17T17:29:24.320Z
That did resolve the error, now onto the next error in a long list of them to get phpunit working on jenkins -.- thanks!
Exziled
CC BY-SA 3.0
2016-05-16T22:07:58.103Z
2016-05-17T17:08:26.360Z
2016-05-17T17:08:26.360Z
Brad Bell
<p>Maybe an edge case, but if you're using the Kint logging/dumping extension for Craft in your console command, be careful. I got the undefined index SERVER_NAME error trying to to save an entry and output success/fail:</p>

<pre><code>d(craft()-&gt;entries-&gt;saveEntry($entryModel)); --&gt; SERVER_NAME index error
</code></pre>

<p>This fixed it:</p>

<pre><code>$save_result = craft()-&gt;entries-&gt;SaveEntry($entryModel);
d($save_result);
</code></pre>

<p>Hope that saved someone a few minutes.</p>

CC BY-SA 3.0
2018-03-15T22:13:23.017Z
2018-03-15T22:13:23.017Z
borducks
<p>I'm trying to run some commands on a Jenkins server to have PHPUnit execute the tests and report the results. </p>

<p>When I do <code>require_once 'craft/app/bootstrap.php';</code> It is giving an error saying: <code>PHP Notice:  Undefined index: SERVER_NAME in craft/app/bootstrap.php on line 105</code></p>

<p>I read that you cannot use bootstrap.php in the craft/app folder because you're essentially trying to bootstrap a webapp (and this server isn't a webserver). It was suggested that I use <code>craft/etc/console/yiic.php</code> as my bootstrap file for PHPUnit. However, this is throwing errors about CDbConnection and is unable to connect.</p>

<p>Is there any way to bootstrap Craft from a command line to run PHPUnit? I'm unable to get it working without an apache server.</p>

<php><bootstrap-craft>
Bootstrapping Craft for PHPUnit on command line
# Bootstrapping Craft for PHPUnit on command line

(Asked by: username_0 on 2016-05-16)

I'm trying to run some commands on a Jenkins server to have PHPUnit execute the tests and report the results.

When I do `require_once 'craft/app/bootstrap.php';` It is giving an error saying: `PHP Notice: Undefined index: SERVER_NAME in craft/app/bootstrap.php on line 105`

I read that you cannot use bootstrap.php in the craft/app folder because you're essentially trying to bootstrap a webapp (and this server isn't a webserver). It was suggested that I use `craft/etc/console/yiic.php` as my bootstrap file for PHPUnit. However, this is throwing errors about CDbConnection and is unable to connect.

Is there any way to bootstrap Craft from a command line to run PHPUnit? I'm unable to get it working without an apache server.

**Tags:** php, bootstrap-craft

---

## 2 answers

### Answer by username_1 on 2016-05-16. Score: 2

> I read that you cannot use bootstrap.php in the craft/app folder because you're essentially trying to bootstrap a webapp (and this server isn't a webserver).

Not sure where you read that, but that doesn't sound right.

I'm assuming you've got a multi-environment config setup? If so, then manually set the [`CRAFT_ENVIRONMENT`](https://craftcms.com/docs/php-constants#craft-environment) PHP constant before you load the Craft `bootstrap.php` file to the environment you want Craft to load in the config files.

That will get rid of the `Undefined index: SERVER_NAME` error you're getting as well as should fixed your database connection issue.

> **Comments:**
> **username_0**: So what about at the top of my phpunit bootstrap file before I call craft's bootstrap file?   Just literally define a PHP constant? Like `define("CRAFT_ENVIRONMENT", "dev")` ??  
> **username_1**: Sounds about right!  
> **username_0**: That did resolve the error, now onto the next error in a long list of them to get phpunit working on jenkins -.- thanks!  

---

### Answer by username_2 on 2018-03-15. Score: 1

Maybe an edge case, but if you're using the Kint logging/dumping extension for Craft in your console command, be careful. I got the undefined index SERVER_NAME error trying to to save an entry and output success/fail:
d(craft()->entries->saveEntry($entryModel)); --> SERVER_NAME index error

This fixed it:
$save_result = craft()->entries->SaveEntry($entryModel);
d($save_result);

Hope that saved someone a few minutes.


---

