0

I was trying to write PHPUnit test in D8, but am blocked due to a weird issue which I am not able to track.

The module for which I am writing the test is at https://github.com/ajitdev/phpunit_d8

I have created a basic class Math with some functions https://github.com/ajitdev/phpunit_d8/blob/master/src/Math.php

<?php
namespace Drupal\phpunit_d8;
class Math {
  public function addNum($a, $b, $c) {
    return $a + $b + $c;
  }
  public function substract($a, $b) {
    return $a - $b;
  }
  public function divide($a, $b) {
    return $a / $b;
  }
}

And the test in tests/src/Unit directory https://github.com/ajitdev/phpunit_d8/blob/master/tests/src/Unit/MathTest.php

<?php
/**
 * @file
 * Contains Drupal\Tests\phpunit_d8\Unit\MathTest
 */
namespace Drupal\Tests\phpunit_d8\Unit;
use Drupal\Tests\UnitTestCase;
use Drupal\phpunit_d8\Math;
/**
 * Class MathTest
 * @package Drupal\Tests\phpunit_d8\Unit
 *
 * @group math
 */
class MathTest extends UnitTestCase {
  public function addNumTest() {
    $math = new Math();
    $this->assertEquals($math->addNum(1,2,3), 6);
  }
}

The test shows up on /admin/config/development/testing, but throws the following exception:

Uncaught PHP Exception Drupal\Core\Database\IntegrityConstraintViolationException: "SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'file' cannot be null: INSERT INTO {simpletest} (test_id, test_class, status, message, message_group, function, line, file) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7); Array
(
    [:db_insert_placeholder_0] => 14
    [:db_insert_placeholder_1] => Drupal\Tests\phpunit_d8\Unit\MathTest
    [:db_insert_placeholder_2] => fail
    [:db_insert_placeholder_3] => 
    [:db_insert_placeholder_4] => Other
    [:db_insert_placeholder_5] => Drupal\Tests\phpunit_d8\Unit\MathTest->Warning()
    [:db_insert_placeholder_6] => 0
    [:db_insert_placeholder_7] => 
)
" at /Applications/MAMP/htdocs/d8/core/lib/Drupal/Core/Database/Connection.php line 673

The issue is empty value for the file value. I am not able to trace down the issue. Any pointers would be really helpful.

1 Answer 1

1

I was able to resolve the issue. You just have to prefix your test with test_. Seriously! No kidding!

My test looks like the following:

<?php
/**
 * @file
 * Contains Drupal\Tests\phpunit_d8\Unit\MathTest
 */

namespace Drupal\Tests\phpunit_d8\Unit;

use Drupal\Tests\UnitTestCase;
use Drupal\phpunit_d8\Math;

/**
 * Class MathTest
 * @package Drupal\Tests\phpunit_d8\Unit
 *
 * @group math
 */
class MathTest extends UnitTestCase {
  /**
   * Very simple test to MathTest::addNum().
   */
  public function testAddNum() {
    $math = new Math();
    $this->assertEquals($math->addNum(1,2,3), 6);
  }
}

Also, pushed at https://github.com/ajitdev/phpunit_d8

1
  • Yes, test methods should prefix with test.
    – mradcliffe
    Apr 1, 2016 at 12:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.