Fr3nch13/CakePHP Utilities

PackagesCommandTest extends TestCase
in package
uses ConsoleIntegrationTestTrait

Cake TestCase class

Table of Contents

Properties

$autoFixtures  : bool
By default, all fixtures attached to this class will be truncated and reloaded after each test.
$dropTables  : bool
Control table create/drops on each test method.
$fixtureManager  : FixtureManager|null
The class responsible for managing the creation, loading and removing of fixtures
$_configure  : array<string|int, mixed>
Configure values to restore at end of test.
$_err  : StubConsoleOutput
Console error output stub
$_exitCode  : int|null
Last exit code
$_in  : StubConsoleInput
Console input mock
$_out  : StubConsoleOutput
Console output stub
$_tableLocator  : LocatorInterface|null
Table locator instance
$_useCommandRunner  : bool
Whether to use the CommandRunner
$command  : PackagesCommand
$defaultTable  : string|null
This object's default table alias.
$fixtures  : array<string|int, string>
Fixtures used by this test case.
$fixtureStrategy  : FixtureStrategyInterface|null

Methods

assertDirectoryDoesNotExist()  : void
Asserts that a directory does not exist.
assertDoesNotMatchRegularExpression()  : void
Asserts that a string does not match a given regular expression.
assertEqualsSql()  : void
Assert that a string matches SQL with db-specific characters like quotes removed.
assertErrorContains()  : void
Asserts `stderr` contains expected output
assertErrorEmpty()  : void
Asserts that `stderr` is empty
assertErrorRegExp()  : void
Asserts `stderr` contains expected regexp
assertEventFired()  : void
Asserts that a global event was fired. You must track events in your event manager for this assertion to work
assertEventFiredWith()  : void
Asserts an event was fired with data
assertExitCode()  : void
Asserts shell exited with the expected code
assertExitError()  : void
Asserts shell exited with Command::CODE_ERROR
assertExitSuccess()  : void
Asserts shell exited with the Command::CODE_SUCCESS
assertFileDoesNotExist()  : void
Asserts that a file does not exist.
assertHtml()  : bool
Asserts HTML tags.
assertMatchesRegularExpression()  : void
Asserts that a string matches a given regular expression.
assertOutputContains()  : void
Asserts `stdout` contains expected output
assertOutputEmpty()  : void
Asserts that `stdout` is empty
assertOutputNotContains()  : void
Asserts `stdout` does not contain expected output
assertOutputRegExp()  : void
Asserts `stdout` contains expected regexp
assertRegExpSql()  : void
Assertion for comparing a regex pattern against a query having its identifiers quoted. It accepts queries quoted with the characters `<` and `>`. If the third parameter is set to true, it will alter the pattern to both accept quoted and unquoted queries
assertTextContains()  : void
Assert that a string contains another string, ignoring differences in newlines.
assertTextEndsNotWith()  : void
Asserts that a string ends not with a given prefix, ignoring differences in newlines.
assertTextEndsWith()  : void
Asserts that a string ends with a given prefix, ignoring differences in newlines.
assertTextEquals()  : void
Assert text equality, ignoring differences in newlines.
assertTextNotContains()  : void
Assert that a text doesn't contain another text, ignoring differences in newlines.
assertTextNotEquals()  : void
Assert text equality, ignoring differences in newlines.
assertTextStartsNotWith()  : void
Asserts that a string starts not with a given prefix, ignoring differences in newlines.
assertTextStartsWith()  : void
Asserts that a string starts with a given prefix, ignoring differences in newlines.
cleanupConsoleTrait()  : void
Cleans state to get ready for the next test
clearPlugins()  : void
Clear all plugins from the global plugin collection.
deprecated()  : void
Helper method for check deprecation methods
exec()  : void
Runs CLI integration test
fetchTable()  : Table
Convenience method to get a table instance.
getFixtures()  : array<string|int, string>
Get the fixtures this test should use.
getMockForModel()  : Table|MockObject
Mock a model, maintain fixtures and table association
getTableLocator()  : LocatorInterface
Gets the table locator.
loadFixtures()  : void
Chooses which fixtures to load for a given test
loadPlugins()  : BaseApplication
Load plugins into a simulated application.
loadRoutes()  : void
Load routes for the application.
removePlugins()  : void
Remove plugins from the global plugin collection.
setAppNamespace()  : string|null
Set the app namespace
setTableLocator()  : $this
Sets the table locator.
setUp()  : void
setUp method
skipIf()  : bool
Overrides SimpleTestCase::skipIf to provide a boolean return value
testBuildOptionParser()  : void
Test \Fr3nch13\Utilities\Command\PackagesCommand::buildOptionParser()
testGetDescription()  : void
Test \Fr3nch13\Utilities\Command\PackagesCommand::getDescription()
testHelpOption()  : void
Test the help option.
testNoOptions()  : void
Test no options.
testOptionBadPath()  : void
Test when the path option is bad.
testOptionCommandAll()  : void
Test the command option with all selected.
testOptionCommandApp()  : void
Test the command option with app selected.
testOptionCommandCompare()  : void
Test the command option with compare selected.
testOptionCommandPackages()  : void
Test the command option with packages selected.
testOptionPath()  : void
Test the path options.
testVerboseOption()  : void
Test the verbose option.
useCommandRunner()  : void
Set this test case to use the CommandRunner rather than the legacy ShellDispatcher
withErrorReporting()  : void
Helper method for tests that needs to use error_reporting()
_assertAttributes()  : string|false
Check the attributes as part of an assertTags() check.
_getTableClassName()  : string
Gets the class name for the table.
_normalizePath()  : string
Normalize a path for comparison.
addFixture()  : $this
Adds a fixture to this test case.
assertNotWithinRange()  : void
Compatibility function to test if a value is not between an acceptable range.
assertOutputContainsRow()  : void
Check that a row of cells exists in the output.
assertPathEquals()  : void
Compatibility function to test paths.
assertWithinRange()  : void
Compatibility function to test if a value is between an acceptable range.
commandStringToArgs()  : array<string|int, string>
Creates an $argv array from a command string
getFixtureStrategy()  : FixtureStrategyInterface
Returns fixture strategy used by these tests.
makeRunner()  : CommandRunner|LegacyCommandRunner
Builds the appropriate command dispatcher
setupFixtures()  : void
Initialized and loads any use fixtures.
skipUnless()  : bool
Compatibility function for skipping.
tearDown()  : void
teardown any static object changes and restore them.
teardownFixtures()  : void
Unloads any use fixtures.

Properties

$autoFixtures

By default, all fixtures attached to this class will be truncated and reloaded after each test.

autoFixtures is only used by deprecated fixture features. This property will be removed in 5.0

public bool $autoFixtures = true

Set this to false to handle manually

$dropTables

Control table create/drops on each test method.

dropTables is only used by deprecated fixture features. This property will be removed in 5.0

public bool $dropTables = false

If true, tables will still be dropped at the end of each test runner execution.

$fixtureManager

The class responsible for managing the creation, loading and removing of fixtures

public static FixtureManager|null $fixtureManager

$_configure

Configure values to restore at end of test.

protected array<string|int, mixed> $_configure = []

$defaultTable

This object's default table alias.

protected string|null $defaultTable = null

$fixtures

Fixtures used by this test case.

protected array<string|int, string> $fixtures = []

Methods

assertDirectoryDoesNotExist()

Asserts that a directory does not exist.

public static assertDirectoryDoesNotExist(string $directory[, string $message = '' ]) : void
Parameters
$directory : string

Directory

$message : string = ''

Message

Tags
throws
InvalidArgumentException
codeCoverageIgnore

assertDoesNotMatchRegularExpression()

Asserts that a string does not match a given regular expression.

public static assertDoesNotMatchRegularExpression(string $pattern, string $string[, string $message = '' ]) : void
Parameters
$pattern : string

Regex pattern

$string : string

String to test

$message : string = ''

Message

Tags
throws
InvalidArgumentException

assertEqualsSql()

Assert that a string matches SQL with db-specific characters like quotes removed.

public assertEqualsSql(string $expected, string $actual[, string $message = '' ]) : void
Parameters
$expected : string

