Skip to content

Commit

Permalink
Replace assertions with expectations API
Browse files Browse the repository at this point in the history
Should prevent failures on PHPUnit <9 where assertObjectHasProperty isn't defined.
  • Loading branch information
dingo-d committed Sep 23, 2023
1 parent 7ae39d6 commit 11a5e70
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 73 deletions.
49 changes: 27 additions & 22 deletions tests/Unit/Exception/ComponentExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,37 @@
function ($argument) {
$exceptionObject = ComponentException::throwNotStringOrArray($argument);
$type = \gettype($argument);

$this->assertIsObject($exceptionObject, "The {$exceptionObject} should be an instance of ComponentException class");
$this->assertObjectHasProperty('message', $exceptionObject, "Object doesn't contain message attribute");
$this->assertSame("{$argument} variable is not a string or array but rather {$type}", $exceptionObject->getMessage(), "Strings for message if item is {$type} do not match!");

})
->with('exceptionArguments');

test('Checks if the throwNotStringOrArray method functions correctly with objects.',
function () {

$object = new stdClass();
$exceptionObject = ComponentException::throwNotStringOrArray($object);

$this->assertIsObject($exceptionObject, "The object should be an instance of ComponentException class");
$this->assertObjectHasProperty('message', $exceptionObject, "Object doesn't contain message attribute");
$this->assertSame('Object couldn\'t be converted to string. Please provide only string or array.', $exceptionObject->getMessage(), "Strings for 'Object couldn't be converted to string' message do not match!");

});
$message = "{$argument} variable is not a string or array but rather {$type}";

expect($exceptionObject)->toBeObject()
->toBeInstanceOf(ComponentException::class)
->toHaveProperty('message')
->and($message)
->toEqual($exceptionObject->getMessage());
})
->with('exceptionArguments');

test('Checks if the throwNotStringOrArray method functions correctly with objects.',
function () {

$object = new stdClass();
$exceptionObject = ComponentException::throwNotStringOrArray($object);

expect($exceptionObject)->toBeObject()
->toBeInstanceOf(ComponentException::class)
->toHaveProperty('message')
->and('Object couldn\'t be converted to string. Please provide only string or array.')
->toEqual($exceptionObject->getMessage());
});

test('Checks if throwUnableToLocateComponent method will return correct response.', function () {

$component = 'nonexistent';
$output = ComponentException::throwUnableToLocateComponent($component);

$this->assertIsObject($output, "The {$output} should be an instance of ComponentException class");
$this->assertObjectHasProperty('message', $output, "Object doesn't contain message attribute");
$this->assertSame("Unable to locate component by path: {$component}", $output->getMessage(), "Strings for 'Unable to locate component by path' message do not match!");
expect($output)->toBeObject()
->toBeInstanceOf(ComponentException::class)
->toHaveProperty('message')
->and("Unable to locate component by path: {$component}")
->toEqual($output->getMessage());
});
9 changes: 6 additions & 3 deletions tests/Unit/Exception/FailedToLoadViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
$exception = new Exception('Error message');

$exceptionObject = FailedToLoadView::viewException($uri, $exception);
$message = "Could not load the View URI: {$uri}. Reason: {$exception->getMessage()}.";

$this->assertIsObject($exceptionObject, "The {$exceptionObject} should be an instance of FailedToLoadView class");
$this->assertObjectHasProperty('message', $exceptionObject, "Object doesn't contain message attribute");
$this->assertSame("Could not load the View URI: {$uri}. Reason: {$exception->getMessage()}.", $exceptionObject->getMessage(), "Strings for exception messages do not match!");
expect($exceptionObject)->toBeObject()
->toBeInstanceOf(FailedToLoadView::class)
->toHaveProperty('message')
->and($message)
->toEqual($exceptionObject->getMessage());
});
80 changes: 50 additions & 30 deletions tests/Unit/Exception/InvalidBlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,34 @@

$missingBlocks = InvalidBlock::missingBlocksException();

