Skip to content

Commit a7985e4

Browse files
authored
Merge pull request #9 from SebKay/better-testing
Better testing
2 parents 0e9a857 + 493962e commit a7985e4

9 files changed

Lines changed: 109 additions & 43 deletions

File tree

.github/workflows/php.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Validate PHP
1+
name: Test PHP
22

33
on: [pull_request]
44

@@ -16,8 +16,8 @@ jobs:
1616
- name: Run PHPCS
1717
run: composer phpcs
1818

19-
run-tests:
20-
name: Run Tests
19+
analyse:
20+
name: Analyse Code
2121
runs-on: ubuntu-latest
2222

2323
steps:
@@ -26,11 +26,11 @@ jobs:
2626
- name: Install dependencies
2727
run: composer install --prefer-dist --no-progress --no-suggest
2828

29-
- name: Run PHPUnit
30-
run: composer phpunit
29+
- name: Run Psalm
30+
run: composer psalm
3131

32-
analyse:
33-
name: Analyse Code
32+
run-tests:
33+
name: Run Tests
3434
runs-on: ubuntu-latest
3535

3636
steps:
@@ -39,5 +39,5 @@ jobs:
3939
- name: Install dependencies
4040
run: composer install --prefer-dist --no-progress --no-suggest
4141

42-
- name: Run Psalm
43-
run: composer psalm
42+
- name: Run PHPUnit
43+
run: composer phpunit

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Directories
12
vendor/
3+
coverage/
4+
5+
# Files
26
composer.lock
37
.phpunit.result.cache

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
![Validate PHP](https://github.com/SebKay/noticeable/workflows/Validate%20PHP/badge.svg)
1+
![Test PHP](https://github.com/SebKay/noticeable/workflows/Test%20PHP/badge.svg)
22

33
# Noticeable
44
Easily output a simple flash message/notice to the page, but only once.

composer.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sebkay/noticeable",
3-
"version" : "2.1.0",
3+
"version" : "2.2.0",
44
"authors": [
55
{
66
"name": "Seb Kay",
@@ -24,14 +24,17 @@
2424
},
2525
"require-dev": {
2626
"squizlabs/php_codesniffer": "^3.5",
27-
"vimeo/psalm": "^3.12",
27+
"vimeo/psalm": "^4.2",
2828
"phpunit/phpunit": "^9.4"
2929
},
3030
"scripts" : {
3131
"refresh" : "git clean -xffd && composer install --ansi",
3232
"phpcs" : "./vendor/bin/phpcs --standard=PSR12 src",
3333
"phpcbf": "./vendor/bin/phpcbf --standard=PSR12 src",
34-
"psalm" : "./vendor/bin/psalm",
35-
"phpunit" : "./vendor/bin/phpunit --colors=always tests"
34+
"psalm": "./vendor/bin/psalm --show-info=true",
35+
"phpunit": "./vendor/bin/phpunit --colors=always tests --process-isolation",
36+
"phpunit:testdox": "@phpunit --testdox",
37+
"phpunit:coverage": "@phpunit --coverage-html coverage",
38+
"test" : "composer phpcs && composer psalm && composer phpunit"
3639
}
3740
}

phpunit.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
colors="true"
55
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.4/phpunit.xsd">
6+
<coverage processUncoveredFiles="true">
7+
<include>
8+
<directory suffix=".php">./src</directory>
9+
</include>
10+
</coverage>
611
<testsuites>
712
<testsuite name="Unit">
813
<directory suffix="Test.php">./tests/Unit/</directory>

src/Notice.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,7 @@ class Notice
1414
*/
1515
public static function set(NoticeContent $notice_content): void
1616
{
17-
if ($notice_content->message() == '') {
18-
throw new \InvalidArgumentException('Please provide a notice message.');
19-
}
20-
21-
if ($notice_content->type() == '') {
22-
throw new \InvalidArgumentException('Please provide a notice type.');
23-
}
24-
17+
$notice_content->verifyMessage();
2518
$notice_content->verifyType();
2619

2720
$_SESSION[self::$session_name] = $notice_content;

src/NoticeContent.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,43 @@ public function __construct(string $message, string $type)
3131
$this->type = $type;
3232
}
3333

34+
public function verifyMessage()
35+
{
36+
if ($this->message() == '') {
37+
throw new \InvalidArgumentException('Please provide a message.');
38+
}
39+
}
40+
3441
/**
3542
* Verify the type is valid
3643
*
3744
* @return void
3845
*/
3946
public function verifyType(): void
4047
{
41-
if (!in_array($this->type, $this->allowed_types)) {
48+
if ($this->type() == '') {
49+
throw new \InvalidArgumentException('Please provide a type.');
50+
}
51+
52+
if (!in_array($this->type(), $this->allowed_types)) {
4253
$types_as_string = implode(', ', $this->allowed_types);
4354

4455
throw new \InvalidArgumentException(
45-
"'$this->type' is not a valid type. Please use either $types_as_string."
56+
"'{$this->type()}' is not a valid type. Please use either $types_as_string."
4657
);
4758
}
4859
}
4960

61+
/**
62+
* Check if content is blank
63+
*
64+
* @return boolean
65+
*/
66+
public function isEmpty()
67+
{
68+
return ($this->message() == '' && $this->type() == '' ? true : false);
69+
}
70+
5071
/**
5172
* Get the message
5273
*

tests/Unit/NoticeContentTest.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,51 @@ class NoticeContentTest extends Test
99
/**
1010
* @test
1111
*/
12-
public function test_type_can_be_checked_against_valid_types()
12+
public function test_message_is_set_successfully()
13+
{
14+
$notice_content = new NoticeContent('This is a notice.', 'success');
15+
16+
$this->assertSame('This is a notice.', $notice_content->message());
17+
}
18+
19+
/**
20+
* @test
21+
*/
22+
public function test_type_is_set_successfully()
23+
{
24+
$notice_content = new NoticeContent('This is a notice.', 'success');
25+
26+
$this->assertSame('success', $notice_content->type());
27+
}
28+
29+
/**
30+
* @test
31+
*/
32+
public function test_empty_message_is_caught()
33+
{
34+
$notice_content = new NoticeContent('', 'success');
35+
36+
$this->expectException(\InvalidArgumentException::class);
37+
38+
$notice_content->verifyMessage();
39+
}
40+
41+
/**
42+
* @test
43+
*/
44+
public function test_empty_type_is_caught()
45+
{
46+
$notice_content = new NoticeContent('This is a notice.', '');
47+
48+
$this->expectException(\InvalidArgumentException::class);
49+
50+
$notice_content->verifyType();
51+
}
52+
53+
/**
54+
* @test
55+
*/
56+
public function test_invalid_type_is_caught()
1357
{
1458
$notice_content = new NoticeContent('This is a notice.', 'successes');
1559

tests/Unit/NoticeTest.php

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,34 @@ public function test_value_object_is_returned()
1616
new NoticeContent('This is a notice.', 'success')
1717
);
1818

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

2222
/**
2323
* @test
2424
*/
25-
public function test_message_cannot_be_empty_on_set()
25+
public function test_no_errors_when_notice_isnt_set()
2626
{
27-
$this->expectException(\InvalidArgumentException::class);
28-
29-
Notice::set(
30-
new NoticeContent('', 'success')
31-
);
27+
$this->assertInstanceOf(NoticeContent::class, Notice::get());
3228
}
3329

3430
/**
3531
* @test
3632
*/
37-
public function test_type_cannot_be_empty_on_set()
33+
public function test_notice_is_cleared_after_initial_get()
3834
{
39-
$this->expectException(\InvalidArgumentException::class);
40-
4135
Notice::set(
42-
new NoticeContent('This is a notice.', '')
36+
new NoticeContent('This is a notice.', 'success')
4337
);
44-
}
4538

46-
/**
47-
* @test
48-
*/
49-
public function test_no_errors_when_notice_doesnt_exist()
50-
{
51-
$this->assertInstanceOf('\Noticeable\NoticeContent', Notice::get());
39+
$notice_1 = Notice::get();
40+
41+
// Content was successfully retrieved on first get
42+
$this->assertFalse($notice_1->isEmpty());
43+
44+
$notice_2 = Notice::get();
45+
46+
// Content was successfully removed on first get
47+
$this->assertTrue($notice_2->isEmpty());
5248
}
5349
}

0 commit comments

Comments
 (0)