The expected sql

$actual : string

The sql to compare

$message : string = ''

The message to display on failure

assertErrorContains()

Asserts `stderr` contains expected output

public assertErrorContains(string $expected[, string $message = '' ]) : void
Parameters
$expected : string

Expected output

$message : string = ''

Failure message

assertErrorEmpty()

Asserts that `stderr` is empty

public assertErrorEmpty([string $message = '' ]) : void
Parameters
$message : string = ''

The message to output when the assertion fails.

assertErrorRegExp()

Asserts `stderr` contains expected regexp

public assertErrorRegExp(string $pattern[, string $message = '' ]) : void
Parameters
$pattern : string

Expected pattern

$message : string = ''

Failure message

assertEventFired()

Asserts that a global event was fired. You must track events in your event manager for this assertion to work

public assertEventFired(string $name[, EventManager|null $eventManager = null ][, string $message = '' ]) : void
Parameters
$name : string

Event name

$eventManager : EventManager|null = null

Event manager to check, defaults to global event manager

$message : string = ''

Assertion failure message

assertEventFiredWith()

Asserts an event was fired with data

public assertEventFiredWith(string $name, string $dataKey, mixed $dataValue[, EventManager|null $eventManager = null ][, string $message = '' ]) : void

If a third argument is passed, that value is used to compare with the value in $dataKey

Parameters
$name : string

Event name

$dataKey : string

Data key

$dataValue : mixed

Data value

$eventManager : EventManager|null = null

Event manager to check, defaults to global event manager

$message : string = ''

Assertion failure message

assertExitCode()

Asserts shell exited with the expected code

public assertExitCode(int $expected[, string $message = '' ]) : void
Parameters
$expected : int

Expected exit code

$message : string = ''

Failure message

assertExitError()

Asserts shell exited with Command::CODE_ERROR

public assertExitError([string $message = '' ]) : void
Parameters
$message : string = ''

Failure message

assertExitSuccess()

Asserts shell exited with the Command::CODE_SUCCESS

public assertExitSuccess([string $message = '' ]) : void
Parameters
$message : string = ''

Failure message

assertFileDoesNotExist()

Asserts that a file does not exist.

public static assertFileDoesNotExist(string $filename[, string $message = '' ]) : void
Parameters
$filename : string

Filename

$message : string = ''

Message

Tags
throws
InvalidArgumentException
codeCoverageIgnore

assertHtml()

Asserts HTML tags.

public assertHtml(array<string|int, mixed> $expected, string $string[, bool $fullDebug = false ]) : bool

Takes an array $expected and generates a regex from it to match the provided $string. Samples for $expected:

Checks for an input tag with a name attribute (contains any non-empty value) and an id attribute that contains 'my-input':

['input' => ['name', 'id' => 'my-input']]

Checks for two p elements with some text in them:

[
  ['p' => true],
  'textA',
  '/p',
  ['p' => true],
  'textB',
  '/p'
]

You can also specify a pattern expression as part of the attribute values, or the tag being defined, if you prepend the value with preg: and enclose it with slashes, like so:

[
  ['input' => ['name', 'id' => 'preg:/FieldName\d+/']],
  'preg:/My\s+field/'
]

Important: This function is very forgiving about whitespace and also accepts any permutation of attribute order. It will also allow whitespace between specified tags.

Parameters
$expected : array<string|int, mixed>

An array, see above

$string : string

An HTML/XHTML/XML string

$fullDebug : bool = false

Whether more verbose output should be used.

Return values
bool

assertMatchesRegularExpression()

Asserts that a string matches a given regular expression.

public static assertMatchesRegularExpression(string $pattern, string $string[, string $message = '' ]) : void
Parameters
$pattern : string

Regex pattern

$string : string

String to test

$message : string = ''

Message

Tags
throws
InvalidArgumentException
codeCoverageIgnore

assertOutputContains()

Asserts `stdout` contains expected output

public assertOutputContains(string $expected[, string $message = '' ]) : void
Parameters
$expected : string

Expected output

$message : string = ''

Failure message

assertOutputEmpty()