$this->assertIsObject($missingBlocks, "The {$missingBlocks} should be an instance of InvalidBlock class");
$this->assertObjectHasProperty('message', $missingBlocks, "Object doesn't contain message attribute");
$this->assertSame('There are no blocks added in your project.', $missingBlocks->getMessage(), "Strings for message if there are no blocks added to the project do not match!");
expect($missingBlocks)->toBeObject()
->toBeInstanceOf(InvalidBlock::class)
->toHaveProperty('message')
->and('There are no blocks added in your project.')
->toEqual($missingBlocks->getMessage());
});

test('Checks if missingComponentsException will return correct response.', function () {

$missingComponents = InvalidBlock::missingComponentsException();

$this->assertIsObject($missingComponents);
$this->assertObjectHasProperty('message', $missingComponents);
$this->assertSame('There are no components added in your project.', $missingComponents->getMessage(), "Strings for message if there are no components added to the project do not match!");
expect($missingComponents)->toBeObject()
->toBeInstanceOf(InvalidBlock::class)
->toHaveProperty('message')
->and('There are no components added in your project.')
->toEqual($missingComponents->getMessage());
});

test('Checks if missingNameException will return correct response.', function () {

$blockPath = 'some/random/path';
$missingName = InvalidBlock::missingNameException($blockPath);

$this->assertIsObject($missingName);
$this->assertObjectHasProperty('message', $missingName);
$this->assertSame("Block in this path {$blockPath} is missing blockName key in its manifest.json.", $missingName->getMessage(), "Strings for message if blockName key is missing in manifest.json do not match!");
expect($missingName)->toBeObject()
->toBeInstanceOf(InvalidBlock::class)
->toHaveProperty('message')
->and("Block in this path {$blockPath} is missing blockName key in its manifest.json.")
->toEqual($missingName->getMessage());
});

test('Checks if missingViewException will return correct response.', function () {
Expand All @@ -38,66 +44,80 @@
$blockPath = 'some/random/path';
$missingView = InvalidBlock::missingViewException($blockName, $blockPath);

$this->assertIsObject($missingView);
$this->assertObjectHasProperty('message', $missingView);
$this->assertSame("Block with this name {$blockName} is missing view template. Template name should be called {$blockName}.php, and it should be located in this path {$blockPath}", $missingView->getMessage(), "Strings for message if block is missing view template do not match!");
expect($missingView)->toBeObject()
->toBeInstanceOf(InvalidBlock::class)
->toHaveProperty('message')
->and("Block with this name {$blockName} is missing view template. Template name should be called {$blockName}.php, and it should be located in this path {$blockPath}")
->toEqual($missingView->getMessage());
});

test('Checks if missingRenderViewException will return correct response.', function () {

$blockPath = 'some/random/path';
$missingRenderView = InvalidBlock::missingRenderViewException($blockPath);

$this->assertIsObject($missingRenderView);
$this->assertObjectHasProperty('message', $missingRenderView);
$this->assertSame("Block view is missing in the provided path. Please check if {$blockPath} is the right path for your block view.", $missingRenderView->getMessage(), "Strings for message if block view is missing provided path do not match!");
expect($missingRenderView)->toBeObject()
->toBeInstanceOf(InvalidBlock::class)
->toHaveProperty('message')
->and("Block view is missing in the provided path. Please check if {$blockPath} is the right path for your block view.")
->toEqual($missingRenderView->getMessage());
});

test('Checks if missingSettingsManifestException will return correct response.', function () {

$manifestPath = 'some/random/path';
$missingManifestPath = InvalidBlock::missingSettingsManifestException($manifestPath);

$this->assertIsObject($missingManifestPath);
$this->assertObjectHasProperty('message', $missingManifestPath);
$this->assertSame("Global blocks settings manifest.json is missing on this location: {$manifestPath}.", $missingManifestPath->getMessage(), "Strings for message if global blocks settings manifest.json is missing do not match!");
expect($missingManifestPath)->toBeObject()
->toBeInstanceOf(InvalidBlock::class)
->toHaveProperty('message')
->and("Global blocks settings manifest.json is missing on this location: {$manifestPath}.")
->toEqual($missingManifestPath->getMessage());
});

test('Checks if missingWrapperManifestException will return correct response.', function () {

$manifestPath = 'some/random/path';
$missingManifestPath = InvalidBlock::missingWrapperManifestException($manifestPath);

$this->assertIsObject($missingManifestPath);
$this->assertObjectHasProperty('message', $missingManifestPath);
$this->assertSame("Wrapper blocks settings manifest.json is missing on this location: {$manifestPath}.", $missingManifestPath->getMessage(), "Strings for message if wrapper blocks settings manifest.json is missing do not match!");
expect($missingManifestPath)->toBeObject()
->toBeInstanceOf(InvalidBlock::class)
->toHaveProperty('message')
->and("Wrapper blocks settings manifest.json is missing on this location: {$manifestPath}.")
->toEqual($missingManifestPath->getMessage());
});

test('Checks if missingComponentManifestException will return correct response.', function () {

$manifestPath = 'some/random/path';
$missingComponentManifest = InvalidBlock::missingComponentManifestException($manifestPath);

$this->assertIsObject($missingComponentManifest);
$this->assertObjectHasProperty('message', $missingComponentManifest);
$this->assertSame("Component manifest.json is missing on this location: {$manifestPath}.", $missingComponentManifest->getMessage(), "Strings for message if component manifest.json is missing do not match!");
expect($missingComponentManifest)->toBeObject()
->toBeInstanceOf(InvalidBlock::class)
->toHaveProperty('message')
->and("Component manifest.json is missing on this location: {$manifestPath}.")
->toEqual($missingComponentManifest->getMessage());
});

test('Checks if missingWrapperViewException will return correct response.', function () {

$wrapperPath = 'some/random/path';
$missingWrapperView = InvalidBlock::missingWrapperViewException($wrapperPath);

$this->assertIsObject($missingWrapperView);
$this->assertObjectHasProperty('message', $missingWrapperView);
$this->assertSame("Wrapper view is missing. Template should be located in this path {$wrapperPath}", $missingWrapperView->getMessage(), "Strings for message if wrapper view is missing do not match!");
expect($missingWrapperView)->toBeObject()
->toBeInstanceOf(InvalidBlock::class)
->toHaveProperty('message')
->and("Wrapper view is missing. Template should be located in this path {$wrapperPath}")
->toEqual($missingWrapperView->getMessage());
});

test('Checks if missingNamespaceException will return correct response.', function () {

$missingNamespace = InvalidBlock::missingNamespaceException();

$this->assertIsObject($missingNamespace);
$this->assertObjectHasProperty('message', $missingNamespace);
$this->assertSame('Global Blocks settings manifest.json is missing a key called namespace. This key prefixes all block names.', $missingNamespace->getMessage(), "Strings for message global settings manifest.json is missing a key called namespace do not match!");
expect($missingNamespace)->toBeObject()
->toBeInstanceOf(InvalidBlock::class)
->toHaveProperty('message')
->and('Global Blocks settings manifest.json is missing a key called namespace. This key prefixes all block names.')
->toEqual($missingNamespace->getMessage());
});
8 changes: 5 additions & 3 deletions tests/Unit/Exception/InvalidCallbackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

$exceptionObject = InvalidCallback::fromCallback($callback);

$this->assertIsObject($exceptionObject, "The {$exceptionObject} should be an instance of InvalidBlock class");
$this->assertObjectHasProperty('message', $exceptionObject, "Object doesn't contain message attribute");
$this->assertSame("The callback {$callback} is not recognized and cannot be registered.", $exceptionObject->getMessage(), "Strings for message if callback isn't recognised do not match!");
expect($exceptionObject)->toBeObject()
->toBeInstanceOf(InvalidCallback::class)
->toHaveProperty('message')
->and("The callback {$callback} is not recognized and cannot be registered.")
->toEqual($exceptionObject->getMessage());
});
24 changes: 15 additions & 9 deletions tests/Unit/Exception/InvalidManifestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@

