zend-blog-3-backend
468 строк · 15.7 Кб
1<?php
2
3namespace spec\App\API;4
5use App\API\DataConverter;6use App\Entity\Category;7use App\Entity\Comment;8use App\Entity\Commentator;9use App\Entity\GeoLocation;10use App\Entity\GeoLocationCity;11use App\Entity\GeoLocationCountry;12use App\Entity\Post;13use App\Entity\Tag;14use App\Entity\User;15use App\Repository\CategoryRepository;16use App\Repository\CommentRepository;17use App\Service\TextProcessor;18use Doctrine\ORM\EntityManager;19use PhpSpec\ObjectBehavior;20use ReflectionClass;21
22class DataConverterSpec extends ObjectBehavior23{
24public function let(EntityManager $em, TextProcessor $tp, CommentRepository $cmr, CategoryRepository $cr)25{26$this->beConstructedWith($em, $tp, $cmr, $cr);27}28
29public function it_is_initializable()30{31$this->shouldHaveType(DataConverter::class);32}33
34public function it_is_get_tag()35{36$tag = new Tag();37$tag38->setName('test-name')39->setUrl('test-url')40;41
42$this->getTag($tag)->shouldReturn(43[44'tag' => [45'id' => null,46'name' => 'test-name',47'url' => 'test-url',48],49]50);51
52$tag2 = new Tag();53$tag254->setName('test2-name')55->setUrl('test2-url')56;57
58$this->getTagArray([$tag, $tag2])->shouldReturn(59[60'tags' => [61[62'id' => null,63'name' => 'test-name',64'url' => 'test-url',65],66[67'id' => null,68'name' => 'test2-name',69'url' => 'test2-url',70],71],72]73);74}75
76public function it_is_get_category()77{78$category = new Category();79$category80->setName('test-name')81->setUrl('test-url')82;83
84$this->getCategory($category)->shouldReturn(85[86'category' => [87'id' => null,88'name' => 'test-name',89'url' => 'test-url',90'parent' => null,91'parentId' => null,92],93]94);95
96$category2 = new Category();97$category298->setName('test2-name')99->setUrl('test2-url')100;101
102$this->getCategoryArray([$category, $category2])->shouldReturn(103[104'categories' => [105[106'id' => null,107'name' => 'test-name',108'url' => 'test-url',109'parent' => null,110'parentId' => null,111],112[113'id' => null,114'name' => 'test2-name',115'url' => 'test2-url',116'parent' => null,117'parentId' => null,118],119],120]121);122}123
124public function it_is_get_commentator()125{126$reflectionClass = new ReflectionClass(Commentator::class);127$reflectionProperty = $reflectionClass->getProperty('id');128$reflectionProperty->setAccessible(true);129
130$commentator = new Commentator();131$commentator132->setName('test-name')133->setEmail('commentator@example.org')134->setWebsite('http://example.org')135;136
137$reflectionProperty->setValue($commentator, 13);138
139$this->getCommentator($commentator)->shouldReturn(140[141'commentator' => [142'id' => 13,143'name' => 'test-name',144'email' => 'commentator@example.org',145'website' => 'http://example.org',146'imageHash' => 'A9GSDZ',147'isMale' => true,148],149]150);151
152$commentator2 = new Commentator();153$commentator2154->setName('test2-name')155->setEmail('two@example.org')156->setWebsite('http://example.com')157->setGender(User::FEMALE)158;159
160$reflectionProperty->setValue($commentator2, 72);161
162$this->getCommentatorArray([$commentator, $commentator2])->shouldReturn(163[164'commentators' => [165[166'id' => 13,167'name' => 'test-name',168'email' => 'commentator@example.org',169'website' => 'http://example.org',170'imageHash' => 'A9GSDZ',171'isMale' => true,172],173[174'id' => 72,175'name' => 'test2-name',176'email' => 'two@example.org',177'website' => 'http://example.com',178'imageHash' => '07XXUP',179'isMale' => false,180],181],182]183);184}185
186public function it_is_get_comment()187{188$reflectionClass = new ReflectionClass(Commentator::class);189$reflectionProperty = $reflectionClass->getProperty('id');190$reflectionProperty->setAccessible(true);191
192$commentator = new Commentator();193$commentator194->setName('test-name')195->setEmail('commentator@example.org')196->setWebsite('http://example.org')197;198
199$reflectionProperty->setValue($commentator, 13);200
201$comment = new Comment();202$comment203->setText('Тестовый комментарий')204->setIpAddress('94.231.112.91')205->setTimeCreated(\DateTime::createFromFormat('Y-m-d H:i:s', '2016-02-28 01:30:49'))206->setCommentator($commentator)207;208
209$this->getComment($comment)->shouldReturn(210[211'comment' => [212'id' => null,213'text' => 'Тестовый комментарий',214'commentator' => 13,215'commentatorId' => 13,216'username' => 'test-name',217'email' => 'commentator@example.org',218'website' => 'http://example.org',219'ipAddr' => '94.231.112.91',220'city' => null,221'region' => null,222'country' => null,223'countryFlag' => '',224'parent' => null,225'imageHash' => 'A9GSDZ',226'deleted' => false,227'userAgent' => null,228'bot' => false,229'createdAt' => '2016-02-28T01:30:49+03:00',230],231]232);233
234$comment2 = new Comment();235$comment2236->setText('йцук фыва олдж')237->setIpAddress('62.72.188.111')238->setTimeCreated(\DateTime::createFromFormat('Y-m-d H:i:s', '2016-02-28 01:43:14'))239;240
241$location = new GeoLocation();242$city = new GeoLocationCity();243$country = new GeoLocationCountry();244
245$country->setName('Germany');246
247$city248->setCity('Frankfurt am Main')249->setRegion('Hessen')250->setCountry($country)251;252
253$location254->setCity($city)255->setIpAddress('62.72.188.111')256;257
258$comment2->setGeoLocation($location);259
260$commentator = new Commentator();261$commentator262->setName('test-name')263->setEmail('commentator@example.org')264->setWebsite('http://example.org')265;266
267$reflectionProperty->setValue($commentator, 34);268
269$comment2->setCommentator($commentator);270
271$this->getCommentArray([$comment, $comment2])->shouldReturn(272[273'comments' => [274[275'id' => null,276'text' => 'Тестовый комментарий',277'commentator' => 13,278'commentatorId' => 13,279'username' => 'test-name',280'email' => 'commentator@example.org',281'website' => 'http://example.org',282'ipAddr' => '94.231.112.91',283'city' => null,284'region' => null,285'country' => null,286'countryFlag' => '',287'parent' => null,288'imageHash' => 'A9GSDZ',289'deleted' => false,290'userAgent' => null,291'bot' => false,292'createdAt' => '2016-02-28T01:30:49+03:00',293],294[295'id' => null,296'text' => 'йцук фыва олдж',297'commentator' => 34,298'commentatorId' => 34,299'username' => 'test-name',300'email' => 'commentator@example.org',301'website' => 'http://example.org',302'ipAddr' => '62.72.188.111',303'city' => 'Frankfurt am Main',304'region' => 'Hessen',305'country' => 'Germany',306'countryFlag' => '',307'parent' => null,308'imageHash' => 'ZJQ6CD',309'deleted' => false,310'userAgent' => null,311'bot' => false,312'createdAt' => '2016-02-28T01:43:14+03:00',313],314],315]316);317
318$this->getCommentArray([$comment2], 'commentator')->shouldReturn(319[320'comments' => [321[322'id' => null,323'text' => 'йцук фыва олдж',324'commentator' => 34,325'commentatorId' => 34,326'username' => 'test-name',327'email' => 'commentator@example.org',328'website' => 'http://example.org',329'ipAddr' => '62.72.188.111',330'city' => 'Frankfurt am Main',331'region' => 'Hessen',332'country' => 'Germany',333'countryFlag' => '',334'parent' => null,335'imageHash' => 'ZJQ6CD',336'deleted' => false,337'userAgent' => null,338'bot' => false,339'createdAt' => '2016-02-28T01:43:14+03:00',340],341],342'commentators' => [343[344'id' => 34,345'name' => 'test-name',346'email' => 'commentator@example.org',347'website' => 'http://example.org',348'imageHash' => 'ZJQ6CD',349'isMale' => true,350],351],352]353);354}355
356public function it_is_get_post()357{358$category = new Category();359$category360->setName('PHP')361->setUrl('php')362;363
364$post = new Post();365$post366->setTitle('ещё о PHP')367->setUrl('esce-o-php')368->setCategory($category)369->setHide(false)370->setRawText('<p>Ещё одна запись о PHP</p>')371->setDescription('description PHP')372->setTimeCreated(\DateTime::createFromFormat('Y-m-d H:i:s', '2016-02-07 22:40:24'))373->setLastUpdate(\DateTime::createFromFormat('Y-m-d H:i:s', '2016-02-07 22:40:24'))374;375
376$this->getPost($post, 'category')->shouldReturn(377[378'post' => [379'id' => null,380'title' => 'ещё о PHP',381'url' => 'esce-o-php',382'category' => null,383'categoryId' => null,384'hidden' => false,385'disableComments' => false,386'text' => '<p>Ещё одна запись о PHP</p>',387'description' => 'description PHP',388'tagsString' => '',389'timeCreated' => '2016-02-07T22:40:24+03:00',390'lastUpdate' => '2016-02-07T22:40:24+03:00',391],392'categories' => [393[394'id' => null,395'name' => 'PHP',396'url' => 'php',397'parent' => null,398'parentId' => null,399],400],401]402);403
404$post2 = new Post();405$post2406->setTitle('Тестовая запись')407->setUrl('testovaya-zapis')408->setCategory($category)409->setHide(false)410->setRawText('<p>Тестовая запись, собственно...</p>')411->setDescription('метатег description')412->setTimeCreated(\DateTime::createFromFormat('Y-m-d H:i:s', '2016-01-11 01:05:33'))413->setLastUpdate(\DateTime::createFromFormat('Y-m-d H:i:s', '2016-01-11 01:05:33'))414;415
416//TODO duplicate categories417$this->getPostArray([$post, $post2], 'category')->shouldReturn(418[419'posts' => [420[421'id' => null,422'title' => 'ещё о PHP',423'url' => 'esce-o-php',424'category' => null,425'categoryId' => null,426'hidden' => false,427'disableComments' => false,428'text' => '<p>Ещё одна запись о PHP</p>',429'description' => 'description PHP',430'tagsString' => '',431'timeCreated' => '2016-02-07T22:40:24+03:00',432'lastUpdate' => '2016-02-07T22:40:24+03:00',433],434[435'id' => null,436'title' => 'Тестовая запись',437'url' => 'testovaya-zapis',438'category' => null,439'categoryId' => null,440'hidden' => false,441'disableComments' => false,442'text' => '<p>Тестовая запись, собственно...</p>',443'description' => 'метатег description',444'tagsString' => '',445'timeCreated' => '2016-01-11T01:05:33+03:00',446'lastUpdate' => '2016-01-11T01:05:33+03:00',447],448],449'categories' => [450[451'id' => null,452'name' => 'PHP',453'url' => 'php',454'parent' => null,455'parentId' => null,456],457[458'id' => null,459'name' => 'PHP',460'url' => 'php',461'parent' => null,462'parentId' => null,463],464],465]466);467}468}
469