Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 0 additions & 103 deletions .drone.jsonnet

This file was deleted.

55 changes: 0 additions & 55 deletions .drone.yml

This file was deleted.

162 changes: 5 additions & 157 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,176 +7,24 @@

This package is a collection of tools that make some of the jobs of unit testing easier.

## TestHelper
## Requirements

`Joomla\Test\TestHelper` is a static helper class that can be used to take some of the pain out of repetitive tasks whilst unit testing with PHPUnit.

### Mocking

There are two methods that help with PHPUnit mock objects.

#### `TestHelper::assignMockCallbacks`

This helper method provides an easy way to configure mock callbacks in bulk.

```php
use Joomla\Test\TestHelper;

class FooTest extends \PHPUnit_Framework_TestCase
{
public function testFoo()
{
// Create the mock.
$mockFoo = $this->getMock(
'Foo',
// Methods array.
array(),
// Constructor arguments.
array(),
// Mock class name.
'',
// Call original constructor.
false
);

$mockCallbacks = array(
// 'Method Name' => <callback>
'method1' => array('\mockFoo', 'method1'),
'method2' => array($this, 'mockMethod2'),
);

TestHelper::assignMockCallbacks($mockFoo, $this, $mockCallbacks);
}

public function mockMethod2($value)
{
return strtolower($value);
}
}

```

#### `TestHelper::assignMockReturns`

This helper method provides an easy way to configure mock returns values in bulk.

```php
use Joomla\Test\TestHelper;

class FooTest extends \PHPUnit_Framework_TestCase
{
public function testFoo()
{
// Create the mock.
$mockFoo = $this->getMock(
'Foo',
// Methods array.
array(),
// Constructor arguments.
array(),
// Mock class name.
'',
// Call original constructor.
false
);

$mockReturns = array(
// 'Method Name' => 'Canned return value'
'method1' => 'canned result 1',
'method2' => 'canned result 2',
'method3' => 'canned result 3',
);

TestHelper::assignMockReturns($mockFoo, $this, $mockReturns);
}
}

```

### Reflection

There are three methods that help with reflection.

#### `TestHelper::getValue`

The `TestHelper::getValue` method allows you to get the value of any protected or private property.

```php
use Joomla\Test\TestHelper;

class FooTest extends \PHPUnit_Framework_TestCase
{
public function testFoo()
{
$instance = new \Foo;

// Get the value of a protected `bar` property.
$value = TestHelper::getValue($instance, 'bar');
}
}

```

This method should be used sparingly. It is usually more appropriate to use PHPunit's `assertAttribute*` methods.

#### `TestHelper::setValue`

The `TestHelper::setValue` method allows you to set the value of any protected or private property.

```php
use Joomla\Test\TestHelper;

class FooTest extends \PHPUnit_Framework_TestCase
{
public function testFoo()
{
$instance = new \Foo;

// Set the value of a protected `bar` property.
TestHelper::setValue($instance, 'bar', 'New Value');
}
}

```

This method is useful for injecting values into an object for the purpose of testing getter methods.

#### `TestHelper::invoke`

The `TestHelper::invoke` method allow you to invoke any protected or private method. After specifying the object and the method name, any remaining arguments are passed to the method being invoked.

```php
use Joomla\Test\TestHelper;

class FooTest extends \PHPUnit_Framework_TestCase
{
public function testFoo()
{
$instance = new \Foo;

// Invoke the protected `bar` method.
$value1 = TestHelper::invoke($instance, 'bar');

// Invoke the protected `bar` method with arguments.
$value2 = TestHelper::invoke($instance, 'bar', 'arg1', 'arg2');
}
}
```
- PHP 8.3 or later

## Installation via Composer

Add `"joomla/test": "~2.0"` to the require block in your composer.json and then run `composer install`.
Add `"joomla/test": "~4.0"` to the require block in your composer.json and then run `composer install`.

```json
{
"require": {
"joomla/test": "~2.0"
"joomla/test": "~4.0"
}
}
```

Alternatively, you can simply run the following from the command line:

```sh
composer require joomla/test "~2.0"
composer require joomla/test "~4.0"
```
4 changes: 3 additions & 1 deletion SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
These versions are currently being supported with security updates:

| Version | Supported |
| ------- | ------------------ |
|---------| ------------------ |
| 4.0.x | :white_check_mark: |
| 3.0.x | :white_check_mark: |
| 2.0.x | :white_check_mark: |
| 1.3.x | :white_check_mark: |
| < 1.3 | :x: |
Expand Down
15 changes: 8 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
"homepage": "https://github.com/joomla-framework/test",
"license": "GPL-2.0-or-later",
"require": {
"php": "^8.1.0"
"php": "^8.3.0"
},
"require-dev": {
"joomla/database": "^3.0",
"phpunit/phpunit": "^9.5.28",
"squizlabs/php_codesniffer": "^3.7.2",
"phpstan/phpstan": "1.12.27",
"phpstan/phpstan-deprecation-rules": "1.2.1"
"joomla/database": "dev-4.x-dev",
"phpunit/phpunit": "^12.0",
"squizlabs/php_codesniffer": "^3.10.2",
"phpstan/phpstan": "2.1.17",
"phpstan/phpstan-deprecation-rules": "2.0.3"
},
"conflict": {
"joomla/database": "<2.0"
Expand All @@ -31,7 +31,8 @@
"extra": {
"branch-alias": {
"dev-master": "2.0-dev",
"dev-3.x-dev": "3.0-dev"
"dev-3.x-dev": "3.0-dev",
"dev-4.x-dev": "4.0-dev"
}
}
}
Loading