$exceptionObject = InvalidManifest::missingManifestItemException($key);

$this->assertIsObject($exceptionObject, "The {$exceptionObject} should be an instance of InvalidManifest class");
$this->assertObjectHasProperty('message', $exceptionObject, "Object doesn't contain message attribute");
$this->assertSame("{$key} key does not exist in manifest.json. Please check if provided key is correct.", $exceptionObject->getMessage(), "Strings for message if manifest key is missing do not match!");
expect($exceptionObject)->toBeObject()
->toBeInstanceOf(InvalidManifest::class)
->toHaveProperty('message')
->and("{$key} key does not exist in manifest.json. Please check if provided key is correct.")
->toEqual($exceptionObject->getMessage());
});

test('Checks if the missingManifestException method will return correct response.', function () {
Expand All @@ -21,9 +23,11 @@

$exceptionObject = InvalidManifest::missingManifestException($manifestPath);

$this->assertIsObject($exceptionObject, "The {$exceptionObject} should be an instance of InvalidManifest class");
$this->assertObjectHasProperty('message', $exceptionObject, "Object doesn't contain message attribute");
$this->assertSame("manifest.json is missing at this path: {$manifestPath}. Bundle the theme before using it. Or your bundling process is returning an error.", $exceptionObject->getMessage(), "Strings for message if manifest is missing do not match!");
expect($exceptionObject)->toBeObject()
->toBeInstanceOf(InvalidManifest::class)
->toHaveProperty('message')
->and("manifest.json is missing at this path: {$manifestPath}. Bundle the theme before using it. Or your bundling process is returning an error.")
->toEqual($exceptionObject->getMessage());
});

test('Checks if the manifestStructureException method will return correct response.', function () {
Expand All @@ -32,7 +36,9 @@

$exceptionObject = InvalidManifest::manifestStructureException($errorMessage);

$this->assertIsObject($exceptionObject, "The {$exceptionObject} should be an instance of InvalidManifest class");
$this->assertObjectHasProperty('message', $exceptionObject, "Object doesn't contain message attribute");
$this->assertSame($errorMessage, $exceptionObject->getMessage(), "Strings for manifest structure error message do not match!");
expect($exceptionObject)->toBeObject()
->toBeInstanceOf(InvalidManifest::class)
->toHaveProperty('message')
->and($errorMessage)
->toEqual($exceptionObject->getMessage());
});
8 changes: 5 additions & 3 deletions tests/Unit/Exception/InvalidServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

$exceptionObject = InvalidService::fromService($service);

$this->assertIsObject($exceptionObject, "The {$exceptionObject} should be an instance of InvalidService class");
$this->assertObjectHasProperty('message', $exceptionObject, "Object doesn't contain message attribute");
$this->assertSame("The service {$service} is not recognized and cannot be registered.", $exceptionObject->getMessage(), "Strings for message if service name isn't recognised do not match!");
expect($exceptionObject)->toBeObject()
->toBeInstanceOf(InvalidService::class)
->toHaveProperty('message')
->and("The service {$service} is not recognized and cannot be registered.")
->toEqual($exceptionObject->getMessage());
});
8 changes: 5 additions & 3 deletions tests/Unit/Exception/PluginActivationFailureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

$exceptionObject = PluginActivationFailure::activationMessage($message);

$this->assertIsObject($exceptionObject, "The {$exceptionObject} should be an instance of PluginActivationFailure class");
$this->assertObjectHasProperty('message', $exceptionObject, "Object doesn't contain message attribute");
$this->assertSame($message, $exceptionObject->getMessage(), "Strings for error activation message do not match!");
expect($exceptionObject)->toBeObject()
->toBeInstanceOf(PluginActivationFailure::class)
->toHaveProperty('message')
->and($message)
->toEqual($exceptionObject->getMessage());
});

0 comments on commit 11a5e70

Please sign in to comment.