How do I use PHPUnit to test __construct with arguments?

I'm new to PHPUnit, and unit testing in general. I can't seem to find a clear tutorial or resource on how best to test:

  1. Passing no argument fails.
  2. How to pass an argument for a constructor test.
  3. Passing an empty argument results in the expected exception.

How would I approach testing this constructor?

<?php

class SiteManagement {
    public function __construct (array $config) {
        // Make sure we actually passed a config
        if (empty($config)) {
            throw new \Exception('Configuration not valid', 100);
        }

        // Sanity check the site list
        if (empty($config['siteList'])) {
            throw new \Exception('Site list not set', 101);
        }
    }
}


Read more here: https://stackoverflow.com/questions/66994196/how-do-i-use-phpunit-to-test-construct-with-arguments

Content Attribution

This content was originally published by Ben Dauphinee at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: