gmail-filter-builder/tests/Unit/FilterTest.php

324 lines
7.3 KiB
PHP
Raw Normal View History

2017-11-03 11:23:22 +00:00
<?php
namespace Opdavies\Tests\GmailFilterBuilder;
2018-06-01 18:15:21 +00:00
use Opdavies\GmailFilterBuilder\Model\Filter;
2017-11-28 07:09:17 +00:00
use PHPUnit\Framework\TestCase;
2017-11-03 11:23:22 +00:00
2018-08-15 20:19:39 +00:00
/**
* Test creating new filters.
*
* @coversDefaultClass \Opdavies\GmailFilterBuilder\Model\Filter
*/
2017-11-28 07:09:17 +00:00
class FilterTest extends TestCase
2017-11-03 11:23:22 +00:00
{
/**
* @var Filter
*/
private $filter;
/**
* {@inheritdoc}
*/
public function setUp()
{
$this->filter = new Filter();
}
/**
2018-08-15 20:19:39 +00:00
* @covers ::has
2017-11-03 11:23:22 +00:00
*/
public function testHas()
{
$this->assertEquals(
['hasTheWord' => 'something'],
2018-06-01 18:33:57 +00:00
$this->filter->has('something')->toArray()
2017-11-03 11:23:22 +00:00
);
}
2017-11-04 09:20:46 +00:00
/**
2018-08-15 20:19:39 +00:00
* @covers ::hasNot
2017-11-04 09:20:46 +00:00
*/
2018-03-20 20:34:27 +00:00
public function testHasNot()
{
2017-11-04 09:20:46 +00:00
$this->assertEquals(
['doesNotHaveTheWord' => 'something'],
2018-06-01 18:33:57 +00:00
$this->filter->hasNot('something')->toArray()
2017-11-04 09:20:46 +00:00
);
}
2017-11-03 11:23:22 +00:00
/**
2018-08-15 20:19:39 +00:00
* @covers ::from
2017-11-03 11:23:22 +00:00
*/
public function testFrom()
{
// Ensure that we can set one from address.
$this->assertEquals(
2017-11-04 09:20:46 +00:00
['from' => ['foo@example.com']],
2018-06-01 18:33:57 +00:00
$this->filter->from('foo@example.com')->toArray()
2017-11-03 11:23:22 +00:00
);
// Ensure that we can set multiple from addresses.
$this->assertEquals(
2017-11-04 09:20:46 +00:00
['from' => ['foo@example.com', 'bar@example.com']],
2018-06-01 18:33:57 +00:00
$this->filter->from(['foo@example.com', 'bar@example.com'])->toArray()
2017-11-03 11:23:22 +00:00
);
}
/**
* Test that no 'from' key exists if no values were entered.
*
2018-08-15 20:19:39 +00:00
* @covers ::from
*/
public function testNoFromPropertyExistsIfTheValueIsEmpty()
{
2018-06-01 18:33:57 +00:00
$this->assertArrayNotHasKey('from', $this->filter->from('')->toArray());
$this->assertArrayNotHasKey('from', $this->filter->from([])->toArray());
2017-11-03 11:23:22 +00:00
}
2017-11-04 09:20:46 +00:00
/**
2018-08-15 20:19:39 +00:00
* @covers ::to
2017-11-04 09:20:46 +00:00
*/
public function testTo()
{
$this->assertEquals(
['to' => ['foo@example.com']],
2018-06-01 18:33:57 +00:00
$this->filter->to('foo@example.com')->toArray()
2017-11-04 09:20:46 +00:00
);
$this->assertEquals(
['to' => ['bar@example.com', 'baz@example.com']],
2018-06-01 18:33:57 +00:00
$this->filter->to(['bar@example.com', 'baz@example.com'])->toArray()
2017-11-04 09:20:46 +00:00
);
}
/**
* Test that no 'to' key exists if values were entered.
*/
public function testNoToPropertyExistsIfTheValueIsEmpty()
{
2018-06-01 18:33:57 +00:00
$this->assertArrayNotHasKey('to', $this->filter->to('')->toArray());
$this->assertArrayNotHasKey('to', $this->filter->to([])->toArray());
}
2017-11-04 09:20:46 +00:00
/**
2018-08-15 20:19:39 +00:00
* @covers ::subject
2017-11-04 09:20:46 +00:00
*/
public function testSubject()
{
$this->assertEquals(
['subject' => '"Something"'],
2018-06-01 18:33:57 +00:00
$this->filter->subject('Something')->toArray()
2017-11-04 09:20:46 +00:00
);
}
/**
* Test that multiple subject conditions can be added.
*/
public function testMultipleSubjectsCanBeAdded()
{
$this->assertEquals(
['subject' => '"Test"|"Foo bar"'],
$this->filter->subject(['Test', 'Foo bar'])->toArray()
);
}
2017-11-04 09:20:46 +00:00
/**
2018-08-15 20:19:39 +00:00
* @covers ::hasAttachment
2017-11-04 09:20:46 +00:00
*/
public function testHasAttachment()
{
$this->assertEquals(
['hasAttachment' => 'true'],
2018-06-01 18:33:57 +00:00
$this->filter->hasAttachment()->toArray()
2017-11-04 09:20:46 +00:00
);
}
2018-08-15 20:36:37 +00:00
/**
* @covers ::list
*/
public function testList()
{
$this->assertEquals(
['hasTheWord' => 'list:foobar'],
$this->filter->list('foobar')->toArray()
);
}
2018-08-15 20:23:10 +00:00
/**
* @covers ::excludeChats
*/
2017-11-04 09:20:46 +00:00
public function testExcludeChats()
{
$this->assertEquals(
['excludeChats' => 'true'],
2018-06-01 18:33:57 +00:00
$this->filter->excludeChats()->toArray()
2017-11-04 09:20:46 +00:00
);
}
2018-08-15 20:19:39 +00:00
2017-11-03 11:23:22 +00:00
/**
2018-08-15 20:19:39 +00:00
* @covers ::label
2017-11-03 11:23:22 +00:00
*/
public function testLabel()
{
2017-11-03 23:22:13 +00:00
$this->assertEquals(
['label' => 'Foo'],
2018-06-01 18:33:57 +00:00
$this->filter->label('Foo')->toArray()
2017-11-03 23:22:13 +00:00
);
2017-11-03 11:23:22 +00:00
}
/**
2018-08-15 20:19:39 +00:00
* @covers ::archive
2017-11-03 11:23:22 +00:00
*/
public function testArchive()
{
$this->assertEquals(
['shouldArchive' => 'true'],
2018-06-01 18:33:57 +00:00
$this->filter->archive()->toArray()
2017-11-03 11:23:22 +00:00
);
}
/**
2018-08-15 20:19:39 +00:00
* @covers ::labelAndArchive
2017-11-03 11:23:22 +00:00
*/
public function testLabelAndArchive()
{
$this->assertEquals(
['shouldArchive' => 'true', 'label' => 'Foo'],
2018-06-01 18:33:57 +00:00
$this->filter->labelAndArchive('Foo')->toArray()
2017-11-03 11:23:22 +00:00
);
}
/**
2018-08-15 20:19:39 +00:00
* @covers ::spam
2017-11-03 11:23:22 +00:00
*/
public function testSpam()
{
$this->assertEquals(
[
'shouldSpam' => 'true',
'shouldNeverSpam' => 'false'
],
2018-06-01 18:33:57 +00:00
$this->filter->spam()->toArray()
2017-11-03 11:23:22 +00:00
);
}
/**
2018-08-15 20:19:39 +00:00
* @covers ::neverSpam
2017-11-03 11:23:22 +00:00
*/
public function testNeverSpam()
{
$this->assertEquals(
[
'shouldSpam' => 'false',
'shouldNeverSpam' => 'true'
],
2018-06-01 18:33:57 +00:00
$this->filter->neverSpam()->toArray()
2017-11-03 11:23:22 +00:00
);
}
/**
2018-08-15 20:19:39 +00:00
* @covers ::trash
2017-11-03 11:23:22 +00:00
*/
public function testTrash()
{
$this->assertEquals(
['shouldTrash' => 'true'],
2018-06-01 18:33:57 +00:00
$this->filter->trash()->toArray()
2017-11-03 23:22:13 +00:00
);
}
2018-08-15 20:23:10 +00:00
/**
* @covers ::read
*/
2017-11-03 23:45:54 +00:00
public function testMarkAsRead()
{
$this->assertEquals(
['markAsRead' => 'true'],
2018-06-01 18:33:57 +00:00
$this->filter->read()->toArray()
2017-11-03 23:45:54 +00:00
);
}
2018-08-15 20:23:10 +00:00
/**
* @covers ::star
*/
2017-11-03 23:45:54 +00:00
public function testStar()
{
$this->assertEquals(
['shouldStar' => 'true'],
2018-06-01 18:33:57 +00:00
$this->filter->star()->toArray()
2017-11-03 23:45:54 +00:00
);
}
2018-08-15 20:23:10 +00:00
/**
* @covers ::forward
*/
2017-11-03 23:45:54 +00:00
public function testForwardTo()
{
$this->assertEquals(
['forwardTo' => 'foo@example.com'],
2018-06-01 18:33:57 +00:00
$this->filter->forward('foo@example.com')->toArray()
2017-11-03 23:45:54 +00:00
);
}
2018-08-15 20:23:10 +00:00
/**
* @covers ::important
*/
2017-11-03 23:45:54 +00:00
public function testMarkImportant()
{
$this->assertEquals(
['shouldAlwaysMarkAsImportant' => 'true'],
2018-06-01 18:33:57 +00:00
$this->filter->important()->toArray()
2017-11-03 23:45:54 +00:00
);
}
2018-08-15 20:23:10 +00:00
/**
* @covers ::notImportant
*/
2017-11-03 23:45:54 +00:00
public function testMarkNotImportant()
{
$this->assertEquals(
['shouldNeverMarkAsImportant' => 'true'],
2018-06-01 18:33:57 +00:00
$this->filter->notImportant()->toArray()
2017-11-03 23:45:54 +00:00
);
}
2018-08-15 20:19:39 +00:00
2018-08-15 20:23:10 +00:00
/**
* @covers ::categorise
*/
2017-11-03 23:45:54 +00:00
public function testCategorise()
{
$this->assertEquals(
['smartLabelToApply' => 'Foo'],
2018-06-01 18:33:57 +00:00
$this->filter->categorise('Foo')->toArray()
2017-11-03 23:45:54 +00:00
);
}
2017-11-03 23:22:13 +00:00
public function testMethodsCanBeChained()
{
$this->assertEquals(
[
2017-11-04 09:20:46 +00:00
'from' => ['foo@example.com', 'bar@example.com'],
2017-11-03 23:22:13 +00:00
'hasTheWord' => 'Something',
2017-11-04 09:20:46 +00:00
'excludeChats' => 'true',
2017-11-03 23:22:13 +00:00
'label' => 'Foo',
2017-11-03 23:45:54 +00:00
'markAsRead' => 'true',
2017-11-03 23:22:13 +00:00
'shouldArchive' => 'true',
'shouldNeverSpam' => 'true',
'shouldSpam' => 'false',
2017-11-03 23:45:54 +00:00
'shouldStar' => 'true',
2017-11-04 09:20:46 +00:00
'shouldAlwaysMarkAsImportant' => 'true',
2017-11-03 23:22:13 +00:00
],
$this->filter->from(['foo@example.com ', 'bar@example.com'])
2017-11-03 23:22:13 +00:00
->has('Something')
2017-11-04 09:20:46 +00:00
->excludeChats()
2017-11-03 23:22:13 +00:00
->labelAndArchive('Foo')
2017-11-03 23:45:54 +00:00
->read()
2017-11-04 09:20:46 +00:00
->important()
2017-11-03 23:22:13 +00:00
->neverSpam()
2017-11-03 23:45:54 +00:00
->star()
2018-06-01 18:33:57 +00:00
->toArray()
2017-11-03 11:23:22 +00:00
);
}
}