Asserts that `stdout` is empty

public assertOutputEmpty([string $message = '' ]) : void
Parameters
$message : string = ''

The message to output when the assertion fails.

assertOutputNotContains()

Asserts `stdout` does not contain expected output

public assertOutputNotContains(string $expected[, string $message = '' ]) : void
Parameters
$expected : string

Expected output

$message : string = ''

Failure message

assertOutputRegExp()

Asserts `stdout` contains expected regexp

public assertOutputRegExp(string $pattern[, string $message = '' ]) : void
Parameters
$pattern : string

Expected pattern

$message : string = ''

Failure message

assertRegExpSql()

Assertion for comparing a regex pattern against a query having its identifiers quoted. It accepts queries quoted with the characters `<` and `>`. If the third parameter is set to true, it will alter the pattern to both accept quoted and unquoted queries

public assertRegExpSql(string $pattern, string $actual[, bool $optional = false ]) : void
Parameters
$pattern : string

The expected sql pattern

$actual : string

The sql to compare

$optional : bool = false

Whether quote characters (marked with <>) are optional

assertTextContains()

Assert that a string contains another string, ignoring differences in newlines.

public assertTextContains(string $needle, string $haystack[, string $message = '' ][, bool $ignoreCase = false ]) : void

Helpful for doing cross platform tests of blocks of text.

Parameters
$needle : string

The string to search for.

$haystack : string

The string to search through.

$message : string = ''

The message to display on failure.

$ignoreCase : bool = false

Whether the search should be case-sensitive.

assertTextEndsNotWith()

Asserts that a string ends not with a given prefix, ignoring differences in newlines.

public assertTextEndsNotWith(string $suffix, string $string[, string $message = '' ]) : void

Helpful for doing cross platform tests of blocks of text.

Parameters
$suffix : string

The suffix to not find.

$string : string

The string to search.

$message : string = ''

The message to use for failure.

assertTextEndsWith()

Asserts that a string ends with a given prefix, ignoring differences in newlines.

public assertTextEndsWith(string $suffix, string $string[, string $message = '' ]) : void

Helpful for doing cross platform tests of blocks of text.

Parameters
$suffix : string

The suffix to find.

$string : string

The string to search.

$message : string = ''

The message to use for failure.

assertTextEquals()

Assert text equality, ignoring differences in newlines.

public assertTextEquals(string $expected, string $result[, string $message = '' ]) : void

Helpful for doing cross platform tests of blocks of text.

Parameters
$expected : string

The expected value.

$result : string

The actual value.

$message : string = ''

The message to use for failure.

assertTextNotContains()

Assert that a text doesn't contain another text, ignoring differences in newlines.

public assertTextNotContains(string $needle, string $haystack[, string $message = '' ][, bool $ignoreCase = false ]) : void

Helpful for doing cross platform tests of blocks of text.

Parameters
$needle : string

The string to search for.

$haystack : string

The string to search through.

$message : string = ''

The message to display on failure.

$ignoreCase : bool = false

Whether the search should be case-sensitive.

assertTextNotEquals()

Assert text equality, ignoring differences in newlines.

public assertTextNotEquals(string $expected, string $result[, string $message = '' ]) : void

Helpful for doing cross platform tests of blocks of text.

Parameters
$expected : string

The expected value.

$result : string

The actual value.

$message : string = ''

The message to use for failure.

assertTextStartsNotWith()

Asserts that a string starts not with a given prefix, ignoring differences in newlines.

public assertTextStartsNotWith(string $prefix, string $string[, string $message = '' ]) : void

Helpful for doing cross platform tests of blocks of text.

Parameters
$prefix : string

The prefix to not find.

$string : string

The string to search.

$message : string = ''

The message to use for failure.

assertTextStartsWith()

Asserts that a string starts with a given prefix, ignoring differences in newlines.

public assertTextStartsWith(string $prefix, string $string[, string $message = '' ]) : void

Helpful for doing cross platform tests of blocks of text.

Parameters
$prefix : string

The prefix to check for.

$string : string

The string to search in.

$message : string = ''

The message to use for failure.

cleanupConsoleTrait()

Cleans state to get ready for the next test

public cleanupConsoleTrait() : void
Tags
after
psalm-suppress

PossiblyNullPropertyAssignmentValue

clearPlugins()

Clear all plugins from the global plugin collection.

public clearPlugins() : void

Useful in test case teardown methods.

deprecated()

Helper method for check deprecation methods

public deprecated(callable $callable) : void
Parameters
$callable : callable

callable function that will receive asserts

exec()

Runs CLI integration test

public exec(string $command[, array<string|int, mixed> $input = [] ]) : void
Parameters
$command : string

Command to run

$input : array<string|int, mixed> = []

Input values to pass to an interactive shell

Tags
throws
MissingConsoleInputException
throws
RuntimeException

fetchTable()

Convenience method to get a table instance.

public fetchTable([string|null $alias = null ][, array<string, mixed> $options = [] ]) : Table
Parameters
$alias : string|null = null

The alias name you want to get. Should be in CamelCase format. If null then the value of $defaultTable property is used.

$options : array<string, mixed> = []

The options you want to build the table with. If a table has already been loaded the registry options will be ignored.

Tags
throws
CakeException

If $alias argument and $defaultTable property both are null.

see
TableLocator::get()
since
4.3.0
Return values
Table

getFixtures()

Get the fixtures this test should use.

public getFixtures() : array<string|int, string>
Return values
array<string|int, string>

getMockForModel()

Mock a model, maintain fixtures and table association

public getMockForModel(string $alias[, array<string|int, string> $methods = [] ][, array<string, mixed> $options = [] ]) : Table|MockObject
Parameters
$alias : string

The model to get a mock for.

$methods : array<string|int, string> = []

The list of methods to mock

$options : array<string, mixed> = []

The config data for the mock's constructor.

Tags
throws
MissingTableClassException
Return values
Table|MockObject

loadFixtures()

Chooses which fixtures to load for a given test

public loadFixtures() : void

Disabling auto-fixtures is deprecated and only available using FixtureInjector fixture system.

Each parameter is a model name that corresponds to a fixture, i.e. 'Posts', 'Authors', etc. Passing no parameters will cause all fixtures on the test case to load.

Tags
see
TestCase::$autoFixtures
throws
RuntimeException

when no fixture manager is available.

loadPlugins()

Load plugins into a simulated application.

public loadPlugins([array<string, mixed> $plugins = [] ]) : BaseApplication

Useful to test how plugins being loaded/not loaded interact with other elements in CakePHP or applications.

Parameters
$plugins : array<string, mixed> = []

List of Plugins to load.

Return values
BaseApplication

loadRoutes()

Load routes for the application.

public loadRoutes([array<string|int, mixed>|null $appArgs = null ]) : void

If no application class can be found an exception will be raised. Routes for plugins will not be loaded. Use loadPlugins() or use Cake\TestSuite\IntegrationTestCaseTrait to better simulate all routes and plugins being loaded.

Parameters
$appArgs : array<string|int, mixed>|null = null

Constructor parameters for the application class.

Tags
since
4.0.1

removePlugins()

Remove plugins from the global plugin collection.

public removePlugins([array<string|int, string> $names = [] ]) : void

Useful in test case teardown methods.

Parameters
$names : array<string|int, string> = []

A list of plugins you want to remove.

setAppNamespace()

Set the app namespace

public static setAppNamespace([string $appNamespace = 'TestApp' ]) : string|null
Parameters
$appNamespace : string = 'TestApp'

The app namespace, defaults to "TestApp".

Return values
string|null

The previous app namespace or null if not set.

skipIf()

Overrides SimpleTestCase::skipIf to provide a boolean return value

public skipIf(bool $shouldSkip[, string $message = '' ]) : bool
Parameters
$shouldSkip : bool

Whether the test should be skipped.

$message : string = ''

The message to display.

Return values
bool

testBuildOptionParser()

Test \Fr3nch13\Utilities\Command\PackagesCommand::buildOptionParser()

public testBuildOptionParser() : void

testGetDescription()

Test \Fr3nch13\Utilities\Command\PackagesCommand::getDescription()

public testGetDescription() : void

testOptionBadPath()

Test when the path option is bad.

public testOptionBadPath() : void

testOptionCommandAll()

Test the command option with all selected.

public testOptionCommandAll() : void

testOptionCommandApp()

Test the command option with app selected.

public testOptionCommandApp() : void

testOptionCommandCompare()

Test the command option with compare selected.

public testOptionCommandCompare() : void

testOptionCommandPackages()

Test the command option with packages selected.

public testOptionCommandPackages() : void

useCommandRunner()

Set this test case to use the CommandRunner rather than the legacy ShellDispatcher

public useCommandRunner() : void

withErrorReporting()

Helper method for tests that needs to use error_reporting()

public withErrorReporting(int $errorLevel, callable $callable) : void
Parameters
$errorLevel : int

value of error_reporting() that needs to use

$callable : callable

callable function that will receive asserts

_assertAttributes()

Check the attributes as part of an assertTags() check.

protected _assertAttributes(array<string, mixed> $assertions, string $string[, bool $fullDebug = false ][, array<string|int, mixed>|string $regex = '' ]) : string|false
Parameters
$assertions : array<string, mixed>

Assertions to run.

$string : string

The HTML string to check.

$fullDebug : bool = false

Whether more verbose output should be used.

$regex : array<string|int, mixed>|string = ''

Full regexp from assertHtml

Return values
string|false

_getTableClassName()

Gets the class name for the table.

protected _getTableClassName(string $alias, array<string, mixed> $options) : string
Parameters
$alias : string

The model to get a mock for.

$options : array<string, mixed>

The config data for the mock's constructor.

Tags
throws
MissingTableClassException
psalm-return

class-string<\Cake\ORM\Table>

Return values
string

_normalizePath()

Normalize a path for comparison.

protected _normalizePath(string $path) : string
Parameters
$path : string

Path separated by "/" slash.

Return values
string

Normalized path separated by DIRECTORY_SEPARATOR.

addFixture()

Adds a fixture to this test case.

protected addFixture(string $fixture) : $this

Examples:

  • core.Tags
  • app.MyRecords
  • plugin.MyPluginName.MyModelName

Use this method inside your test cases' method to build up the fixture list.

Parameters
$fixture : string

Fixture

Return values
$this

assertNotWithinRange()

Compatibility function to test if a value is not between an acceptable range.

protected static assertNotWithinRange(float $expected, float $result, float $margin[, string $message = '' ]) : void
Parameters
$expected : float
$result : float
$margin : float

the rage of acceptation

$message : string = ''

the text to display if the assertion is not correct

assertOutputContainsRow()

Check that a row of cells exists in the output.

protected assertOutputContainsRow(array<string|int, mixed> $row[, string $message = '' ]) : void
Parameters
$row : array<string|int, mixed>

Row of cells to ensure exist in the output.

$message : string = ''

Failure message.

assertPathEquals()

Compatibility function to test paths.

protected static assertPathEquals(string $expected, string $result[, string $message = '' ]) : void
Parameters
$expected : string
$result : string
$message : string = ''

the text to display if the assertion is not correct

assertWithinRange()

Compatibility function to test if a value is between an acceptable range.

protected static assertWithinRange(float $expected, float $result, float $margin[, string $message = '' ]) : void
Parameters
$expected : float
$result : float
$margin : float

the rage of acceptation

$message : string = ''

the text to display if the assertion is not correct

commandStringToArgs()

Creates an $argv array from a command string

protected commandStringToArgs(string $command) : array<string|int, string>
Parameters
$command : string

Command string

Return values
array<string|int, string>

setupFixtures()

Initialized and loads any use fixtures.

protected setupFixtures() : void

skipUnless()

Compatibility function for skipping.

protected skipUnless(bool $condition[, string $message = '' ]) : bool
Parameters
$condition : bool

Condition to trigger skipping

$message : string = ''

Message for skip

Return values
bool

tearDown()

teardown any static object changes and restore them.

protected tearDown() : void

teardownFixtures()

Unloads any use fixtures.

protected teardownFixtures() : void

        
On this page

Search results