PEAR2_Services_NYTimesPEAR2_Services_NYTimes-0.1.0/test/pear2.php.net/PEAR2_Services_NYTimes/Services/NYTimes/NewswireTest.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/**
 * PEAR2\Services\NYTimes\NewswireTest
 *
 * PHP version 5
 *
 * @category  Services
 * @package   PEAR2_Services_NYTimes
 * @author    Till Klampaeckel <till@php.net>
 * @copyright 2011 Till Klampaeckel
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version   SVN: $Id$
 * @link      https://github.com/pear2/Services_NYTimes
 */

/**
 * NewswireTest covers {@link \PEAR2\Services\NYTimes\Newswire}.
 *
 * This tests requires an api key to run until we mock it.
 *
 * @category  Services
 * @package   PEAR2_Services_NYTimes
 * @author    Till Klampaeckel <till@php.net>
 * @copyright 2011 Till Klampaeckel
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @link      https://github.com/pear2/Services_NYTimes
 */
namespace PEAR2\Services\NYTimes;
class NewswireTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var \PEAR2\Services\NYTimes\Newswire
     */
    protected $nw;

    /**
     * For now we skip all tests when no api key is defined.
     *
     * @return void
     */
    protected function setUp()
    {
        if (!defined('NEWSWIRE_API_KEY')) {
            $this->markTestSkipped("This requires an API key.");
        }
        $this->nw = new Newswire(NEWSWIRE_API_KEY);
    }

    /**
     * Lulz.
     *
     * @return void
     */
    public function testNewswireConstruct()
    {
        $this->assertInstanceOf('PEAR2\Services\NYTimes\Newswire', $this->nw);
    }

    /**
     * Created to make sure that query data is stripped.
     *
     * @return array
     * @see    self::testGetItemByUrl()
     */
    public static function urlProvider()
    {
        return array(
            array('http://www.nytimes.com/2011/07/09/science/space/09shuttle.html'),
            array('http://www.nytimes.com/2011/07/09/science/space/09shuttle.html?hp'),
        );
    }

    /**
     * Regular test item by url (json is used).
     *
     * @return void
     *
     * @dataProvider urlProvider
     */
    public function testGetItemByUrl($url)
    {
        $response = $this->nw->getItemByUrl($url);

        $this->assertInstanceOf('stdClass', $response);
    }

    /**
     * Make sure XML wrapped in DOMDocument is returned.
     *
     * @return void
     */
    public function testGetItemByUrlInExEmHell()
    {
        $response = $this->nw
            ->setResponseFormat('xml')
            ->getItemByUrl('http://www.nytimes.com/2011/07/09/science/space/09shuttle.html');

        $this->assertInstanceOf('\DOMDocument', $response);

        $this->assertEquals('OK', $response->getElementsByTagName('status')->item(0)->nodeValue);
    }

    /**
     * Confirm that we can do PHP too.
     *
     * @return void
     */
    public function testGetItemByUrlInPHP()
    {
        $response = $this->nw
            ->setResponseFormat('sphp')
            ->getItemByUrl('http://www.nytimes.com/2011/07/09/science/space/09shuttle.html');

        $this->assertInternalType('array', $response);
        $this->assertArrayHasKey('status', $response);
        $this->assertArrayHasKey('results', $response);
    }

    /**
     * Return all sections.
     *
     * @return void
     */
    public function testGetSections()
    {
        $sections = $this->nw->getSections();
        $this->assertInternalType('array', $sections);
    }
}
EOF