Skip to content

Commit

Permalink
Make naming make sense again (#3)
Browse files Browse the repository at this point in the history
* Make naming make sense again
* Add tests for trait
  • Loading branch information
svenluijten authored Apr 30, 2021
1 parent 2a83beb commit 17d644f
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 19 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Or add the package to your dependencies in `composer.json` and run

## Usage
This package exposes a single trait: `\Sven\LaravelViewAssertions\InteractsWithViews`.
When you `use` this trait in your tests as below, you'll get access to 4 assertions:
When you `use` this trait in your tests as below, you'll get access to several assertions:

```php
<?php
Expand All @@ -50,13 +50,15 @@ class ExampleTest extends TestCase
// ...

$this->assertViewExists('some.view-file');
$this->assertViewsExist(['posts.index', 'posts.show']);
}

public function test_it_does_not_create_a_view()
{
// ...

$this->assertViewNotExists('some.view-file');
$this->assertViewDoesNotExist('some.view-file');
$this->assertViewsDoNotExist(['posts.edit', 'posts.create']);
}

public function test_the_view_equals()
Expand All @@ -70,7 +72,7 @@ class ExampleTest extends TestCase
{
// ...

$this->assertViewNotEquals('This Is Not The Content You\'re Looking For', 'index');
$this->assertViewDoesNotEqual('This Is Not The Content You\'re Looking For', 'index');
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use PHPUnit\Framework\Constraint\Constraint;

class ViewNotEquals extends Constraint
class ViewDoesNotEqual extends Constraint
{
public function __construct(protected string $expected)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use InvalidArgumentException;
use PHPUnit\Framework\Constraint\Constraint;

class ViewNotExists extends Constraint
class ViewDoesNotExist extends Constraint
{
protected function matches($other): bool
{
Expand Down
34 changes: 30 additions & 4 deletions src/InteractsWithViews.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace Sven\LaravelViewAssertions;

use PHPUnit\Framework\Assert as PHPUnit;
use Sven\LaravelViewAssertions\Constraints\ViewDoesNotEqual;
use Sven\LaravelViewAssertions\Constraints\ViewDoesNotExist;
use Sven\LaravelViewAssertions\Constraints\ViewEquals;
use Sven\LaravelViewAssertions\Constraints\ViewExists;
use Sven\LaravelViewAssertions\Constraints\ViewNotEquals;
use Sven\LaravelViewAssertions\Constraints\ViewNotExists;

trait InteractsWithViews
{
Expand All @@ -15,18 +15,44 @@ public function assertViewExists(string $name, string $message = ''): void
PHPUnit::assertThat($name, new ViewExists, $message);
}

public function assertViewsExist(array $names): void
{
foreach ($names as $name) {
$this->assertViewExists($name);
}
}

public function assertViewDoesNotExist(string $name, string $message = ''): void
{
PHPUnit::assertThat($name, new ViewDoesNotExist, $message);
}

/** @deprecated */
public function assertViewNotExists(string $name, string $message = ''): void
{
PHPUnit::assertThat($name, new ViewNotExists, $message);
$this->assertViewDoesNotExist($name, $message);
}

public function assertViewsDoNotExist(array $names): void
{
foreach ($names as $name) {
$this->assertViewDoesNotExist($name);
}
}

public function assertViewEquals(string $expected, string $view, string $message = ''): void
{
PHPUnit::assertThat($view, new ViewEquals($expected), $message);
}

public function assertViewDoesNotEqual(string $expected, string $view, string $message = ''): void
{
PHPUnit::assertThat($view, new ViewDoesNotEqual($expected), $message);
}

/** @deprecated */
public function assertViewNotEquals(string $expected, string $view, string $message = ''): void
{
PHPUnit::assertThat($view, new ViewNotEquals($expected), $message);
$this->assertViewDoesNotEqual($expected, $view, $message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

namespace Sven\LaravelViewAssertions\Tests\Constraints;

use Sven\LaravelViewAssertions\Constraints\ViewNotEquals;
use Sven\LaravelViewAssertions\Constraints\ViewDoesNotEqual;
use Sven\LaravelViewAssertions\Tests\TestCase;

class ViewNotEqualsTest extends TestCase
class ViewDoesNotEqualTest extends TestCase
{
/** @test */
public function the_contents_of_a_view_do_not_equal_the_given_value(): void
{
$this->makeView('viewname', 'Contents of the view');

$constraint = new ViewNotEquals('Other contents');
$constraint = new ViewDoesNotEqual('Other contents');

$this->assertTrue($constraint->evaluate('viewname', '', true));
}
Expand All @@ -22,7 +22,7 @@ public function the_contents_of_a_view_equal_the_given_value(): void
{
$this->makeView('viewname', 'Contents of the view');

$constraint = new ViewNotEquals('Contents of the view');
$constraint = new ViewDoesNotEqual('Contents of the view');

$this->assertFalse($constraint->evaluate('viewname', '', true));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@

namespace Sven\LaravelViewAssertions\Tests\Constraints;

use Sven\LaravelViewAssertions\Constraints\ViewNotExists;
use Sven\LaravelViewAssertions\Constraints\ViewDoesNotExist;
use Sven\LaravelViewAssertions\Tests\TestCase;

class ViewNotExistsTest extends TestCase
class ViewDoesNotExistTest extends TestCase
{
/** @test */
public function a_view_exists(): void
{
$this->assertTrue((new ViewNotExists)->evaluate('does-not-exist', '', true));
$this->assertTrue((new ViewDoesNotExist)->evaluate('does-not-exist', '', true));
}

/** @test */
public function a_view_does_not_exist(): void
{
$this->makeView('exists');

$this->assertFalse((new ViewNotExists)->evaluate('exists', '', true));
$this->assertFalse((new ViewDoesNotExist)->evaluate('exists', '', true));
}
}
4 changes: 2 additions & 2 deletions tests/Constraints/ViewExistsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ public function a_view_exists(): void
{
$this->makeView('exists');

$this->assertTrue((new ViewExists)->evaluate('exists', '', true));
$this->assertTrue((new ViewExists)->evaluate('exists', returnResult: true));
}

/** @test */
public function a_view_does_not_exist(): void
{
$this->assertFalse((new ViewExists)->evaluate('does-not-exist', '', true));
$this->assertFalse((new ViewExists)->evaluate('does-not-exist', returnResult: true));
}
}
32 changes: 32 additions & 0 deletions tests/InteractsWithViewsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Sven\LaravelViewAssertions\Tests;

use Sven\LaravelViewAssertions\InteractsWithViews;

class InteractsWithViewsTest extends TestCase
{
use InteractsWithViews;

/** @test */
public function views_existance(): void
{
$this->makeView('exists');
$this->makeView('also-exists');

$this->assertViewExists('exists');
$this->assertViewsExist(['exists', 'also-exists']);
$this->assertViewDoesNotExist('does-not-exist');
$this->assertViewsDoNotExist(['does-not-exist', 'also-does-not-exist']);
}

/** @test */
public function views_equal(): void
{
$this->makeView('one', 'hello');
$this->makeView('two', 'goodbye');

$this->assertViewEquals('hello', 'one');
$this->assertViewDoesNotEqual('farewell', 'two');
}
}

0 comments on commit 17d644f

Please sign in to comment.