Completed
Push — master ( 513b92...79c3ce )
by Richard van
15s queued 11s
created

testNestedItemsRetrievalViaArrayAccess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Micrometa\Ports\Item;
4
5
use Jkphl\Micrometa\Infrastructure\Factory\MicroformatsFactory;
6
use Jkphl\Micrometa\Ports\Exceptions\OutOfBoundsException;
7
use Jkphl\Micrometa\Ports\Item\ItemInterface;
8
use Jkphl\Micrometa\Ports\Item\ItemList;
9
use Jkphl\Micrometa\Tests\MicroformatsFeedTrait;
10
use PHPUnit\Framework\TestCase;
11
12
class ItemListTest extends TestCase
13
{
14
    /**
15
     * Use the Microformats feed method
16
     */
17
    use MicroformatsFeedTrait;
18
19
    private $feedItemList;
20
21
    protected function setUp(): void
22
    {
23
        $this->feedItemList = new ItemList([$this->getFeedItem(), $this->getFeedItem()]);
24
    }
25
26
    public function testNestedItemsCounts()
27
    {
28
        // Test the number of nested items
29
        self::assertCount(2, $this->feedItemList);
30
        self::assertCount(2, $this->feedItemList->getItems());
31
    }
32
33
    public function testNestedItemsIteration()
34
    {
35
        foreach ($this->feedItemList->getItems() as $itemIndex => $entryItem) {
36
            self::assertInstanceOf(ItemInterface::class, $entryItem);
37
            self::assertIsInt($itemIndex);
38
        }
39
    }
40
41
    public function testNestedItemsRetrievalViaArrayAccess()
42
    {
43
        $entryItems = $this->feedItemList->getFirstItem()->getItems('h-entry');
44
        $entryItem = $entryItems[1];
45
46
        self::assertInstanceOf(ItemInterface::class, $entryItem);
47
    }
48
49
    public function testFirstNestedItemRetrieval()
50
    {
51
        self::assertInstanceOf(ItemInterface::class, $this->feedItemList[0]->getFirstItem('h-entry'));
52
        self::assertInstanceOf(
53
            ItemInterface::class,
54
            $this->feedItemList[0]->getFirstItem('h-entry', MicroformatsFactory::MF2_PROFILE_URI)
55
        );
56
    }
57
58
    public function testExistingFirstNestedItem()
59
    {
60
        self::assertEquals('John Doe', $this->feedItemList[0]->hEntry()->author->name);
61
        self::assertEquals('John Doe', $this->feedItemList->getFirstItem()->hEntry()->author->name);
0 ignored issues
show
Bug introduced by
The method hEntry() does not seem to exist on object<Jkphl\Micrometa\Ports\Item\ItemInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
62
    }
63
64
    public function testNonExistingSecondNestedItem()
65
    {
66
        $this->expectException(OutOfBoundsException::class);
67
        $this->expectExceptionCode('1492418999');
68
69
        $this->feedItemList->hEntry(2);
0 ignored issues
show
Documentation Bug introduced by
The method hEntry does not exist on object<Jkphl\Micrometa\Ports\Item\ItemList>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
70
    }
71
}
72