Skip to content

Commit

Permalink
Merge pull request #9 from SebKay/better-testing
Browse files Browse the repository at this point in the history
Better testing
  • Loading branch information
SebKay authored Dec 1, 2020
2 parents 0e9a857 + 493962e commit a7985e4
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 43 deletions.
18 changes: 9 additions & 9 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Validate PHP
name: Test PHP

on: [pull_request]

Expand All @@ -16,8 +16,8 @@ jobs:
- name: Run PHPCS
run: composer phpcs

run-tests:
name: Run Tests
analyse:
name: Analyse Code
runs-on: ubuntu-latest

steps:
Expand All @@ -26,11 +26,11 @@ jobs:
- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest

- name: Run PHPUnit
run: composer phpunit
- name: Run Psalm
run: composer psalm

analyse:
name: Analyse Code
run-tests:
name: Run Tests
runs-on: ubuntu-latest

steps:
Expand All @@ -39,5 +39,5 @@ jobs:
- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest

- name: Run Psalm
run: composer psalm
- name: Run PHPUnit
run: composer phpunit
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Directories
vendor/
coverage/

# Files
composer.lock
.phpunit.result.cache
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![Validate PHP](https://github.com/SebKay/noticeable/workflows/Validate%20PHP/badge.svg)
![Test PHP](https://github.com/SebKay/noticeable/workflows/Test%20PHP/badge.svg)

# Noticeable
Easily output a simple flash message/notice to the page, but only once.
Expand Down
11 changes: 7 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sebkay/noticeable",
"version" : "2.1.0",
"version" : "2.2.0",
"authors": [
{
"name": "Seb Kay",
Expand All @@ -24,14 +24,17 @@
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.5",
"vimeo/psalm": "^3.12",
"vimeo/psalm": "^4.2",
"phpunit/phpunit": "^9.4"
},
"scripts" : {
"refresh" : "git clean -xffd && composer install --ansi",
"phpcs" : "./vendor/bin/phpcs --standard=PSR12 src",
"phpcbf": "./vendor/bin/phpcbf --standard=PSR12 src",
"psalm" : "./vendor/bin/psalm",
"phpunit" : "./vendor/bin/phpunit --colors=always tests"
"psalm": "./vendor/bin/psalm --show-info=true",
"phpunit": "./vendor/bin/phpunit --colors=always tests --process-isolation",
"phpunit:testdox": "@phpunit --testdox",
"phpunit:coverage": "@phpunit --coverage-html coverage",
"test" : "composer phpcs && composer psalm && composer phpunit"
}
}
5 changes: 5 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
colors="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.4/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit/</directory>
Expand Down
9 changes: 1 addition & 8 deletions src/Notice.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,7 @@ class Notice
*/
public static function set(NoticeContent $notice_content): void
{
if ($notice_content->message() == '') {
throw new \InvalidArgumentException('Please provide a notice message.');
}

if ($notice_content->type() == '') {
throw new \InvalidArgumentException('Please provide a notice type.');
}

$notice_content->verifyMessage();
$notice_content->verifyType();

$_SESSION[self::$session_name] = $notice_content;
Expand Down
25 changes: 23 additions & 2 deletions src/NoticeContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,43 @@ public function __construct(string $message, string $type)
$this->type = $type;
}

public function verifyMessage()
{
if ($this->message() == '') {
throw new \InvalidArgumentException('Please provide a message.');
}
}

/**
* Verify the type is valid
*
* @return void
*/
public function verifyType(): void
{
if (!in_array($this->type, $this->allowed_types)) {
if ($this->type() == '') {
throw new \InvalidArgumentException('Please provide a type.');
}

if (!in_array($this->type(), $this->allowed_types)) {
$types_as_string = implode(', ', $this->allowed_types);

throw new \InvalidArgumentException(
"'$this->type' is not a valid type. Please use either $types_as_string."
"'{$this->type()}' is not a valid type. Please use either $types_as_string."
);
}
}

/**
* Check if content is blank
*
* @return boolean
*/
public function isEmpty()
{
return ($this->message() == '' && $this->type() == '' ? true : false);
}

/**
* Get the message
*
Expand Down
46 changes: 45 additions & 1 deletion tests/Unit/NoticeContentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,51 @@ class NoticeContentTest extends Test
/**
* @test
*/
public function test_type_can_be_checked_against_valid_types()
public function test_message_is_set_successfully()
{
$notice_content = new NoticeContent('This is a notice.', 'success');

$this->assertSame('This is a notice.', $notice_content->message());
}

/**
* @test
*/
public function test_type_is_set_successfully()
{
$notice_content = new NoticeContent('This is a notice.', 'success');

$this->assertSame('success', $notice_content->type());
}

/**
* @test
*/
public function test_empty_message_is_caught()
{
$notice_content = new NoticeContent('', 'success');

$this->expectException(\InvalidArgumentException::class);

$notice_content->verifyMessage();
}

/**
* @test
*/
public function test_empty_type_is_caught()
{
$notice_content = new NoticeContent('This is a notice.', '');

$this->expectException(\InvalidArgumentException::class);

$notice_content->verifyType();
}

/**
* @test
*/
public function test_invalid_type_is_caught()
{
$notice_content = new NoticeContent('This is a notice.', 'successes');

Expand Down
32 changes: 14 additions & 18 deletions tests/Unit/NoticeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,34 @@ public function test_value_object_is_returned()
new NoticeContent('This is a notice.', 'success')
);

$this->assertInstanceOf('\Noticeable\NoticeContent', Notice::get());
$this->assertInstanceOf(NoticeContent::class, Notice::get());
}

/**
* @test
*/
public function test_message_cannot_be_empty_on_set()
public function test_no_errors_when_notice_isnt_set()
{
$this->expectException(\InvalidArgumentException::class);

Notice::set(
new NoticeContent('', 'success')
);
$this->assertInstanceOf(NoticeContent::class, Notice::get());
}

/**
* @test
*/
public function test_type_cannot_be_empty_on_set()
public function test_notice_is_cleared_after_initial_get()
{
$this->expectException(\InvalidArgumentException::class);

Notice::set(
new NoticeContent('This is a notice.', '')
new NoticeContent('This is a notice.', 'success')
);
}

/**
* @test
*/
public function test_no_errors_when_notice_doesnt_exist()
{
$this->assertInstanceOf('\Noticeable\NoticeContent', Notice::get());
$notice_1 = Notice::get();

// Content was successfully retrieved on first get
$this->assertFalse($notice_1->isEmpty());

$notice_2 = Notice::get();

// Content was successfully removed on first get
$this->assertTrue($notice_2->isEmpty());
}
}

0 comments on commit a7985e4

Please # to comment.